A high performance C++14 library for effortlessly reading and writing UBJSON

UbjsonCpp

A high performance C++14 library for effortlessly reading and writing UBJSON

This library implements UBJSON Draft 12 and Value semmantics

About

Please see [UBJSON Official Website][ubjson_website]

About This Library

UjsonCpp is a UBJSON Library that enables a client to seamlessly stream In/Out UBJ encoded data from a StreamSource. The StreamSource is a template type that can be substituted provided it meets the Concept requirements. For example, std::ostream, std::istream and Boost.Asio can seamless be plugged in.

This library has been implemented using C++14, as part of the timl (Timothy's Libraries) for public use.

The Value type is an intuitive union type that is used to hold any UBJSON type, which includes int, char, strings, floats, binary, etc. See documentation for details

Usage Snippet

using namespace ubjson;
Value v1, v2;

v1 = 34535;     //Assign to integral types
v1 = "I so much love C++14";    //std::string constructable
v2 = v1;

std::string s1 = std::move(v1); //move string
//very viable for large amounts of strings


auto b = static_cast<std::string>(v2)   //safely cast to std::string or throw;
v2 = -2342.2525236;

std::string s2 = v2.asString();   //Value::asString() exception safe unconditional cast
std::cout << v2.asInt() << std::endl;   //unconditionally cast to int (noexcept)....

Modifiying Values in place? ...Yes

using namespace ubjson;
Value val = "Are we there yet Mr. Donald Knuth?";
std::string& ref1 = val;
auto& ref2 = static_cast<std::string&>(val);

//To lower
ref1[0] = std::tolower(ref2[0]);

auto boool = (ref1 == ref2) and (ref1 == val); //Will be true

Key-Value pairs or Maps? ...perfect

using namespace ubjson;
Value m1("country", "Nigeria");
Value m2;
m2["country"] = "Nigeria";
m2["location"]["latitude"]["relative"] = 34.2523; //creates the maps on the fly... fast

m1 == m2;   //Compare an Value type;

Arrays? ...No problems

using namespace ubjson;
Value array = {"Timothy", 2015, -34253535.235235, '@', Value("country", "Nigeria")};
array.push_back("Amazing Array");

for(auto val : array)
  std::cout << val.asString() << std::endl;

if(array.contains(2015))
  array.remove(2015)

Value also has binary types:

using namespace ubjson;
using Binary = Value::BinaryType;   //Actually, an alias for std::vector<unsigned char>
Value binary = Binary({0xF3, 0x33, 0x76, 0xAA, 0x23});

Stream Operations

Reading from a Stream is very simple.

  std::ifstream input;
  input.open("sample1.ubj", ios::binary);

  StreamReader<decltype(input)> reader(input);
  Value val = reader.getNextValue();

  //or
  StreamReader<std::ifstream> reader(input); ///your choice :-)

Writing to a Stream is likewise very simple.

  Value planet;
  planet["name"] = "Earth";
  planet["position"] = 3;

  std::ofstream output;
  output.open("sample2.ubj", ios::binary);

  StreamWriter<std::ostream> writer(output);
  auto result = writer.writeValue(planet);

  if(result.second)
    std::cout << "Successfully wrote: " << result.first << " bytes" << std::endl;

Pretty Printing.... easy (always outputs a valid json document):

Value value;
value["name"] = "Timothy"
value["country"] = "Nigeria";
value["favorites"] = { 34.351, -253, '@', value["country"], 34, value};

std::cout << to_ostream(value) << std::endl;
// or 
std::cout << to_ostream(value, to_ostream::pretty) << std::endl;

// for compact printing use
std::cout << to_ostream(value, to_ostream::compact) << std::endl;

Output of the first to_ostream above is as shown below:

{
    "name" : "Timothy",
    "country" : "Nigeria",
    "favorites" : [ 34.351, -253, "@", "Nigeria", 34,
        {
            "name" : "Timothy",
            "country" : "Nigeria"
        }]
}

Current Status

  • High Precision numbers haven't been implemented for Value semmantics - slated for 28th Aug, 2018
  • StreamReader and StreamWriter cannot cater for HighPrecision Numbers, and Binary - beta, slated for 20th Aug, 2018
  • StreamReader already handles Strongly typed containers, but StreamWriter is yet to do so - 20th Aug, 2018
  • The requirements for StreamType isn't well defined, yet. - slated for - 25th Aug, 2018

Written and authored by Ibrahim Timothy Onogu. [ubjson_website]: http://ubjson.org

Owner
Ibrahim Timothy Onogu
I am a Mechanical Engineer, a Software Engineer and a C++ fanatic. My interests are in Mission Critical, performance and scalable Systems.
Ibrahim Timothy Onogu
Similar Resources

A C++11 ASN.1 BER Encoding and Decoding Library

fast_ber A performant ASN.1 BER encoding and decoding library written in C++11 Introduction fast_ber is a small, lightweight library for BER encoding

Dec 1, 2022

SimpleBaseLib4CPP is a simple C++11 Base Encoding library that provides at the moment support for encoding and decoding various bases such as Base16, Base32 (various variants), Base58 (various variants), Base64 (various variants).

SimpleBaseLib4CPP SimpleBaseLib4CPP is a simple C++11 Base Encoding library that provides at the moment support for encoding and decoding various base

Nov 29, 2021

Your binary serialization library

Bitsery Header only C++ binary serialization library. It is designed around the networking requirements for real-time data delivery, especially for ga

Dec 1, 2022

A C++11 library for serialization

A C++11 library for serialization

cereal - A C++11 library for serialization cereal is a header-only C++11 serialization library. cereal takes arbitrary data types and reversibly turns

Dec 2, 2022

FlatBuffers: Memory Efficient Serialization Library

FlatBuffers FlatBuffers is a cross platform serialization library architected for maximum memory efficiency. It allows you to directly access serializ

Nov 27, 2022

Simple C++ 20 Serialization Library that works out of the box with aggregate types!

BinaryLove3 Simple C++ 20 Serialization Library that works out of the box with aggregate types! Requirements BinaryLove3 is a c++20 only library.

Sep 2, 2022

A C++ library for interacting with JSON.

JsonCpp JSON is a lightweight data-interchange format. It can represent numbers, strings, ordered sequences of values, and collections of name/value p

Dec 4, 2022

A header only C++11 library for parsing TOML

tinytoml A header only C++11 library for parsing TOML. This parser is based on TOML v0.4.0. This library is distributed under simplified BSD License.

Oct 10, 2022
Comments
  • rvalue reference qualified conversion operator induces a potential bug

    rvalue reference qualified conversion operator induces a potential bug

    Given this code

    using namespace ubjson;
        Value m;
        m = "Hello UBJSON"
        static_cast<std::string>(std::move(m))
        //or
        //static_cast<std::string&&)(m)
    
        ASSERT(m.type() == Type::String); //this assertion fires
    

    Given, that no object recieved the rvalue conversion, this object is still a string

  • Problems with unsigned int and unsigned long

    Problems with unsigned int and unsigned long

  • Add support for strongly typed arrays.

    Add support for strongly typed arrays.

    The support for homogeneous arrays is missing. see StreamWriter<StreamType>::append_array

        //! \todo TODO... detect homogeneous arrays and treat accordingly
        //! WORK in progress
    

    I think you made this difficult because you got the binary type wrong. I.E there is not one binary type, but all the strong typed arrays are different binary types. One should be able to write something like:

    std::vector<int> ints = foo();
    ubjson::Value value;
    value["ints"] = ints;
    

    and that should serialize into

    [{][i][4][ints][[][$][I][#][i][5][1][2][3][4][5]
    

    Note the [$][I]. The type of the array should be kept as passed in so that it is received that way.

  • This badly needs binary support for the streams

    This badly needs binary support for the streams

    Binary read+write support. How many days is this out? If I don't have it soon I may have to write it myself

    //else if(v.isBinary()) // k = append_binary(v); //currently not supported

High-performance specialized replacements for PHP's pack() and unpack() functions

ext-encoding High-performance specialized replacements for PHP's pack() and unpack() functions Under a profiler, it becomes obvious that PHP's pack()

Sep 17, 2022
Use to copy a file from an NTFS partitioned volume by reading the raw volume and parsing the NTFS structures.

ntfsDump Use to copy a file from an NTFS partitioned volume by reading the raw volume and parsing the NTFS structures. Similar to https://github.com/P

Nov 28, 2022
Simple Binary Encoding (SBE) - High Performance Message Codec

Simple Binary Encoding (SBE) SBE is an OSI layer 6 presentation for encoding and decoding binary application messages for low-latency financial applic

Nov 30, 2022
Zmeya is a header-only C++11 binary serialization library designed for games and performance-critical applications

Zmeya Zmeya is a header-only C++11 binary serialization library designed for games and performance-critical applications. Zmeya is not even a serializ

Nov 2, 2022
Nov 28, 2022
A C++11 or library for parsing and serializing JSON to and from a DOM container in memory.
A C++11 or library for parsing and serializing JSON to and from a DOM container in memory.

Branch master develop Azure Docs Drone Matrix Fuzzing --- Appveyor codecov.io Boost.JSON Overview Boost.JSON is a portable C++ library which provides

Dec 1, 2022
libcluon is a small and efficient, single-file and header-only library written in modern C++ to power microservices.

libcluon Linux & OSX Build (TravisCI) Win64 Build (AppVeyor) Test Coverage Coverity Analysis CII Best Practices libcluon is a small single-file, heade

Nov 19, 2022
Cap'n Proto serialization/RPC system - core tools and C++ library
Cap'n Proto serialization/RPC system - core tools and C++ library

Cap'n Proto is an insanely fast data interchange format and capability-based RPC system. Think JSON, except binary. Or think Protocol Buffers, except

Dec 1, 2022
Header-only C++11 library to encode/decode base64, base64url, base32, base32hex and hex (a.k.a. base16) as specified in RFC 4648, plus Crockford's base32. MIT licensed with consistent, flexible API.

cppcodec Header-only C++11 library to encode/decode base64, base64url, base32, base32hex and hex (a.k.a. base16) as specified in RFC 4648, plus Crockf

Nov 17, 2022
FlatBuffers Compiler and Library in C for C

OS-X & Ubuntu: Windows: The JSON parser may change the interface for parsing union vectors in a future release which requires code generation to match

Nov 26, 2022