Skip to content

DreamPast/efjson-cpp

Repository files navigation

efjson: A Streaming and Event-driven JSON Parser

English 简体中文

Other programming languages:

Features

  • no extra dependencies
  • stream parser only depends on C89 and 8-bit byte
  • supports JSON5 and JSONC
  • stream parser requires minimal memory when no events are triggered

Installation

Stream Parser (C89)

Add efjson_stream.h to your project, and define EFJSON_STREAM_IMPL to implement the library.

C++20

Add efjson_stream.h and efjson.hpp to your project.

Example

Stream Parsing (C89)

#include <stdio.h>
#define EFJSON_STREAM_IMPL
#include "efjson_stream.h"
static const char src[] =
  "{\n\
\"null\":null,\"true\":true,\"false\":false,\n\
\"string\":\"string,\\\"escape\\\",\\uD83D\\uDE00\",\n\
\"integer\":12,\"negative\":-12,\"fraction\":12.34,\"exponent\":1.234e2,\n\
\"array\":[\"1st element\",{\"object\":\"nesting\"}],\n\
\"object\":{\"1st\":[],\"2st\":{}}\n\
}";
int main(void) {
  efjsonStreamParser parser;
  unsigned i, n = sizeof(src) / sizeof(src[0]);
  efjsonToken token;

  efjsonStreamParser_init(&parser, 0);
  for(i = 0; i < n; ++i) {
    printf(
      "%2lu:%-2lu(%3lu)%-8s  ", efjson_cast(unsigned long, efjsonStreamParser_getLine(&parser)),
      efjson_cast(unsigned long, efjsonStreamParser_getColumn(&parser)),
      efjson_cast(unsigned long, efjsonStreamParser_getPosition(&parser)),
      efjson_stringifyLocation(efjsonStreamParser_getLocation(&parser))
    );
    token = efjsonStreamParser_feedOne(&parser, efjson_cast(unsigned char, src[i]));
    if(token.type == efjsonType_ERROR) {
      printf("%s\n", efjson_stringifyError(efjson_cast(efjsonUint8, token.extra)));
      return 1;
    } else {
      printf("%-30s %u%c", efjson_stringifyType(token.type), token.index, token.done ? '*' : ' ');
      if(token.done) {
        if(token.type == efjsonType_STRING_ESCAPE || token.type == efjsonType_STRING_ESCAPE_UNICODE
           || token.type == efjsonType_STRING_ESCAPE_HEX || token.type == efjsonType_IDENTIFIER_ESCAPE) {
          printf(" (U+%05lx)", efjson_cast(unsigned long, token.extra));
        }
      }
      printf("\n");
    }
  }
  return 0;
}

Stream Parsing (C++20)

#include "efjson.hpp"
int main() {
  static const char8_t src[] =
    u8"{\
\"null\":null,\"true\":true,\"false\":false,\
\"string\":\"string,\\\"escape\\\",\\uD83D\\uDE00😊\",\
\"integer\":12,\"negative\":-12,\"fraction\":12.34,\"exponent\":1.234e2,\
\"array\":[\"1st element\",{\"object\":\"nesting\"}],\
\"object\":{\"1st\":[],\"2st\":{}}\
}";
  efjson::StreamParser parser;
  for(auto& token: parser.feed(src)) std::cout << token.toDebugString() << '\n';
  return 0;
}

References

JSON Specification: RFC 4627 on JSON

JSON State Diagram: JSON

JSON5 Specification: The JSON5 Data Interchange Format

JSON Pointer: JavaScript Object Notation (JSON) Pointer

Relative JSON Pointers: Relative JSON Pointers

Test data

JSON_checker

json5-tests

Native JSON Benchmark

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages