GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Categories
  • Newsletter
  • Submit
  • Login
  • Awesome C++
  • JSON

This view

  • Original page
  • New address

GeezerVison is showing cpp.libhunt.com — jbson Alternatives - C++ JSON | LibHunt

  • Subscribe
  • Submit
  • Categories
  • About
  • Login

jbson

JSON & BSON parser/writer

chrismanning logo
chrismanning.github.io    Source Code    Changelog
Suggest Changes
Take the C++ Developer Survey - About 6 minutes on the tools, libraries and AI you use with C++. You get the results by email. Take the survey »
Popularity
1.8
Stable
Activity
0.0
Stable
Stars 38
Watchers 4
Forks 5
Last Commit almost 11 years ago

Code Quality Rank: L1
Programming language: C++
License: gtkbook License
Tags: JSON    

jbson alternatives and similar libraries

Based on the "JSON" category.
Alternatively, view jbson alternatives based on common mentions on social networks and blogs.

  • json

    9.9 9.8 L1 jbson VS json
    JSON for Modern C++
    nlohmann logo
  • RapidJSON

    9.5 0.0 L1 jbson VS RapidJSON
    A fast JSON parser/generator for C++ with both SAX/DOM style API
    Tencent logo
  • SaaSHub - Software Alternatives and Reviews

    SaaSHub helps you find the best software and product alternatives
    Promo www.saashub.com
    SaaSHub Logo
  • simdjson

    9.5 9.5 jbson VS simdjson
    Parsing gigabytes of JSON per second : used by Facebook/Meta Velox, the Node.js runtime, ClickHouse, WatermelonDB, Apache Doris, Milvus, StarRocks
    simdjson logo
  • cJSON

    9.4 3.9 L3 jbson VS cJSON
    Ultralightweight JSON parser in ANSI C
    DaveGamble logo
  • JsonCpp

    9.1 7.8 L1 jbson VS JsonCpp
    A C++ library for interacting with JSON.
    open-source-parsers logo
  • yaml-cpp

    8.7 8.1 jbson VS yaml-cpp
    A YAML parser and emitter in C++
    jbeder logo
  • ArduinoJson

    8.6 5.1 jbson VS ArduinoJson
    📟 JSON library for Arduino and embedded C++. Simple and efficient.
    bblanchon logo
  • JSMN

    7.8 0.0 L3 jbson VS JSMN
    Jsmn is a world fastest JSON parser/tokenizer. This is the official repo replacing the old one at Bitbucket
    zserge logo
  • json-c

    7.8 8.7 jbson VS json-c
    https://github.com/json-c/json-c is the official code repository for json-c. See the wiki for release tarballs for download. API docs at http://json-c.github.io/json-c/
    json-c logo
  • Jansson

    7.7 7.2 L2 jbson VS Jansson
    C library for encoding, decoding and manipulating JSON data
    akheron logo
  • json11

    7.5 0.6 L2 jbson VS json11
    DISCONTINUED. A tiny JSON library for C++11. [MIT]
  • YAJL

    7.0 0.0 L3 jbson VS YAJL
    A fast streaming JSON parsing library in C.
    lloyd logo
  • LIBUCL

    6.2 8.1 L2 jbson VS LIBUCL
    Universal configuration library parser
    vstakhov logo
  • json-parser

    6.0 5.3 L1 jbson VS json-parser
    Very low footprint DOM-style JSON parser written in portable ANSI C
    json-parser logo
  • PicoJSON

    5.8 0.0 L1 jbson VS PicoJSON
    a header-file-only, JSON parser serializer in C++
    kazuho logo
  • frozen

    5.1 2.6 L1 jbson VS frozen
    JSON parser and generator for C/C++ with scanf/printf like interface. Targeting embedded systems.
    cesanta logo
  • json.h

    4.8 7.8 jbson VS json.h
    🗄️ single header json parser for C and C++
    sheredom logo
  • JSON++

    4.7 0.0 L1 jbson VS JSON++
    DISCONTINUED. A JSON parser in C++
  • QJson

    3.9 1.9 L1 jbson VS QJson
    DISCONTINUED. QJson is a qt-based library that maps JSON data to QVariant objects.
    flavio logo
  • qt-json

    3.8 0.0 L3 jbson VS qt-json
    DISCONTINUED. A simple class for parsing JSON data into a QVariant hierarchy and vice versa.
    qt-json logo
  • libjson

    3.8 1.4 L3 jbson VS libjson
    a JSON parser and printer library in C. easy to integrate with any model.
    vincenthz logo
  • Boost.PropertyTree

    3.1 0.7 L1 jbson VS Boost.PropertyTree
    Boost.org property_tree module
    boostorg logo
  • json_dto

    2.9 7.4 jbson VS json_dto
    A small header-only library for converting data between json representation and c++ structs
    Stiffstream logo
  • JeayeSON

    2.8 0.0 L2 jbson VS JeayeSON
    A very sane (header only) C++14 JSON library
    jeaye logo
  • json-voorhees

    2.7 9.3 L3 jbson VS json-voorhees
    A killer modern C++ library for interacting with JSON.
    tgockel logo
  • StructMapping

    2.6 0.0 jbson VS StructMapping
    Mapping json to and from a c++ structure
    bk192077 logo
  • Jsonifier

    2.4 4.7 jbson VS Jsonifier
    A few classes for extremely fast json parsing/serializing in modern C++. Possibly the fastest json parser in C++. Possibly the fastest json serializer in C++.
    nihilai-collective logo
  • jute

    2.4 2.2 L3 jbson VS jute
    Very simple C++ JSON Parser
    amir-s logo
  • ujson

    2.3 - jbson VS ujson
    µjson is a a small, C++11, UTF-8, JSON library. [MIT]
  • iod.metajson

    2.0 0.0 jbson VS iod.metajson
    Non-intrusive, high performance C++17 lightweight JSON de/serializer
    iodcpp logo
  • jvar

    1.8 0.0 jbson VS jvar
    JS inspired Variants and JSON parsing for C++
    YasserAsmi logo

* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.

Do you think we are missing an alternative of jbson or a related project?

Add another 'JSON' Library

Popular Comparisons

  • jbson
    vs
    RapidJSON
  • jbson
    vs
    JsonCpp
  • jbson
    vs
    PicoJSON
  • jbson
    vs
    json
  • jbson
    vs
    ujson

README

jbson is a library for building & iterating BSON data, and JSON documents in C++14.

\tableofcontents

Features # {#features}

  • Header only. Boost licensed.
  • Depends only on Boost headers & C++14 standard library. note 3
  • Fully compatible with documented BSON specs.
  • A strict, iterator-based JSON parser. Mostly compliant with JSON standard. note 2
  • Parses MongoDB extended JSON from UTF-8,16,32 strings.
  • User-defined JSON literals ""_json_set, ""_json_doc, ""_json_arr.
  • An implementation of JSONPath, with some support for filtering with boolean expressions.

Usage # {#usage}

All types and functions are under the [jbson](@ref jbson) namespace.

Elements {#elements}

jbson::basic_element is the class through which elements' names & values are accessed and has a template parameter for its data container. Generally, this will be a non-owning range (e.g. boost::iterator_range), but there is an jbson::element alias to jbson::basic_element<std::vector<char>> to enforce data ownership. The names are always copied (may change in the future). Value data may or may not be, depending on the container template parameter.
The type of an element can be determined through comparison with the jbson::element_type enum class values.
Values can be accessed through the jbson::basic_element::value() member function or the jbson::get() free function.

    using jbson::element_type;
    jbson::element elem;
    //...
    if(elem.type() == element_type::string_element) {
        auto str = elem.value<std::string>();
        //OR
        auto str = jbson::get<std::string>(elem);
        //OR if container is contiguous
        auto str = elem.value<boost::string_ref>();
        //OR
        auto str = jbson::get<boost::string_ref>(elem);
        //OR returns either std::string or boost::string_ref, depending on container
        auto str = jbson::get<element_type::string_element>(elem);
    }

Elements can be modified using the value() member function.

    using jbson::element_type;
    jbson::element elem;
    elem.value("some string");
    assert(elem.type() == element_type::string_element);
    elem.value(123);
    assert(elem.type() == element_type::int32_element);
    elem.value<bool>(123);
    assert(elem.type() == element_type::boolean_element);
    elem.value<element_type::boolean_element>(123);
    assert(elem.type() == element_type::boolean_element);
    elem.value(element_type::boolean_element, 123);
    assert(elem.type() == element_type::boolean_element);

Elements can also be accessed via the visitor pattern.

    using namespace jbson;
    element elem;
    //...
    struct Visitor {
        template <typename T>
        void operator()(boost::string_ref name, element_type etype, T&& value) const {
            // do something
        }

        void operator()(boost::string_ref name, element_type etype) const {
            // for elements whose type cannot have a value
            // e.g. null_element, undefined_element
        }
    };
    elem.visit(Visitor{});

Visitors can also return values. It is not necessary to typedef a return_type.

    using namespace jbson;
    element elem;
    //...
    struct IsNull {
        template <typename T>
        bool operator()(boost::string_ref, element_type, T&&) const {
            return false;
        }
        bool operator()(boost::string_ref, element_type) const {
            return true;
        }
    };
    bool null = elem.visit(IsNull{});

Building Documents & Arrays {#building}

builder & array_builder are simple classes used for building documents & arrays, respectively. These allow for documents & arrays to be built from variables, rather than just literals.

    using namespace jbson;
    auto str = "str"s;
    int num = 123;
    // {
    //     "some string": "str",
    //     "some int": 123,
    //     "some obj": {
    //         "child bool": false
    //     }
    // }
    document doc = builder
        ("some string", element_type::string_element, str)
        ("some int", element_type::int32_element, num)
        ("some obj", element_type::document_element, builder
            ("child bool", element_type::boolean_element, false)
        );

While the intent of this is clear, it's a little verbose due to the explicit type. This can be omitted when the type is compatible with a JSON type. The above example becomes:

    using namespace jbson;
    auto str = "str"s;
    int num = 123;
    // {
    //     "some string": "str",
    //     "some int": 123,
    //     "some obj": {
    //         "child bool": false
    //     }
    // }
    document doc = builder
        ("some string", str)
        ("some int", num)
        ("some obj", builder
            ("child bool", false)
        );

That's about as good as it's going to get. array_builder is used the same way, apart from the lack of a name parameter, which is an implicitly set integer.

    using namespace jbson;
    auto str = "str"s;
    int num = 123;
    // ["str", 123, { "child bool": false }]
    array arr = array_builder
        (str)
        (num)
        (builder
            ("child bool", false)
        );

Documents {#docs}

The main classes of jbson are the templates jbson::basic_document, jbson::basic_array & jbson::basic_element. These types are parameterised with their underlying container or range. It's generally recommended to use a random-access range. Their default aliases i.e. document & element are parameterised with std::vector<char>. The second template parameter of basic_document defines the parameter of child basic_elements. By default this is defined as a boost::iterator_range to avoid copying data, though care should be taken that this element does not outlive the container/document which owns the data, else it can be copied to its own jbson::element.

    jbson::document doc;
    //...
    for(auto&& element: doc) {
        //...
    }
    std::for_each(doc.begin(), doc.end(), [](auto&& v) { something(v); });

basic_array is implemented using basic_document and the classes can usually be interchanged freely. Of course, basic_document is for documents and basic_array is for arrays and should be used as such.

basic_document is simply a constant wrapper around its BSON data container, allowing iteration of elements in the form of a basic_element. It can be used with C++11's range-based for loop, and non-modifying standard algorithms.
Iterators retain a copy of elements. This does not mean that an iterator contains a copy of an element's data, however.

Document Set {#doc_set}

basic_document_set is an alias template to std::set<basic_element,...> which basic_document and basic_array are explicitly convertible to. This can be used to easily modify an existing document, at the cost of copying/converting the entire document into its constituent elements. The type parameter of basic_document_set should be a container not a range, i.e. it should own the data. The default alias document_set is basic_document_set<std::vector<char>> making it a set of basic_element<std::vector<char>>.

    jbson::document doc;
    //...
    jbson::document_set set(doc);
    //... modify set
    doc = set; // convert back

JSON Parsing {#json}

JSON documents can be parse with the json_reader class, which parses directly to BSON data and can be implicitly converted to any valid basic_document, basic_array or basic_document_set. It can also be move-converted to a document or array for efficient conversion.

    jbson::json_reader reader{};
    reader.parse("{\"some json\": 123 }");
    reader.parse(R"({"some json": 123 })"); // C++11 raw string
    auto doc = jbson::document(std::move(reader));

json_reader can parse string literals, or any range of UTF-(8, 16 or 32) characters e.g. std::string, boost::string_ref, boost::iterator_range<...>, QByteArray, etc. To parse UTF-16 or UTF-32 documents, json_reader must be passed a range of (or iterators to) char16_t or char32_t, respectively. It is assumed the input code units use the system endian, and a BOM (byte-order mark) at the beginning of input is an error. json_reader may also work with wchar_t, though this usage is non-standard, non-portable and unsupported.
For convenience, user-defined literals have been implemented for JSON documents.

    using jbson::literal;
    auto doc = R"({
        "some json": 123
    })"_json_doc;

This is equivalent to the above example.

Performance {#performance}

Performance of the JSON parser is decent, as measured by json_benchmark, it's 2nd only to rapidjson (or 3rd to QJsonDocument with small input):

QJsonDocument.small: 1000 iterations of 500 parsings in 1.04621 to 1.05643 sec based on 2 benchmarks
QJsonDocument.large: 1000 iterations of 1 parsings in 58.1027 to 58.1235 sec based on 2 benchmarks

jsoncpp.small: 1000 iterations of 500 parsings in 2.4124 to 2.45696 sec based on 2 benchmarks
jsoncpp.large: 1000 iterations of 1 parsings in 62.7781 to 62.7823 sec based on 2 benchmarks

rapidjson.small: 1000 iterations of 500 parsings in 0.454047 to 0.48559 sec based on 2 benchmarks
rapidjson.large: 1000 iterations of 1 parsings in 4.8566 to 4.86322 sec based on 2 benchmarks

jbson.small: 1000 iterations of 500 parsings in 1.24328 to 1.27775 sec based on 2 benchmarks
jbson.large: 1000 iterations of 1 parsings in 43.0072 to 43.0377 sec based on 2 benchmarks

jsoncons.small: 1000 iterations of 500 parsings in 10.2386 to 10.2747 sec based on 2 benchmarks
jsoncons.large: 1000 iterations of 1 parsings in 243.634 to 243.796 sec based on 2 benchmarks

Some more measurements using https://github.com/sanSS/json-bechmarks.

Small Objects

Parser Average Time (us) Memory Usage
jsoncpp 9411 4,084 K
QJsonDocument 7009 3,684 K
jbson 5981 1,132 K

Medium Objects

Parser Average Time (us) Memory Usage
jsoncpp 121035 44,224 K
QJsonDocument 82839 37,996 K
jbson 64800 24,708 K

Large Objects

Parser Average Time (us) Memory Usage
jsoncpp 438745 131,204 K
QJsonDocument DNF N/A
jbson 190883 70,7748 K

Notes {#notes}

  1. jbson uses BSON-like terminology. Eg. a document is not only the root object, but also all embedded objects.
  2. BSON is incompatible with JSON in that element names cannot contain '\0', making names not fully UTF-8 compliant, which JSON requires.
  3. Requires codecvt header (C++11, missing in gcc-4.8 & gcc-4.9 stdlib), various template aliases (C++14), probably more where gcc's stdlib is behind on the standard.
    Known only to work with an up-to-date libc++ currently.


*Note that all licence references and agreements mentioned in the jbson README section above are relevant to that project's source code only.

Do not miss the trending, libraries, news and articles with our weekly report.

Awesome C++ is part of the LibHunt network. Terms. Privacy Policy.

(CC)
BY-SA
Spin The Wheel Of Names for a cryptographically secure random name picker.
Schedule a when to meet poll.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.