Git Product home page Git Product logo

cpp-json's Introduction

cpp-json is licensed under the GNU General Public License, version 2 or later.

NOTE: version 2.2 will be the last to not require C++11.

There are a few different JSON parsing libraries out there. But cpp-json aims to be the simplest to use while still being efficient by using modern c++ techniques. Additionally, this library is header only making it trivial to include in existing projects.

Currently, the only active item on the TODO list is to better support Unicode. The parser can be given iterators referring to wide characters, but there is no runtime detection or endian-ness detection (yet)

However, Unicode is generally well supported in the form of \uXXXX encoding, including code points which require surrogate pairs. The resulting json::value object will contain the string, encoded as UTF-8 since it is stored in a std::string.

Of course special consideration is needed when displaying these strings if they do in fact contain non ASCII characters.

So, for example,

{"test1" : "\uD840\uDC8A"}

will correctly parse and the object's "test1" member will have the byte sequence: 0xF0 0xA0 0x82 0x8A

Here is a simple example of the usage of this library:

#include "cpp-json/json.h"
#include <fstream>
#include <iostream>

int main() {
	// open a file
	std::ifstream file("test.json");

	// json::parse can take two iterators or a std::istream
	json::value json = json::parse(file);

	// you can access objects like associative array's easily
	// the result is a json::value
	// ... though in real code you may want to check the type first ;-)
	auto servlets = json["web-app"]["servlet"];

	// when dealing with arrays, you can just use iterators, 
	// or feel free to use C++11 ranged-for
	const json::array &a = as_array(servlets);
	for(auto it = a.begin(); it != a.end(); ++it) {
        	const json::value &v = *it;
        	// all basic types (numbers, strings, booleans) can be converted 
        	// to a string
        	std::cout << to_string(v["servlet-name"]) << std::endl;
	}
}

You can also programmatically create json::value objects like this:

int main(int argc, char *argv[]) {
	auto arr = json::array {
		1,
		2,
		3,
		4,
		"Testing 1 2 3",
		json::object{
			{ "hello", 1234 },
			{ "world", 5678 }
		}
	};
	
	std::cout << stringify(arr) << std::endl;
}

Which of course results in a object representing the following JSON:

[
	1,
	2,
	3,
	4,
	"Testing 1 2 3", 
	{
		"hello" : 1234,
		"world" : 5678
	}
]

Finally, this library is very fast, when processing a 190 MB JSON file I randomly selected, parsing took no more than 18 seconds on my machine. For a Qt4 JSON parsing library, you can also checkout my other project: QJson4

cpp-json's People

Contributors

eteran avatar

Watchers

 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.