No description, website, or topics provided.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
include/cson remove betetr_t from cson() Jul 21, 2014
.gitignore fixes issues with std::vector Jul 19, 2014
LICENSE.txt changed license as per the advise by Amazon Legal team. Feb 26, 2018
README.md added mixed access Jul 20, 2014
makefile just a first version of cson Jul 19, 2014
test.cpp added mixed access Jul 20, 2014

README.md

C++ Simple Object Notation (CSON)

CSON allows users to create objects with named type-safe attributes on the fly, using notation inspired from JSON:

auto x = cson(name = "Rafi", age = 55, work = "Singing");

std::cout << x.name << "," << x.age << "," << x.work << std::endl;

Here the type of each attribute is statically determined during the creation of object — which means x.name is std::string, x.age is int and so on:

x.age = "I'm a string"; //compilation error :  age is int
x.age++;                //okay         
x.worK = "New job";     //compilation error :  object x has no member named `worK`

That's very intuitive.

###More examples

#include <iostream>
#include <cson/cson.h++>

//creates few labels first
CSON_LABEL(name);
CSON_LABEL(age);
CSON_LABEL(work);
CSON_LABEL(func);
CSON_LABEL(child);
CSON_LABEL(items);

int main()
{
	//create a cson object with some attributes, even nested cson object
	auto x = cson
	(
		name = "Rafi",
		age = 55,
		items = {1,2,3,4,5},  //this turns into a std::vector<int>
		func = [](std::string const & name)  //can be a functor/function/lambda also
		{ 
			std::cout << ("Hello " + name)  << std::endl; 
		},
		child = cson   //nested cson object
		(
			name  = 100,                      //now name becomes int
			age   = "here age is string",     //age is std::string now
			items = {"Shaan", "Udit", "Sonu"},//this turns into std::vector<std::string>
			child = cson                      //yet again
			(
				func = "Hi",
				child = "Bye"
			)
		)
	);
	
	x.func("Lata");  //invoke function
	
	std::cout << x.child.name << std::endl; //access child attribute to print it
	
	std::cout << x << std::endl;  //print the entire object
}

It is also possibe to treat the attributes as keys, so we can write as:

x[func]("Lata");  //invoke function
	
std::cout << x[child][name] << std::endl; //access child attribute to print it
	

And we can even mix them:

std::cout << x.child[name] << std::endl; //access child attribute to print it
std::cout << x[child].name << std::endl; //access child attribute to print it

All mean exactly same thing!

###Compilers

Tested with GCC 4.8.2 with -std=c++11.

###Suggestions

Suggestions are most welcome. Feel free to report issues, send pull requests or suggest improvements by sending email at sir_nawaz959@yahoo.com.