Coder Social home page Coder Social logo

minijson's Introduction

MiniJson

Build Status codecov

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.

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

#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<Apple>(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<T> for any json serialisable T
  • std::string
  • int
  • size_t
  • float
  • double

Limitations:

  • std::tuple is not yet supported.
  • Optional fields are not supported.

minijson's People

Contributors

dkiss-lateral avatar nefelejcske avatar snorrwe avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

nefelejcske

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.