neujson

JSON parser/generator in C++17

Quick Started GitHub open in new window

Simple、Fast

neujson contains only header files, does not rely on Boost, and takes full advantage of the template's features. You can "assemble" neujson's Handler at will.

API simplicity

neujson supports both DOM and SAX-style API, where SAX allows custom handlers for streaming processing.

Unicode friendly

neujson fully supports UTF-8 codec and can parse utF-8 characters such as '\u0000'

Multiple input/output streams

neujson has built-in string input/output streams and file input/output streams, and make full use of memory buffers to improve read and write speed

Std Streams Wrapper

neujson provides official wrappers for std::istream and std::ostream, can be combined with neujson's built-in input/output streams

Performant

neujson internally implements standards such as IEEE754 and uses high-performance algorithms built into different compilers. neujson also plans to implement Grisu2 algorithm and use SIMD instructions to speed up conversion between floating point numbers and strings.

Usage at a glance

This simple example parses a JSON string into a document (DOM), make a simple modification of the DOM, and finally stringify the DOM to a JSON string.

#include <cstdio>

#include "neujson/document.h"
#include "neujson/writer.h"
#include "neujson/string_write_stream.h"
#include "../sample.h"

int main() {
  // 1. Parse a JSON string into DOM.
  neujson::Document doc;
  auto err = doc.parse(kSample[0]);
  if (err != neujson::error::PARSE_OK) {
    puts(neujson::parseErrorStr(err));
    return EXIT_FAILURE;
  }

  // 2. Modify it by DOM.
  auto &s = doc[0]["Longitude"];
  s.SetDouble(s.GetDouble() + 100.0);

  // 3. Stringify the DOM
  neujson::StringWriteStream os;
  neujson::Writer<neujson::StringWriteStream> writer(os);
  doc.WriteTo(writer);

  // Output
  fprintf(stdout, "%.*s", static_cast<int>(os.get().length()), os.get().data());
  return 0;
}

Output:

[{"precision":"zip","Latitude":37.766800000000003,"Longitude":-22.395899999999997,"Address":"","City":"SAN FRANCISCO","State":"CA","Zip":"94107","Country":"US"},{"precision":"zip","Latitude":37.371991000000001,"Longitude":-122.02602,"Address":"","City":"SUNNYVALE","State":"CA","Zip":"94085","Country":"US"}]