Skip to content

A JSON stringifier / parser that uses boost fusion introspection methods for automagic struct <-> JSON conversion (including allmost all STL containers)

License

Notifications You must be signed in to change notification settings

5cript/SimpleJSON

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SimpleJSON

A JSON stringifier / parser that uses boost fusion introspection methods for automagic struct <-> JSON conversion

This library is not fine tuned for speed. The slowest part should be the boost/property_tree library used for parsing JSON.

Dependencies:

boost/property_tree
boost/fusion

Code example:

#ifndef Q_MOC_RUN // A Qt workaround, for those of you who use Qt
#   include "parse/jsd.h"
#   include "parse/jsd_convenience.h"
#   include "stringify/jss.h"
#   include "stringify/jss_fusion_adapted_struct.h"
#endif

#include <string>
#include <vector>

struct ConfigContent : public JSON::FusionStruct <ConfigContent>
                     , public JSON::ParsableStruct <ConfigContent>
{
  int id;
  std::string libPath;
  std::vector <std::string> someArray;
};

BOOST_FUSION_ADAPT_STRUCT
(
    ConfigContent,
    (int, id)
    (std::string, libPath)
    (std::vector <std::string>, someArray)
)

ConfigContent parse(std::string const& json)
{
  ConfigContent cc;
  auto tree = JSON::parse_json(json);
  JSON::js_parse(cc, "config_content", tree);
  return cc;
}

std::string stringify(ConfigContent const& cc)
{
  return std::string("{\"config_content\":") + JSON::js_try_stringify("config_content", cc) + "}";
}

int main()
{
  std::string str;
  ConfigContent cc;
  cc.id = 2;

  str = stringify(cc);
  auto unnecessarilyComplexCopy = parse(str);

  // assert unnecessarilyComplexCopy == cc
}

About

A JSON stringifier / parser that uses boost fusion introspection methods for automagic struct <-> JSON conversion (including allmost all STL containers)

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages