a JSON parser written in C++ and designed for simplicity of both use and using the results
Switch branches/tags
Clone or download
Permalink
Failed to load latest commit information.
VS Removed the now unusued Memory.cpp and Memory.h - memRealloc() was no… Apr 26, 2017
bin Added some ignores to help filter build output out as a windows build… Feb 15, 2016
test Reverted the test/test.h situation back to the earlier state to "fix"… Oct 2, 2018
.appveyor.yml Begun writing a Meson file for the tests Sep 30, 2018
.codecov.yml Added a config file for codecov Apr 15, 2017
.gitignore Added the ability to build with code coverage stats Apr 15, 2017
.travis.yml Added coverage builds for each of the clang builds Sep 29, 2018
JSONArray.cpp Swapped out all managedPtr_t<> use for opaquePtr_t<> use, implementin… Oct 2, 2018
JSONAtom.cpp 64-bit'd the integer type throughout Apr 26, 2017
JSONBool.cpp Implemented a function to be able to set the value of a JSONBool Jul 15, 2014
JSONErrors.cpp Fixed all the String.h includes to String.hxx Sep 30, 2018
JSONFloat.cpp Corrected the copyright notices to reflect the current year and seein… Mar 27, 2013
JSONInt.cpp 64-bit'd the integer type throughout Apr 26, 2017
JSONNull.cpp Corrected the copyright notices to reflect the current year and seein… Mar 27, 2013
JSONObject.cpp Swapped out all managedPtr_t<> use for opaquePtr_t<> use, implementin… Oct 2, 2018
JSONString.cpp Enhanced JSONString with better value retrieval, and a membership ("i… Apr 25, 2017
LICENSE.GPL Added licensing to the entire library so it's properly LGPL covered Oct 31, 2012
LICENSE.LGPL Added licensing to the entire library so it's properly LGPL covered Oct 31, 2012
Makefile Fixed our Makefile-based build system's ordering for parallel builds … Oct 2, 2018
Makefile.inc Slightly corrected my implementation of code coverage Apr 15, 2017
Makefile.mac Fixed a stupid mistake in the Mac OSX override for the library output… Jul 17, 2014
Parser.cpp Fixed the previous change causing valid test failures by providing th… Sep 30, 2018
Parser.h Removed the unused field jsonEnd from JSONParser Jun 21, 2018
README.md Added our AppVeyor build status to the README Sep 30, 2018
Stream.cpp Fixed the data loss warnings from MSVC in the stream code as we happe… Sep 30, 2018
String.cpp Fixed all the String.h includes to String.hxx Sep 30, 2018
String.hxx Removed the debugging on_finish from the AppVeyor config Sep 30, 2018
Writer.cpp Same fix as the last commit but for JSONInt::store() Sep 30, 2018
build32.sh Fixed the build system helpers Jul 11, 2014
build64.sh Fixed the build system helpers Jul 11, 2014
build64Debian.sh On my system, GCC 5 or newer is now required to successfully build th… Sep 29, 2018
buildmac.sh Implemented build overrides for Mac to allow rSON to be buildable Jul 16, 2014
internal.h Reverted the test/test.h situation back to the earlier state to "fix"… Oct 2, 2018
meson.build Begun writing a Meson file for the tests Sep 30, 2018
rSON.h Fixed a bunch of `final override` definitions that should have used r… Oct 4, 2018
rSON.pc.in Bumped the version number after the last set of changes to the organi… Jul 7, 2017
rSON_socket.cpp Fixed the type conversions for the windows send()/recv() calls in the… Sep 30, 2018
rSON_socket.h ... And again Sep 30, 2018
rSON_socket.pc.in Fixed several issues with the build of librSON.socket.so and also the… Jul 7, 2017

README.md

rSON 0.2.2

The project was conceived after surveying the C/C++ based JSON implementations for something that was lightweight and easy to use (and by that, I mean requires no setup code and simply works to produce output that is easy to access the structure of and grab data out from) and comming up very short.

Build Status Build status codecov

The Idea

rSON implements a configuration-less JSON complient parser which does literally all the donkey work.

An example call works like:

JSONAtom *rootNode = rSON::parseJSON("[\"My test array\", \"With multiple elements\", True]");

Or to parse from a file:

rSON::fileStream_t file("file.json", O_RDONLY);
JSONAtom *rootNode = rSON::parseJSON(file);

It also implements a set of classes that form the following tree structure to build trees out of the results of parsing:

  • JSONAtom
    • JSONNull
    • JSONBool
    • JSONInt
    • JSONFloat
    • JSONString
    • JSONArray
    • JSONObject

Internally the tree is very strongly typed, throwing exceptions that you can catch when you attempt an invalid conversion, but due to limitations in C++ itself has to present the entire tree as a set of JSONAtom's. To do the job of converting from those JSONAtom pointers/references, JSONAtom contains a set of as*() functions which perform the type conversions. Although mildly inconvinient, this still provides nice semantics in my honest opinion.

rSON implements a stream-based interface (As of 0.2.0), which allows user programs to easilly plug unknown data sources via stream_t adapters into rSON to extract the JSON data into a node tree (and write it back out)

The API

The following should be everything you need to know without reading the header for imediately using rSON in your programs and using it effectively:

JSONAtom implements the following conversion functions:

  • void *asNull()
  • bool asBool()
  • int asInt()
  • double asFloat() - The return type for this is forced by the numerical requirements of the standard
  • const char *asString() - do not call free() or delete on this as cleanup is automatic when you delete the root JSONAtom
  • JSONString &asStringRef()
  • JSONArray *asArray() and JSONArray &asArrayRef()
  • JSONObject *asObject() and JSONObject &asObjectRef()

The last three sets of functions are the odd ones out in that they convert between rSON types rather than directly to C++ types for use directly in your programs. This is done to allow rSON to provide a nicer interface as JSONArray and JSONObject implement various operator overrides to allow transparent lookup of JSONAtom's held by each via either numerical index (JSONArray) or associative style string index (JSONObject). This is also done to allow direct access to a JSONString object for std::string-like use.

The JSONAtom also fully supports indexing if the real type it represents would also - so for JSONArray and JSONObject nodes, no manual conversion is required for indexing into complex trees (as of 0.1.0). This further allows elegant code through rSON in user programs.

rSON attempts (and I hope succeeds) to provide a fully Associative Array style interface to JSONObject. I have to implement ->get() methods though for when this is too bulky or doesn't fit nicely when using rSON.

There are a couple of exceptions that will get thrown either if you feed rSON with data that's not actually JSON, or if you try to convert to a type but the JSONAtom is not that type. These exceptions are:

  • JSONParserError
  • JSONTypeError

They both provide an error() function for displaying a message of what was wrong, and JSONParserError provides way to get the raw error constants if you wish to write your own messages. The other exceptions, although possible, are not disuessed here as they are involved in the lookup systems for JSONArray and JSONObject and should only arrise from the JSON not being what was expected during data extraction.

stream_t (new in 0.2.0) provides a simple but strong interface to any byte-based stream interfaces, be it a file, memory, some kind of socket (JSON-RPC), or anything else for which you can dream up an interface conforming to stream_t. As of 0.2.0, rSON::parseJSONFile() is deprecated in favour of using a stream object and passing that into rSON::parseJSON() to get a node tree. This change is designed to greatly simplify more complex use-cases so the user program doesn't have to worry about how to construct a buffer that the parser can deal with, which also helps speed the entire process up.

The License

As stated in the code, I have licensed the library using LGPL v3+. Please report bugs to dx-mon@users.sourceforge.net

Known Bugs

None