GithubHelp home page GithubHelp logo

skyformat99 / jsonla Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ggicci/jsonla

0.0 2.0 0.0 54 KB

A cute C++ library for easily parsing and manipulating JSON data structure.

License: GNU General Public License v3.0

C++ 100.00%

jsonla's Introduction

jsonla

A cute C++ library for easily parsing and manipulating JSON data structure.

Parser

Convert a json string to a plain C++ object. Then you can retrieve data or modify them by operating on the C++ object.

Now you have a json structural string like { "year": 2013, "month": 8, "day": 29 }, and just use the static function ggicci::Json::Parse(const char*) to get a ggicci::Json object. Then you can do what you want on it. See below.

Files to Include

There're only two files: jsonla.h and jsonla.cc. You have to include jsonla.h. And the namespace is ggicci. We assume that using namespace ggicci; already declared.

Manipulation

Construct a Json Object

// from int
Json json(3);
// from double
Json json(4.23);
// from bool
Json json(true);
// from string, no parse
Json json("hello world");
// from a json string, parse
Json json = Json::Parse("[1, false, null, {\"name\": \"Ggicci\", \"age\": 21}");
// null
Json json; // json.IsNull(); --> true

Data Retrieving

Json json = Json::Parse("{				\
	\"id\": 20130192,					\
	\"user\": \"ggicci\",				\
	\"mail\": \"[email protected]\",		\
	\"password\": \"md5 code here\",	\
	\"birthday\": 19911110,				\
	\"gender\": 1, 						\
	\"tags\": [							\
		\"program fans\",				\
		\"dog\",						\
		\"nightbird\",					\
		\"anime fans\",					\
		\"almost perfectionist\"		\
	],									\
	\"motto\": null						\
}");
cout << json.ToString() << endl;
// output: 
// { "birthday": 1.99111e+007, "gender": 1, "id": 2.01302e+010, "mail": "[email protected]", "motto": null, "password": "md5 code here", "tags": [ "program fans", "dog", "nightbird", "anime fans", "almost perfectionist" ], "user": "ggicci" }

cout << json["id"].AsInt() << endl; // output: 20130192
cout << json["user"].AsString() << endl; // output: ggicci
cout << (json["motto"].IsNull() ? "lazy man leaves nothing..."
	: json["motto"].AsString()) << endl; // output: lazy man leaves nothing...

// Traverse Array
Json &tags = json["tags"];
for (int i = 0; i < tags.Size(); ++i)
{
	cout << tags[i].AsString() << endl;
}
// output:
// program fans
// dog
// nightbird
// anime fans
// almost perfectionist

// Traverse Object
vector<string> keys = json.Keys();
for (vector<string>::const_iterator cit = keys.begin();
	cit != keys.end(); ++cit)
{
	cout << *cit << ": " << json[cit->c_str()].ToString() << endl;
}
// output:
// birthday: 1.99111e+007
// gender: 1
// id: 2.01302e+007
// mail: "[email protected]"
// motto: null
// password: "md5 code here"
// tags: [ "program fans", "dog", "nightbird", "anime fans", "almost perfectionist" ]
// user: "ggicci"

Data Modification

// Assignment
Json json;		// null
json = 1;		// 1
json = 3.234;	// 3.234
json = true;	// true
json = "hello";	// "hello"
json = Json::Parse("[1, 2, 3, 4]"); // array
json = Json();	// null

// Reference
Json json = Json::Parse("[1, 2, 3, 4]");
json[0] = 0; // [0, 2, 3, 4]
Json &second = json[1];
second = "hello"; // [ 0, "hello", 3, 4 ]

// Copy
Json json = Json::Parse("[1, 2, 3, 4]");
Json co_third = json[2];
co_third = true; // [1, 2, 3, 4]

// Push Values to Array
Json json(1);
json.Push(Json("hello")).Push(Json()).Push(Json::Parse("{}"));
// [ 1, "hello", null, {  } ]

// Add Properties to an Object
Json json = Json::Parse("{}");
json.AddProperty("id", Json(1))
	.AddProperty("name", Json("ggicci"))
	.AddProperty("motto", Json());
// { "id": 1, "motto": null, "name": "ggicci" }
// Exception(a bad conversion) will be thrown if you apply
// AddProperty() on a non-object Json object.

// Remove Item from an Array, no cascade
Json json = Json::Parse("[1, 2, 3, 4]");
json.Remove(0);
json.Remove(1);
json.Remove(8); // invalid index do noting
cout << json.ToString() << endl;
// [ 2, 4 ]

// Remove Item from an Object, can cascade
Json json = Json::Parse("{ \"id\": 123, \"name\": \"ggicci\", \"gender\": 1 }");
json.Remove("id").Remove("gender").Remove("age");
cout << json.ToString() << endl;
// { "name": "ggicci" }

Exception Handling

// Parse Exception
try
{
	Json json = Json::Parse("[1, 2, 3, 4, ]");
}
catch (exception& e)
{
	cout << e.what() << endl; // SyntaxError: Unexpected token ] at pos 13
}

// Convert Exception
try
{
	Json json = Json::Parse("[1, 2, 3, 4, 5]");
	json[0].AsString(); // should use AsInt() or AsDouble().
}
catch (exception& e)
{
	cout << e.what() << endl; // a bad conversion
}	

More

Note

There may exist potential bugs, if you found some bugs, please be sure to let me know.

jsonla's People

Contributors

ggicci avatar

Watchers

skyformat99 avatar  avatar

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.