https://github.com/pb82/elson
C++11 JSON Parser/Serializer
https://github.com/pb82/elson
Last synced: about 1 year ago
JSON representation
C++11 JSON Parser/Serializer
- Host: GitHub
- URL: https://github.com/pb82/elson
- Owner: pb82
- License: mit
- Created: 2013-04-23T20:44:43.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2014-03-12T20:12:58.000Z (over 12 years ago)
- Last Synced: 2023-03-24T11:11:51.638Z (over 3 years ago)
- Language: C++
- Size: 332 KB
- Stars: 5
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Elson [](https://travis-ci.org/pb82/Elson)
=====
A C++11 JSON Parser/Serializer that aims to make the usage of JSON in C++
as natural and simple as possible. No dependencies (other than stl). Header only.
Tested with g++ 4.6, clang 3.1 (via Travis CI) and g++ 4.7.2
License: MIT
Contributions welcome!
Writing JSON
-------------
```c++
#include
#include "Elson.hpp"
int main() {
using namespace JSON;
Value val;
val["Name"] = "Homer";
val["Family"]["Wife"] = "Marge";
val["Family"]["Kids"] = { "Bart", "Lisa", "Maggie" };
val["Age"] = 40;
PrettyPrinter printer;
std::cout << printer.print(val) << std::endl;
return 0;
}
```
Since a JSON::Object is just a typedef'd std::map you may also use map's
initializer list constructor and write:
```c++
#include
#include "Elson.hpp"
int main() {
using namespace JSON;
Value val = Object {
{ "Name", "Homer" },
{ "Family", Object {
{ "Wife", "Marge" },
{ "Kids", { "Bart", "Lisa", "Maggie" } }
}
},
{ "Age", 40 }
};
PrettyPrinter printer;
std::cout << printer.print(val) << std::endl;
return 0;
}
```
Almost JSON Literals. Nested Arrays may be written as:
```c++
Value val = { {1,0,0}, {0,1,0}, {0,0,1} };
```
Reading JSON
-------------
```c++
#include
#include "Elson.hpp"
int main() {
using namespace JSON;
Value val;
Parser p;
p.parse(val, "{\"Answer\": \"42\"}");
double answer = val["Answer"].as();
std::cout << answer << std::endl;
return 0;
}
```