Giter VIP home page Giter VIP logo

fastjson's Introduction

FastJson

FastJson is a library for reading and writing json in C++. Its designed to be fast and light, yet complete.

How to use FastJson

FastJson has an odd but functional interface. We will just look at the high-level interface here.

First FastJson uses two constructs Tokens and Chunks. A Token is like a node in the JSON, however the Token does not own any of the memory that it references. The owner of this memory is (usually) a Chunk.

So parsing a json string looks like this:

fastjson::Token token;
fastjson::dom::Chunk chunk;
std::string error_message;
if( ! fastjson::dom::parse_string("{}", &token, &chunk, &error_message) )
{
  std::cerr<<"ERROR : "<<error_message<<std::endl;
  return;
}

Writing one back to a string is even easier

std::string s = fastjson::as_string( token );

Manipulating or interogating the JSON tree is a little uglier - for this there are some helper classes in the dom namespace

fastjson::dom::Dictionary dict = fastjson::dom::Dictionary::as_dict( &token, &chunk );
//Get the id field
int id;
if( dict.get<int>("an_id", &id) )
{
  std::cerr<<"NO id found"<<std::endl;
  return;
}
double f = id+0.5;
dict.add<double>( "something", &f );

To properly navigate a deeper tree you need to use some of the lower-level constructs, covered in the next section. Some of this will be hidden when I work out the neatest way.

FastJson under the covers

First some things to note:

  1. FastJson uses an analyse then allocate approach, meaning that while it reads over the json twice, it only allocates its data in large blocks. This makes it fast.
  2. FastJson values are stored as strings with a type hint. This means large numbers can be read easily without fastjson caring about precission or length, this is the users concern.
  3. Since we allocate in big chunks, we need something to do our memory management for us .. thats where the Chunk objects come in.

Now some words about tokens. A token is just a discriminated union type. It can either be an ArrayType, DictType, ValueType, or one of the Litterals (Null, False, True). Most of these types contain further pointers to data or more tokens.

This means traversing an Array in json looks like this:

Token token = ...
if( token.type != Token::ArrayToken ) { ... }
ArrayEntry * child = token.array.ptr;
while(child)
{
   ...
   child = child->next;
}

Traversing a dictionary is similar.

fastjson's People

Contributors

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