https://github.com/mpark/jpark
https://github.com/mpark/jpark
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/mpark/jpark
- Owner: mpark
- Created: 2015-09-08T03:42:48.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2015-09-08T17:21:48.000Z (almost 11 years ago)
- Last Synced: 2025-01-20T16:28:21.228Z (over 1 year ago)
- Language: C++
- Size: 148 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# JPark
## Quick Start
This project includes git submodules, use the `--recursive` flag when cloning.
```bash
git clone --recursive git@github.com:mpark/jpark.git
```
## Major Concepts
### Static JSON Schema
The following are the currently supported JSON schemas:
* `json::boolean`
* `json::number`
* `json::string`
* `json::array()` e.g. `json::array(json::number)`
* `json::object(json::field(, , ), ...)`
e.g. `json::object(json::field("Name", &Person::name, json::string))`
### Apply the schema to an instance
All of the schemas mentioned above has an `operator()` which projects the schema
onto an instance. For example, we can apply the `json::boolean` onto `true`:
`std::cout << json::boolean(true) // prints: true`
## Example
```cpp
#include
#include
#include
struct Person {
std::string first_name;
std::string last_name;
double height;
std::vector friends;
};
int main() {
Person person{"J", "Park", 180, {"Moris"}};
// Only extract the first/last name.
auto name = json::object(
json::field("first name", &Person::first_name, json::string),
json::field("last name", &Person::last_name, json::string)
);
// {"first name":"J","last name":"Park"}
std::cout << name(person) << std::endl;
// Full info.
auto full = json::object(
json::field("given name", &Person::first_name, json::string),
json::field("surname", &Person::last_name, json::string),
json::field("height", &Person::height, json::number),
json::field("friends", &Person::friends, json::array(json::string))
);
// {"given name":"J","surname":"Park","height":180,"friends":["Moris"]}
std::cout << full(person) << std::endl;
}
```
## Requirements
This library requires a standard conformant __C++11__ compiler.
The following configurations were successfully tested:
* __Mac OS X Yosemite__: Clang 3.5, 3.6, _Apple_ (with libc++)