Giter Club home page Giter Club logo

esj's Introduction

esj

###Extremely Simple JSON for C++

Enable bi-directional JSON serialization of C++ classes with via dependency-free header only library written in tested, portable C++ (Visual C++/Clang/GCC/ARM)

Usage:

//-----------------------------------------------------------------------------
// Code support required for serialization.
class JSONExample
{
public:
    // to be JSON'ised
    std::string text;
public:
    // each class requires a public serialize function
    void serialize(JSON::Adapter& adapter)
    {
        // this pattern is required 
        JSON::Class root(adapter,"JSONExample");
        // this is the last member variable we serialize so use the _T variant
        JSON_T(adapter,text);
    }
};
// demonstrate how to first produce and then consume JSON strings
int main(int argc,char* argv[])
{
    // try/catch omitted for brevity
    // The JSON enabled class as above
    JSONExample source;
    source.text = "Hello JSON World";
    // create JSON from a producer
    std::string json = JSON::producer<JSONExample>::convert(source);
    // and then create a new instance from a consumer ...
    JSONExample sink = JSON::consumer<JSONExample>::convert(json);
    // we are done ...
}

produces:

{"JSONExample":{"text":"Hello JSON World"}}

###Notes

std::string maps to JSON string.

std::wstring maps to JSON string with support for \UXXXX encoding and decoding (UTF16 and UTF32).

int maps to JSON number (ignores the fractional part when de-serializing).

double also maps to JSON number.

bool maps to JSON true or false.

std::vector maps directly to a JSON array.

If T implements the correct serialize() function, then the serializer will work as expected for vectors of T.

The serializer will also correctly handle nested serializable instances, thus allowing fairly complex constructs to be easily transformed to and from JSON.

Single line macros used for brevity and stringizing only.

N.B. The current release requires c++11 support. Please file an issue if this stops you from using ESJ!

See /doc for more or visit: http://www.codeproject.com/script/Articles/View.aspx?aid=856277

esj's People

Contributors

btcinsight avatar g40 avatar tustvold avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.