https://github.com/snorrwe/minijson
Lightweight json library for C++
https://github.com/snorrwe/minijson
json json-library json-parser json-serialization json-serializer
Last synced: 11 days ago
JSON representation
Lightweight json library for C++
- Host: GitHub
- URL: https://github.com/snorrwe/minijson
- Owner: snorrwe
- License: mit
- Created: 2018-08-25T22:15:09.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-10-16T15:51:52.000Z (over 5 years ago)
- Last Synced: 2025-02-16T12:57:38.850Z (2 months ago)
- Topics: json, json-library, json-parser, json-serialization, json-serializer
- Language: C++
- Size: 27.3 KB
- Stars: 1
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# MiniJson
[](https://travis-ci.org/snorrwe/minijson)
[](https://codecov.io/gh/snorrwe/minijson)Lightweight header-only JSON library written in C++17. Just include the headers and you're good to go. __MiniJSON__ is a very opinionated framework for parsing `JSON`.
__MiniJSON__ might be a suitable for your application if your `JSON` communication has a very strict schema.## Requirements
- C++17 compatible compiler
## Usage
To be able to parse custom data structures the structures must provide a `static` `constexpr` method named `json_propertied` that returns a tuple of the `public` data members that we wish to serialise / parse.
```cpp
struct Seed
{
// Serialisable data members must be declared public
float radius = 0.0;constexpr static auto json_properties()
{
return std::make_tuple(mini_json::property(&Seed::radius, "radius"));
}
};
```## Usage example
```cpp
#include "json.h"
struct Seed
{
float radius = 0.0;constexpr static auto json_properties()
{
return std::make_tuple(mini_json::property(&Seed::radius, "radius"));
}
};struct Apple
{
std::string color = "";
int size = 0;
Seed seed;constexpr static auto json_properties()
{
return std::make_tuple(mini_json::property(&Apple::color, "color"),
mini_json::property(&Apple::seed, "seed"),
mini_json::property(&Apple::size, "size"));
}
};TEST_F(TestJsonParser, CanReadJsonIntoObject)
{
/*
{
"color":"red",
"size": -25,
"seed": {
"radius": -3.14
}
}
*/
const auto json = "{\"color\":\"red\",\"size\": -25,\"seed\":{\"radius\":-3.14}}"s;auto result = mini_json::parse(json.begin(), json.end());
EXPECT_EQ(result.color, "red");
EXPECT_EQ(result.size, -25);
EXPECT_FLOAT_EQ(result.seed.radius, -3.14f);
}
```## Supported types:
- Any `T` that implements the `json_properties` static member function
- `std::vector` for any json serialisable `T`
- `std::string`
- `int`
- `size_t`
- `float`
- `double`## Limitations:
- `std::tuple` is not yet supported.
- Optional fields are not supported.