Giter Club home page Giter Club logo

simpleson's Introduction

Build status

simpleson

Lightweight C++ JSON parser & serializer that is C++98 compatible with no dependencies

Why simpleson?

Simpleson is built under the following requirements:

  • One header and one source file only
  • No external dependencies
  • ISO/IEC 14882:1998 (aka C++98) compatible
  • Cross-platform

A primary use case for simpleson is in an memory-constrained embedded system.

Building simpleson

Simpleson was intentionally built such that a developer could simply copy json.h into the target project's inc folder, copy json.cpp into the src folder, and then compile the target project. No linking -> no drama.

Building the library and tests follows the standard build chain via CMake:

mkdir build
cd build
cmake ../
make

Unit tests can then be run by executing make test

Quickstart

// Create the input
std::string input = "{ \"hello\": \"world\" }";

// Parse the input
json::jobject result = json::jobject::parse(input);

// Get a value
std::string value = (std::string)result.get_entry("hello").value

// Add entry
json::key_value_pair item;
item.key = "new_key";
item.value = json::jvalue(123.4);
result.push_back(item);

// Serialize the new object
std::string serial = (std::string)result;

Using simpleson

The namespace of simpleson is simply json. JSON objects can be parsed by calling json::jobject::parse(), which returns a jobject. The jobject class is simply an extension of a vector of key-value pairs. Useful methods of jobject include:

  • has_key("key")
  • get_keys()
  • get_entry("key")

An instance of jobject can be searlized by casting it to a std::string. Note that an instance of jobject does not retain it's original formatting (it drops tabs, spaces outside strings, and newlines).

You can build your own jobject from scratch by creating a fresh instance of jobject and then pushing (push_back) key-value pairs into it.

A note on booleans

Booleans are handled a bit differently than other data types. Since everything can be cast to a boolean, having an implicit boolean operator meant everything goes to a boolean! Instead, jvalue objects of type jbool are created using the jvalue::jbool(const bool) static method. If you do not use this method and instead directly create/assign a boolean to a jvalue object, then the boolean will be cast to a jnumber type with a value of 0 or 1.

Key-value pairs

A key-value pair is just that: a key and a value; the members key and value of json::key_value_pair are public. The key member is simply the string of the key whie the value member is a jvalue instance. A jvalue instance can be casted to int, long, double, float, std::string, and json::jarray depending on the type of value it holds.

You can check the type of value by calling get_type() on the value.

You can deserialze a nested object by calling json::jobject::parse(parent.get_entry("child").value).

Arrays

Simpleson includes a class called jarray, which is an extension of a vector of strings. You can create an instance of jarray by parsing a string via json::jarray::parse(input). An instance of jarray can also be casted to std::vector<int>, std::vector<long>, std::vector<double>, and std::vector<float>.

simpleson's People

Contributors

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