Coder Social home page Coder Social logo

json's Introduction

Json

Simple Json file parser/writer writing in C++ for fun, just drop Json.hpp to your project and it will works.

Usage

#include <iostream>
#include <string>
#include "Json.hpp"

int main()
{
	auto obj = Json::CreateJson<Json::Object>();

	if (obj)
	{
		auto &object = *obj;
		object["String"] = Json::CreateJson<Json::String>("Json String");
		object["Number"] = Json::CreateJson<Json::Number>(1234);
		object["Null"] = Json::CreateJson<Json::Null>();
		object["Obj"] = Json::CreateJson<Json::Object>();
		auto array = object["Array"] = Json::CreateJson<Json::Array>();
		
		// fill array
		auto arrayPtr = Json::ConvertJson<Json::Array>(array);
		for (auto i = 0; i < 10; ++i)
		{
			arrayPtr->push_back(Json::CreateJson<Json::Number>(i));
		}

		// write to file
		Json::FileWriter writer;
		writer.write("test.json", obj);
	}


	try
	{
		// read
		Json::FileReader reader;
		auto obj = Json::ConvertJson<Json::Object>(reader.parseFile("test.json"));
		// or parse from std string
		// auto obj = Json::ConvertJson<Json::Object>(reader.parseFile("{}"));

		auto str = obj->get<Json::String>("String")->toSTDString();
		auto number = obj->get<Json::Number>("Number")->toLongLong();
		auto arrayPtr = obj->get<Json::Array>("Array");
		
		if (arrayPtr)
		{
			auto const &array = *arrayPtr;
			for (auto i = 0U; i < array.count(); ++i)
			{
				auto item = Json::ConvertJson<Json::Number>(array[i])->toLongLong();
			}
		}
	}
	catch (Json::ParserException const &e)
	{
		std::cout << e.getMessage() << std::endl;
	}

	return EXIT_SUCCESS;
}

json's People

Contributors

shangluo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  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.