JSON++ v1.01 A set of C++ classes to support
I have been working with JSON recently in a number of C++ projects and wanted
to learn it by understanding the JSON specification by writing my own JSON parser
/ encoder. There are a number of nice JSON libraries for C++ out there such as
JSON for Modern C++. I particularly
liked the SimpleJSON library which was able to implement a nice JSON parser / encoder
in c. 1300 lines of code. I decided to write my own class based on SimpleJSON but
using some modern C++ features such as r-value references, move constructors and
operator=. Thus was born JSON++. Included in the download is the JSON++ source code
and a VC 2017 application to fully exercise all the functionality of JSON++. The
main class provided is CValue and is implemented in a JSONPP namespace. All the
constructors of the CValue class allow a JSON value object to be constructed directly
from other C++ types such as bool, double, long, STL strings, STL vectors, STL maps
etc. Standard C++ operator= methods are also provided which mirror the constructor
methods. The class also supports construction and assignment from C++ r-value references.
A "Parse" method of CValue allows a JSON encoded string (either UTF8 or
UTF16) to be parsed into the CValue instance. An "Encode" method allows
a JSON encoded string in the form of a std::wstring to be created from a CValue
instance. Various other CValue methods are modeled on the SimpleJSON classes.
- Provides a complete JSON parser and encoder in c. 1300 lines of C++.
- All the code is provided in a single C++ Header only module of "JSON++.h"
- All the code clean compiles at warning level 4, is /analyze and /permissive-
clean and includes ASCII, Unicode, x86 and x64 built configurations.
- The class supports both UTF8 and UTF16 encoded JSON.
- Performance testing of the code has shown that JSON++ is about 50% faster
than SimpleJSON for parsing. This is mainly achieved through the use of r-value
references and placement new to avoid use of explicit heap memory allocations
which SimpleJSON is forced to use. The encoding speed of JSON++ is the same
as SimpleJSON, although support is provided in JSON++ for pretty printing the
- Performance testing for parsing of a c. 400 character UTF16 JSON string
on my Core I7 quad core 4Ghz development machine indicates a performance of
about 30 MB / second.
- Encoding performance works out at about 50,000 encodes per second for a
c. 400 character JSON string on my Core I7 machine.
- If a parsing error occurs during a call to the CValue::Parse method, then
a JSONPP::CParseException will be thrown. This class provides a "m_Reason"
enum value which specifies why the parsing failed and a "m_nIndex"
value which specifies where the parsing error occurred in the JSON encoded text.
- All the functionality of JSON++ is implemented using standard C++. This
includes UTF8 to UTF16 string conversion.
- The classes have been tested only on VC 2017 but should be compilable on
earlier versions of Visual C++. The sample app uses some VC specific classes
but again should be easily converted to support running on non Windows operating
systems. If you think this would be useful, then please let me know.
- Provides support for JSON pointer (https://tools.ietf.org/html/rfc6901)
via a simple JSONPP::CValue::JSONPointer method.
The enclosed zip file contains the JSON++ source
code and a VC 2017 console based application with exercises all of the JSON++ functionality.
- You are allowed to include the source code in any product (commercial, shareware,
freeware or otherwise) when your product is released in binary form.
- You are allowed to modify the source code in any way you want except you
cannot modify the copyright details at the top of each module.
- If you want to distribute source code with your application, then you are
only allowed to distribute versions released by the author. This is to maintain
a single distribution point for the source code.
- To use the JSON++ classes in your project simply #include "JSON++.h"
in which ever of the modules in your application requires the JSON functionality.
The header file will look after pulling in any dependent header files
- That should be it. You should now have a fully functional JSON parser and
encoder now available to your Windows C++ application.
8 February 2018
v1.01 (10 January 2018)
- Updated copyright details.
- Added support for JSON Pointer (https://tools.ietf.org/html/rfc6901)
support to the code via a new CValue::JSONPointer method.
- Made a number of methods of the CValue class static as they did not need
to be non-static.
V1.0 (25 November 2017)