Git Product home page Git Product logo

ethiffeault / ejson Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 185 KB

json single header only c++. Parsing is stream friendly to minimize memory footprint for large json file. Support both char and wide char. Provide a "Value" type to handle underlying json type making it very easy manipulation in code.

License: MIT License

C++ 99.96% C# 0.04% C 0.01%
json cpp header-only-library stream rtti

ejson's Introduction

ejson

json library in c++

Single header only c++. Parsing is stream friendly to minimize memory footprint for large json file. Support both char and wide char. Provide a "Value" type to handle underlying json type (null, bool, number, string, array and object) making it very easy manipulation in code.

Parsing is separated in 2 layers, making it very easy to customize and implement json serialization for any rtti library:

read json from string:

    std::wstring input = L"{\"name\":\"John\"}";
    ejson::Value value;
    ejson::Read(input, value);
    std::wcout << "value name is : " << value[L"name"].AsString() << std::endl;
    value name is : John

write it back to a string:

    std::wstring output;
    ejson::Write(value, output);

    if (input == output)
        std::wcout << "input and output are the same: " << std::endl;;
    std::wcout << output << std::endl << std::endl;
    input and output are the same:
    {"name":"John"}

write prettify json to string:

    std::wstring prettifyOutput;
    ejson::Write(value, prettifyOutput, true);
    std::wcout << prettifyOutput << std::endl << std::endl;
    {
        "name": "John"
    }

create json in typed code

        Value json;
        json[L"FirstName"] = L"John";
        json[L"LastName"] = L"Doe";
        json[L"Age"] = 71;
        json[L"Music"][0] = L"punk";
        json[L"Music"][1] = L"country";
        json[L"Music"][2] = L"folk";

        std::wstring str;
        Write(json, str);
        std::wcout << str << std::endl << std::endl;
{"FirstName":"John","LastName":"Doe","Age":71,"Music":["punk","country","folk"]}

File

Read

    std::wifstream input("..\\data\\john_doe.json");
    ejson::Value value;
    ejson::Read(input, value);

    std::wstring output;
    ejson::Write(value, output);
    std::wcout << output;
    {"FirstName":"John","LastName":"Doe","Age":71,"Music":["punk","country","folk"]}

Write

read from file and write back to another file in pretty format:

    // read
    std::wifstream input("..\\data\\john_doe.json");
    ejson::Value value;
    ejson::Read(input, value);

    // write
    std::wofstream output("..\\data\\john_doe_output.json");
    ejson::Write(value, output, true);

john_doe_output.json:

    {
        "FirstName": "John",
        "LastName": "Doe",
        "Age": 71,
        "Music": [
            "punk",
            "country",
            "folk"
        ]
    }

Json as cpp

create json in typed code

        ejson::Value json;
        json[L"FirstName"] = L"John";
        json[L"LastName"] = L"Doe";
        json[L"Age"] = 71;
        json[L"Music"][0] = L"punk";
        json[L"Music"][1] = L"country";
        json[L"Music"][2] = L"folk";

        std::wstring str;
        ejson::Write(json, str);
        std::wcout << str << std::endl << std::endl;
{"FirstName":"John","LastName":"Doe","Age":71,"Music":["punk","country","folk"]}

could be build composed:

    ejson::Value json;
    json[L"FirstName"] = L"John";
    json[L"LastName"] = L"Doe";
    json[L"Age"] = 71;
    ejson::Value& music = json[L"Music"];
    music[0] = L"punk";
    music[1] = L"country";
    music[2] = L"folk";

when loading data from file in read only, to make sure to not change input Value, use const Value& for your queries and make validation like this:

    // read
    std::wifstream input("..\\data\\john_doe.json");
    ejson::Value fileValue;
    ejson::Read(input, fileValue);

    // use const& here to prevent appending value to fileValue
    ejson::Value& value = fileValue;
    if (value[L"FirstName"].IsString())
    {
        std::wstring firstName = value[L"FirstName"].AsString();
        //...
    }

Error

read with error:

    std::wifstream input("..\\data\\john_doe_err.json");
    ejson::Value value;
    ejson::ParserError error;
    if (ejson::Read(input, value, error))
    {
        // no error ...
    }
    else
    {
        // error
        std::wcout << "error at line/column " << error.Line << "/" << error.Column << ": " << error.Error;
    }
    error at line/column 3/6: invalid token

Configuration

modify configuration at the beginning of ejson.h

char/wchar_t

    #define EJSON_WCHAR 1 // wchar_t (default)
    #define EJSON_WCHAR 0 // char

Assert

    #define EJSON_ASSERT // (default impl use cassert)
    #define EJSON_ERROR 

float/double

    using number = double; // (default)
    using number = float;

order ot not

    #define EJSON_MAP_ORDERED 1 // (default, keep load/write ordered)
    #define EJSON_MAP_ORDERED 0 // (faster, don't keep ordered, ex: suitable for final build that only read)

Others

eti use awesome great unit tests framework: doctest

ejson's People

Contributors

ethiffeault avatar

Watchers

 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.