Repo symbol

json_struct repository

c-plus-plus serialization json parse deserialization template-metaprogramming template-specialisations
Repo symbol

json_struct repository

c-plus-plus serialization json parse deserialization template-metaprogramming template-specialisations
Repo symbol

json_struct repository

c-plus-plus serialization json parse deserialization template-metaprogramming template-specialisations
Repo symbol

json_struct repository

c-plus-plus serialization json parse deserialization template-metaprogramming template-specialisations
Repo symbol

json_struct repository

c-plus-plus serialization json parse deserialization template-metaprogramming template-specialisations json_struct

Repository Summary

Description json_struct is a single header only C++ library for parsing JSON directly to C++ structs and vice versa
Checkout URI https://github.com/jorgen/json_struct.git
VCS Type git
VCS Version master
Last Updated 2025-06-10
Dev Status UNKNOWN
Released UNRELEASED
Tags c-plus-plus serialization json parse deserialization template-metaprogramming template-specialisations
Contributing Help Wanted (-)
Good First Issues (-)
Pull Requests to Review (-)

Packages

Name Version
json_struct 0.0.1

README

Structurize your JSON

Build status ClusterFuzzLite PR fuzzing

json_struct is a single header only library that parses JSON to C++ structs/classes and serializing structs/classes to JSON.

It is intended to be used by copying the json_struct.h file from the include folder into the include path for the project. It is only the json_struct.h file that is needed to serialize and deserialize json from structures.

It is dependent on some C++11 features and is tested on newer versions of gcc and clang. It is also tested on VS 2015 and newer.

Structs

json_struct can parse JSON and automatically populate structures with content by adding some metadata to the C++ structs.

{
    "One" : 1,
    "Two" : "two",
    "Three" : 3.333
}

can be parsed into a structure defined like this:

struct JsonObject
{
    int One;
    std::string Two;
    double Three;

    JS_OBJ(One, Two, Three);
};

or

struct JsonObject
{
    int One;
    std::string Two;
    double Three;
};
JS_OBJ_EXT(JsonObject, One, Two, Three);

Populating the struct would look like this:

JS::ParseContext context(json_data);
JsonObject obj;
context.parseTo(obj);

Serializing the struct to json could be done like this:

std::string pretty_json = JS::serializeStruct(obj);
// or
std::string compact_json = JS::serializeStruct(obj, JS::SerializerOptions(JS::SerializerOptions::Compact));

Maps

Sometimes the structure of the JSON is dependent on some value in the JSON. Say there is some input JSON that describes a transportation vehicle. It can looks something like this

{
  "type" : "car",
  "wheels" : 4,
  "electric" : true,
...
}

or it could look like this:

{
  "type" : "sailboat",
  "sail_area_m2" : 106.5,
  "swimming_platform": true,
...
}

This doesn’t fit well with the static nature of json_struct. However, it is possible to parse the JSON into a map structure, query some child members for data and then dispatch the conversion into an appropriate type.

```c++ void handle_data(const char *data, size_t size) { JS::Map map; JS::ParseContext parseContext(data, size, map);

File truncated at 100 lines see the full file

Repo symbol

json_struct repository

c-plus-plus serialization json parse deserialization template-metaprogramming template-specialisations
Repo symbol

json_struct repository

c-plus-plus serialization json parse deserialization template-metaprogramming template-specialisations
Repo symbol

json_struct repository

c-plus-plus serialization json parse deserialization template-metaprogramming template-specialisations
Repo symbol

json_struct repository

c-plus-plus serialization json parse deserialization template-metaprogramming template-specialisations