Presentation is loading. Please wait.

Presentation is loading. Please wait.

JSON++ - A Simple class library for JSON

Similar presentations


Presentation on theme: "JSON++ - A Simple class library for JSON"— Presentation transcript:

1 JSON++ - A Simple class library for JSON
PJ Naughter UL Identity Management & Security Microsoft MVP in Visual Studio and Development Technologies Dublin C/C++ Users Group Presentation (11 June 2018)

2 Talk Agenda JSON Introduction JSON Data Types Sample JSON JSON Pointer
JSON++ Features JSON++ Implementation JSON++ Performance Further Reading

3 JSON Introduction JSON is a designed as a lightweight data-interchange format Based on a subset of the JavaScript Programming Language which is an ECMA standard Is a text format but uses conventions familiar to the C-family of program languages including C, C++, C#, Java, JavaScript, Perl & Python JSON is quite a compact specification ( and boils down to roughly just three pages and 350 words. Uses a simple flow diagramming metaphor to describe the various parts of the specification. This is IMHO the main reason for its popularity in modern micro service based development when compared to XML. Is solely based on UTF-8 Unicode encoding. Unlike XML which supports all the complexity and flavours of Unicode such as UTF-16 (BE & LE), UTF-32 (BE & LE) and ASCII code pages and BOMs (Byte Order Markers). This is one of the reasons why developing an XML support library is so difficult as you need a comprehensive Unicode library to support XML itself. Examples of this would include Windows APIs or ICU (International Components for Unicode

4 JSON Data Types A JSON object is a collection of zero or more name value pairs. In a programming language this would corresponds to a struct, record, dictionary, hash table or associative array. Represented as “{ }” A JSON array is an ordered list of zero or more values. In a programming language this corresponds to an array, vector, list or sequence. Represented as “[ …… ]” A pair is two values represented as “string : value” A value itself can be a string, number, object, array, “true”, “false” or “null”. A string can be sequence of zero or more Unicode characters. The spec has details on how a string can be encoded into a valid JSON representation A number is some numeric value. A JSON support library / framework is free to implement / represent this however it sees fit. In C++ if you were to encode all numbers to one type you would probably use the IEEE 754 “double” data type. Again the spec specifies how a number can be encoded into a valid JSON representation “true” represents a positive boolean value “false” represents a negative boolean value “null” represents a null value in the same sense as a database null value i.e. not a value.

5 Sample JSON { "string_name" : "string value and a "quote" and a unicode char u00BE and a c:path or a /unix/path/", "bool_name" : true, "bool_second" : FaLsE, "null_name" : nULl, "negative" : , "sub_object" : { "foo" : "abc", "bar" : 1.35e2, "blah" : { "a" : "A", "b" : "B", "c" : "C" } }, "array_letters" : [ "a", "b", "c", [ 1, 2, 3 ] ] }";

6 JSON Pointer A JSON Object is a collection of zero or more name value pairs. In a programming language this would corresponds to a struct, record, dictionary, hash table or associative array. Represented as “{ }” A JSON Array is an ordered list of zero or more values. In a programming language this corresponds Similar to XPath in XML, this allows specific JSON data to be selected / extracted using a string specifier or path Published as RFC (Internet Request for Comments) 6901 Separator used is “/” Because “~” and “/” have special meaning in JSON Pointer if you want to use these as literal values, then they need to be encoded using specific rules Strings in JSON Pointer follow the same encoding rules as for JSON itself Does not support more complicated concepts in XPath such as functions, selectors etc. A specific element in a JSON array can be accessed using a simple numeric offset A specific name / value pair in an object can be accessed by using the name of the pair There are other query languages / libraries for JSON such as JSONiq, JQ, OboeJS, jsonfilter, Postgres, SQLite & SQL Server

7 Sample JSON for JSON Pointer
{ "foo": ["bar", "baz"], "": 0, "a/b": 1, "c%d": 2, "e^f": 3, "g|h": 4, "i\\j": 5, "k\"l": 6, " ": 7, "m~n": 8 }

8 Sample JSON Pointer Queries
"" returns the whole document "/foo" returns ["bar", "baz"] "/foo/0" returns "bar" "/" returns 0 "/a~1b" returns 1 "/c%d" returns 2 "/e^f" returns 3 "/g|h" returns 4 "/i\\j" returns 5 "/k\"l" returns 6 "/ " returns 7 "/m~0n" returns 8

9 JSON++ Features A simple C++ Header only Library for JSON
Available at Based on SimpleJSON C++ library A JSON value is represented via the CValue class in the JSONPP namespace The CValue class is modeled on std::variant and provides one class which can store any of the JSON data types and any sub elements Uses C++ placement new to optimize the storage of the data Takes advantage of C++ 11 features such as r-value references, move constructors and operator= methods

10 JSON++ Implementation
Parsing of JSON is provided via CValue::Parse(const char* pszJSON) and CValue::Parse(const wchar_t* pszJSON). Returns a DOM representation in the current CValue instance Encoding of JSON is provided via std::wstring CValue::Encode(unsigned long nFlags = ENCODE_FLAGS::NONE) const. The nFlags value includes support for “Pretty Printing” JSON Pointer supported via CValue* CValue::JSONPointer(const char* pszJSONPointer) and CValue* CValue::JSONPointer(const wchar_t* pszJSONPointer) Implemented using just standard C++. No Windows dependencies. Uses std::codecvt_utf8_utf16 for UTF16 support. Currently only supported / compiled on Windows but should be easily ported to GCC/Linux with minimal effort Implemented in just 1500 lines of code Comprehensive set of unit tests included in the download

11 JSON++ Performance SimpleJSON originally had a member variable for all possible JSON data types. The current implementation has moved to a C style union with pointers to C++ class instances With the simple introduction of r-value references and move semantics JSON++ was 15% faster JSON++ was then reimplemented using a std::aligned_union to store only the current active JSON data type. Parsing is now 50% faster than SimpleJSON for x86 and 40% faster for x64 Encoding is roughly the same speed as SimpleJSON

12 Further Reading My Web site http://www.naughter.com
JSON++ My Blob My JSON Specification JSON Pointer SimpleJSON C++ library simplejson Library for Python JSON for Modern C++ JQ OboeJS jsonfilter


Download ppt "JSON++ - A Simple class library for JSON"

Similar presentations


Ads by Google