/JSON-cpp

Simple serialization and json file manipulation in C++11

Primary LanguageC++MIT LicenseMIT

JSON-cpp Build Status

This project was awarded with the first prize on the VII ULL Free Software Contest

Simple serialization and json file manipulation for C++11 on the fly!

This library allows you to do some fancy stuff like these:

Serialization

#include "..../serializable.hpp"

class A : public json::Serializable {
  int a;
  string b;
  bool c;
  std::vector <int> d;

  SERIAL_START    // magic macro for reflection
    "a", a,       // key / value
    "b", b,
    "c", c,
    "d", d
  SERIAL_END
};

int main (void) {
  A obj;
  // some values initializations
  A.serializeOut ("file.json"); // obviously there is the serializeIn equivalent
}

file.json

{
  "a" : 12,
  "b" : "this is awesome!",
  "c" : true,
  "d" : [
    56, 78, 90, 10
  ]
}

Raw json manipulation

#include "..../manager.hpp"
#include "..../parser.hpp"
int main (void) {
  json::Parser parser;
  json::JsonTree tree;

  parser.parseFile("file.json", tree);

  int a;
  tree.get(a, "a");
  std::cout << a << std::endl;
  tree.get(a, "b");             // data won't change due to invalid types
  std::cout << a << std::endl;
}     

ouput

12
ERROR : Attempting to load the element 'b' as a NUMBER_INT when is of type STRING.
12

Ok, but what can I serialize?

  • Both value & pointer

    • int, long int, long long
    • float, double
    • bool
    • string, char
    • custom serializable classes
  • Only value

    • std::vector <any_of_the_above>
    • std::vector <any_of_the_above*>
  • Magical features

    • Inheritance of custom serializable classes (any depth)
    • Polymorphism of custom serializable classes
    • n-dimensional matrixes: std::vector < std::vector <std::vector <.....> > >

Note: pointers arrays currently not work, use std::vector instead

Usage

Just copy the /src content into your project and include the convenient headers

#include "..../parser.h"         // for reading json files
#include "..../manager.h"        // for managing json data
#include "..../serializable.h"   // for creating custom serializable classes

Third party libraries

Special thanks to Eleazar Díaz