Giter Site home page Giter Site logo

cppson's Introduction

cppson

cpp json utility.

korean

Setup

just add a 'src/cppson.hpp' file to your project.

Branch

  • v-1.0 : A zero-dependency version which uses its own parsing system (inefficient, slow, unstable).

  • jsoncpp-compatible : use jsoncpp to parse json file. recommand to use this version.

Requirement

  • Visual studio 2013 +

mapping

json C++
integer int
real number float, double
bool bool
string std::string
array std::vector
object JSON_CLASS

How to use

JSON_CLASS(className) macro that creates a class mapped to json file.
FIELD(type, fieldName) macro that creates a field mapped to json key - value pair. member created by FIELD macro has below methods:

  • isNull : whether the field has a value or not.
  • get : return that field's inner value.
  • operator -> : if that field's inner value type is class or structure, using operator -> to access inner value's member.

field type is compatible with that field's inner value type.

and, call loadFile function(jsonClass.loadFile), automatically mapped json file to that class.

parse vector from json file, use cppson::loadFile function.

See the example below.

Example

#include "cppson.hpp"

JSON_CLASS(Test)
{
public:
	FIELD(int, field1);
	FIELD(int, field2);
	FIELD(std::vector<int>, field3);
};

int main()
{
	Test t;
	
	//call loadFile, json file data mapped to class member variable.
	if(t.loadFile("test.json"))
	{
		printf("%d %d %d\n", t.field1, t.field2); //1 2
		printf("%d\n", t.field3->size()); //0
	}
	else
	{
		printf("parse failed.\n");
	}

	std::vector<int> vi;

	if(cppson::loadFile("test2.json"))
	{
		//1, 2, 3, 4, 5, 10
		for(auto i : vi)
		{
			printf("%d ", i);
		}
	}
	
	return 0;
}
//test.json
{
	"field1" : 1,
	"field2" : 2,
	"field3" : []
}
//test2.json
[
	1, 2, 3, 4, 5, 10
]

cppson's People

Contributors

jwvg0425 avatar pjc024789 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

cppson's Issues

move sementics

handle boolean type values which not wrapped with quotes.

In standard json format, json can accept 'null', 'false' and 'true' values. but your implementation cannot parse boolean types which not wrapped with quotes("").

expected result

JSON_CLASS(Foo)
{
public:
  FIELD(bool, b);
}
{
  "b" : true
}

toJson

instance data to json file

add tests

#define CATCH_CONFIG_MAIN
#include "catch.h"

TEST_CASE("parse from string") {
    JSON_CLASS(NestedObject)
    {
    public:
        FIELD(int, _int);
        FIELD(bool, _bool);
    };
    JSON_CLASS(TestObject)
    {
    public:
        FIELD(int, _int);
        FIELD(bool, _bool);
        //FIELD(float, _float);
        FIELD(std::string, _string);

        FIELD(std::vector<int>, _vector);

        FIELD(NestedObject, _nested);
    };

    TestObject obj;
    obj.ParseFromString(
        "{"
        /* for 'basic types' section */
        "\"_int\" : 5,"
        "\"_bool\" : \"true\","
        //"\"_float\" : 2.5,"
        "\"_string\" : \"hello\","

        /* for 'std::vector type' section */
        "\"_vector\" : [1, 2, 3 ,4 ,5],"

        /* for 'std::map type' section */
        //"\"_vector\" : [1, 2, 3 ,4 ,5],"

        /* for 'nested object' section */
        "\"_nested\" : {"
            "\"_int\" : 14,"
            "\"_bool\" : \"false\""
            "}"
        "}");

    SECTION("basic types") {
        REQUIRE(obj._int == 5);
        REQUIRE(obj._bool == true);
        //REQUIRE(obj._float == 5.0f);
        REQUIRE(obj._string == "hello");
    }
    SECTION("std::vector type") {
        REQUIRE(obj._vector.size() == 5);
        REQUIRE(obj._vector == (std::vector<int>{1, 2, 3, 4, 5}));
    }
    SECTION("std::map type") {
        /* .... */
    }
    SECTION("nested object") {
        REQUIRE(obj._nested._int == 14);
        REQUIRE(obj._nested._bool == false);
    }
}

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.