Giter VIP home page Giter VIP logo

jeayeson's Introduction

JeayeSON - a very sane C++14 JSON library Build Status

JeayeSON was designed out of frustration that there aren't many template-based approaches to handling JSON in modern C++. Given a very limited number of types (that JSON offers), functions can be written in a generic manner to provide the most consistent interface, no matter the type. JeayeSON is a non-validating JSON library; it expects valid JSON all of the time.

  • Header only (easy to add to any project)
  • Small, consistent C++ API
  • Typesafe, C++14 interface
  • Absolutely no macros needed nor used
  • UTF-8 (with UTF-16 transcoding)
  • Extensive test suite (using jest, a "sane and minimal C++14 unit test framework")

Practice

Assuming the JeayeSON headers are in your search path, simply

#include <jeayeson/jeayeson.hpp>

NOTE: If you're on Windows and symbolic links don't work, use

#include <jeayeson/value.hpp>

This will give you access to the following types:

json_value    /* variant type */
json_map      /* string->json_value map */
json_array    /* contiguous array of json_values */
json_null     /* json_value's default state */
json_int      /* defaults to int64_t */
json_float    /* defaults to double */
json_file     /* aggregate type representing a filename */
json_data     /* aggregate type representing json data as a string */

Building JSON

Assume we want to create this JSON:

{
  "hello": "world",
  "arr":
  [ 1.1, 2.2, 3.3 ],
  "person":
  {
    "name": "Tom",
    "age": 36,
    "weapon": null
  }
}

We can simply do:

json_value val // json_value can be any json type; here, it's a json_map
{
  { "hello", "world" }, // nested pairs represent key/value
  {
    "arr",
    { 1.1, 2.2, 3.3 } // arrays are easy
  },
  {
    "person",
    {
      { "name", "Tom" },
      { "age", 36 },
      { "weapon", nullptr } // can also use json_null
    }
  }
};

Or, if we wanted to build it piece by piece:

json_map val; // explicitly make a map this time

val["hello"] = "world"; // simple assignments work on all compatible types

val["arr"] = { 1.1, 2.2, 3.3 }; // implicit array construction

json_map person; // we'll build the person separately, too
person["name"] = "Tom";
person["age"] = 36;
person["weapon"] = nullptr;

val["person"] = person; // now we can just put the person map into val

// we can even dig into nested maps and arrays using op[]
val["person"]["name"] = "Susan";

Reading a JSON string

std::string json; // Acquired/initialized elsewhere
json_array arr{ json_data{ json } }; // simple aggregate for type-safety

Reading a file

json_map map{ json_file{ "my_file.json" } }; // simple aggregate for type-safety

Writing JSON

// maps, arrays, and values can all be used with streams
json_map map{ json_file{ "my_file.json" } };
std::cout << map << std::endl;

// you can also just write them to a string
std::string const json{ map.to_string() };

Feels like the C++ stdlib

You'll find all the normal stdlib-like functions, including iterator support.

json_array json;
json.push_back("string");
json.push_back(3.14159);
json.push_back(false);

for(auto const &j : json) // has begin, end, cbegin, cend, etc
{ std::cout << j << std::endl; }

// works like an std::vector or std::map
json.size();
json.empty();
json.clear();
json.reserve(42);

Type checking and casting

json_map json
{
  { "boolean", false },
  { "str", "..." }
};

// check types with is<T>()
json["boolean"].is<json_value::type::boolean>(); // true
json["str"].is<json_value::type::string>(); // true

// query the type enum
auto const type(json["str"].get_type());

// cast with as<T>() to get the complete type
auto const str(json["str"].as<json_string>());

Installation

The ./configure script must be used at least once to automagically generate jeayeson/config.hpp (see Customization). Since JeayeSON is a header-only library, simply copy over the contents of include to your project, or, better yet, add JeayeSON as a submodule and introduce jeayeson/include to your header search paths

A full installation can also be achieved by using ./configure && make install. See the ./configure script for prefix options.

Dependencies

  • A C++14 compiler (GCC 5.x or Clang 3.8+ recommended)
  • Boost (1.55.0+ recommended)

Customization

NOTE: All configuration is easily done in jeayeson/config.hpp, which is generated when you run ./configure (and is not overwritten subsequently -- delete it to reset).

Customization can be achieved by adjusting the types in the jeayeson::config struct template. A specialization is already provided, which contains the default types used by JeayeSON. Feel free to change the types to any other, still compatible, types.

For example, you may want the json integer type to be 32bit instead of the default 64bit. Or, you may want to use std::unordered_map instead of std::map.

Building tests

NOTE: You don't actually have to build JeayeSON, since it's a header-only library. This build process is only for the tests.

In the project directory, run:

$ ./configure && make

# Depending on your compiler setup (gcc or clang, linux or osx, etc)
# you may need to specify some extra flags. An example case:
# (allows clang & libc++ to work on Arch Linux)
$ CXX=clang++ CXX_FLAGS=-stdlib=libc++ LD_LIBS=-lc++abi make

Once built, you can run the tests:

$ make test

The tests (in test/src and test/include) can give more examples on how to use JeayeSON.

jeayeson's People

Contributors

cengiz-io avatar cogwheel avatar jeaye avatar kuhar avatar tzhuan avatar xu-cheng avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jeayeson's Issues

JSON is always preparsed

Allow for the specification of when JSON is parsed (sync vs async 'lazy'). This would be nice as an added template param of json_map and json_array.

json_map<json_parse::async> my_map; // maybe something like this? <-- Requires C++11 (how could it be done without it?)

Perhaps
json_map json_map_sync json_array json_array_sync (or similar?)

Functions in json_map and json_array would then be specialized on those types. Meaning 4 new files (map_sync, map_async, array_sync, array_async). The problem with this is the duplication of code.

Further note:
How would json_map::size() or similar work on an async object?

Formatting output?

Hi,

Is it possible to get a formatted output with some indentation?
I've tried to dump a json file, but it gets dumped in a single line, which isn't readable.

`make install` does not copy `details` directory

Hi there,

include/details directory is not copied to target installation directory.

Thus, header resolving fails:

$ clang++ -std=c++14  json_test.cpp -o json_test -IROOT/include/ 
In file included from json_test.cpp:1:
ROOT/include/jeayeson/jeayeson.hpp:14:10: fatal error: 'detail/traits.hpp' file not found
#include "detail/traits.hpp"
        ^
1 error generated.

If I manually copy details directory under include/jeayeson, compilation succeeds.

Have a nice day

No mention of Boost dependencies in README

There's no mention of the Boost dependencies anywhere in the project that I could find, except for the include directives that unfortunately brought my program to a halt when I tried to use jeayeson. While it would be great to not have these dependencies (feature request?), these should at least be prominently documented. A lot of users won't have or want to deal with Boost, and others will at least need to know how to install is required by this project.

codecvt missing in libstdc++

When compiling with gcc-4.9 and using libstdc++ following error occurs:
`In file included from include/jeayeson/detail/parser_util.hpp:20:
include/jeayeson/detail/utf.hpp:12:10: fatal error: 'codecvt' file not found

include `

How about using boost equivalent?

Add an ODR test

As part of the upcoming test refactoring, we need an ODR test.

Memes in examples

These memes are bad examples
"Hello, This is Dog"

Please use a better meme, next time you won't trigger me?

Current escaped strings are incomplete

Currently we only escape character ", but with further thinking, we also need to escape all strings which start with \, e.g. \\, \n ..., in both reading and writing.

Escaped strings

JeayeSON needs to properly handle escaped strings within keys and string-based values. This applies both when readying and writing JSON.

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.