Code Monkey home page Code Monkey logo

jsonbuilder's Introduction

Build Status

JsonBuilder

JsonBuilder is a small C++ library for building a space-efficient binary representation of structured data and, when ready, rendering it to JSON. The library offers STL-like syntax for adding and finding data as well as STL-like iterators for efficiently tracking location.

Examples

Building structured data

Let's try to build the following JSON up using the JsonBuilder interface:

{
    "e": 2.718,
    "enabled": true,
    "user": "john",
    "resolution": {
        "x": 1024,
        "y": 768
    },
    "colors": [
        "Red",
        "Green",
        "Blue"
    ]
}

The code to do so would look like this:

JsonBuilder jb;
jb.push_back(jb.end(), "e", 2.718);
jb.push_back(jb.end(), "enabled", true);
jb.push_back(jb.end(), "user", "john");

JsonIterator resolutionItr = jb.push_back(jb.end(), "resolution", JsonObject);
jb.push_back(resolutionItr, "x", 1024);
jb.push_back(resolutionItr, "y", 768);

JsonIterator colorIterator = jb.push_back(jb.end(), "colors", JsonArray);
jb.push_back(colorIterator, "", "Red");
jb.push_back(colorIterator, "", "Green");
jb.push_back(colorIterator, "", "Blue");

Getting an iterator to existing data

Using the built JsonBuilder object above as a starting point:

// Float
JsonConstIterator eItr = jb.find("e");
float e = eItr->GetUnchecked<float>();
std::cout << e << std::endl;

// Object
JsonConstIterator resolutionItr = jb.find("resolution");
for (JsonConstIterator beginItr = resolutionItr.begin(),
                        endItr = resolutionItr.end();
        beginItr != endItr;
        ++beginItr)
{
    std::string name(beginItr->Name().data(), beginItr->Name().length());
    std::cout << name << " " << beginItr->GetUnchecked<int64_t>()
                << std::endl;
}

// Array
JsonConstIterator colorsItr = jb.find("colors");
for (JsonConstIterator beginItr = colorsItr.begin(), endItr = colorsItr.end();
        beginItr != endItr;
        ++beginItr)
{
    auto color = beginItr->GetUnchecked<std::string_view>();
    std::cout << color << std::endl;
}

Rendering to JSON

Using the built JsonBuilder object above as a starting point:

// Create a renderer and reserve 2048 bytes up front
JsonRenderer renderer;
renderer.Reserve(2048);

// Render a json builder object to a string
std::string_view result = renderer.Render(_jsonBuilder);
std::string stl_string(result.data(), result.size());
std::cout << stl_string.c_str() << std::endl;

Dependencies

The Linux tests for this project depend on the uuid library. To develop with this project, install the development version of the library:

sudo apt-get install uuid-dev

If you checkout with submodules, you will receive a version of Catch2 for testing that can be used automatically. If you do not checkout this submodule, the build system will instead search for an installed version of Catch2 and use that to build the tests.

Integration

JsonBuilder builds as a static library and requires C++17. The project creates a CMake compatible 'jsonbuilder' target which you can use for linking against the library.

  1. Add this project as a subdirectory in your project, either as a git submodule or copying the code directly.
  2. Add that directory to your top-level CMakeLists.txt with 'add_subdirectory'. This will make the 'jsonbuilder' target available.
  3. Add the 'jsonbuilder' target to the target_link_libraries of any target that will use the JsonBuilder library.

Reporting Security Issues

Security issues and bugs should be reported privately, via email, to the Microsoft Security Response Center (MSRC) at <[email protected]>. You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Further information, including the MSRC PGP key, can be found in the Security TechCenter.

Code of Conduct

This project has adopted the Microsoft Open Source Code of Conduct.

For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

Contributing

Want to contribute? The team encourages community feedback and contributions. Please follow our contributing guidelines.

We also welcome issues submitted on GitHub.

Project Status

This project is currently in active development.

Contact

The easiest way to contact us is via the Issues page.

jsonbuilder's People

Contributors

bfulper avatar idigdoug avatar jsmrcina avatar microsoft-github-policy-service[bot] avatar microsoftopensource avatar msftgits avatar nickbopp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

jsonbuilder's Issues

Cannot build with `catch` versions 3 and up.

While trying to build jsonbuilder, I've hit the following errors:

time="2024-02-20T12:00:04Z" level=debug msg="/usr/bin/make  -f test/CMakeFiles/jsonbuilderTest.dir/build.make test/CMakeFiles/jsonbuilderTest.dir/build"
time="2024-02-20T12:00:04Z" level=debug msg="/usr/src/mariner/BUILD/jsonbuilder-0.2.1/test/CatchMain.cpp:5:10: fatal error: catch2/catch.hpp: No such file or directory"
time="2024-02-20T12:00:04Z" level=debug msg="    5 | #include <catch2/catch.hpp>"
time="2024-02-20T12:00:04Z" level=debug msg="      |          ^~~~~~~~~~~~~~~~~~"
time="2024-02-20T12:00:04Z" level=debug msg="compilation terminated."
time="2024-02-20T12:00:04Z" level=debug msg="make[2]: *** [test/CMakeFiles/jsonbuilderTest.dir/build.make:79: test/CMakeFiles/jsonbuilderTest.dir/CatchMain.cpp.o] Error 1"
time="2024-02-20T12:00:04Z" level=debug msg="make[2]: Entering directory '/usr/src/mariner/BUILD/jsonbuilder-0.2.1/build'"
time="2024-02-20T12:00:04Z" level=debug msg="[ 62%] Building CXX object test/CMakeFiles/jsonbuilderTest.dir/CatchMain.cpp.o"
time="2024-02-20T12:00:04Z" level=debug msg="cd /usr/src/mariner/BUILD/jsonbuilder-0.2.1/build/test && /usr/lib/ccache/c++  -I/usr/src/mariner/BUILD/jsonbuilder-0.2.1/include -isystem /usr/include/uuid -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/mariner/default-hardened-cc1   -fcommon -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -Wall -Wextra -MD -MT test/CMakeFiles/jsonbuilderTest.dir/CatchMain.cpp.o -MF CMakeFiles/jsonbuilderTest.dir/CatchMain.cpp.o.d -o CMakeFiles/jsonbuilderTest.dir/CatchMain.cpp.o -c /usr/src/mariner/BUILD/jsonbuilder-0.2.1/test/CatchMain.cpp"
time="2024-02-20T12:00:04Z" level=debug msg="make[2]: Leaving directory '/usr/src/mariner/BUILD/jsonbuilder-0.2.1/build'"
time="2024-02-20T12:00:04Z" level=debug msg="make[2]: *** Waiting for unfinished jobs...."
time="2024-02-20T12:00:04Z" level=debug msg="/usr/src/mariner/BUILD/jsonbuilder-0.2.1/test/TestBuilder.cpp:4:10: fatal error: catch2/catch.hpp: No such file or directory"
time="2024-02-20T12:00:04Z" level=debug msg="    4 | #include <catch2/catch.hpp>"
time="2024-02-20T12:00:04Z" level=debug msg="      |          ^~~~~~~~~~~~~~~~~~"
time="2024-02-20T12:00:04Z" level=debug msg="compilation terminated."
time="2024-02-20T12:00:04Z" level=debug msg="make[2]: *** [test/CMakeFiles/jsonbuilderTest.dir/build.make:93: test/CMakeFiles/jsonbuilderTest.dir/TestBuilder.cpp.o] Error 1"
time="2024-02-20T12:00:04Z" level=debug msg="make[2]: Entering directory '/usr/src/mariner/BUILD/jsonbuilder-0.2.1/build'"
time="2024-02-20T12:00:04Z" level=debug msg="[ 75%] Building CXX object test/CMakeFiles/jsonbuilderTest.dir/TestBuilder.cpp.o"
time="2024-02-20T12:00:04Z" level=debug msg="cd /usr/src/mariner/BUILD/jsonbuilder-0.2.1/build/test && /usr/lib/ccache/c++  -I/usr/src/mariner/BUILD/jsonbuilder-0.2.1/include -isystem /usr/include/uuid -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/mariner/default-hardened-cc1   -fcommon -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -Wall -Wextra -MD -MT test/CMakeFiles/jsonbuilderTest.dir/TestBuilder.cpp.o -MF CMakeFiles/jsonbuilderTest.dir/TestBuilder.cpp.o.d -o CMakeFiles/jsonbuilderTest.dir/TestBuilder.cpp.o -c /usr/src/mariner/BUILD/jsonbuilder-0.2.1/test/TestBuilder.cpp"
time="2024-02-20T12:00:04Z" level=debug msg="make[2]: Leaving directory '/usr/src/mariner/BUILD/jsonbuilder-0.2.1/build'"
time="2024-02-20T12:00:05Z" level=debug msg="/usr/src/mariner/BUILD/jsonbuilder-0.2.1/test/TestRenderer.cpp:8:10: fatal error: catch2/catch.hpp: No such file or directory"
time="2024-02-20T12:00:05Z" level=debug msg="    8 | #include <catch2/catch.hpp>"
time="2024-02-20T12:00:05Z" level=debug msg="      |          ^~~~~~~~~~~~~~~~~~"
time="2024-02-20T12:00:05Z" level=debug msg="compilation terminated."

It looks like catch has removed the catch2/catch.hpp header and split it into smaller chunks (see: 3.0.0-preview4 release notes).

I've tried to summarize the changes, which allowed me to build jsonbuilder with the 3+ version of catch in #30.

linux specific cmake

when attempting to cmake I get an error:
cl : command line error D8021: invalid numeric argument '/Wextra'
cannot open uuid.h

which as far as I can tell is linux specific?

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.