Store: store structured data as JSON objects #38
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request makes an overhaul of
Store
implementation, extending its support of edge cases in user data.The primary change is that the structured data stored in a
Store
such as vectors and maps, are stored as JSON arrays and objects, with the strings in keys and values properly encoded, so a"q } w"
should result the same string when requested, and it won't confuse the JSON parser and serializer.The type of an object stored is encoded by the first character of its serialized string:
"
means that it's a string,[
--- a vector,{
--- a map. Any other character means that it is an object with custom encoding (see below). Strings and custom objects are stored as is, but when serialized to JSON format, their contents are properly escaped to produce a valid JSON string. Vectors and maps are stored in JSON format initially.The user can add support for storage of their own classes
T
by implementingoperator<<(std::ostream&, T)
andoperator>>(std::istream&, T&)
, orjson_encode_r(std::ostream&, T)
andjson_decode_r(const char*&, T&)
. When both the operators and the functions are defined, the functions take priority.json_encode_r
andjson_decode_r
extend the JSON parser and serializer to support custom user classes and are useful outside theStore
class.Speaking of JSON, this pull request also adds generic functions for JSON production, validation, and parsing, see
json_encode
,json_scan*
andjson_decode
inJson.h
.Store::Initialise
has been updated to allow escaped comments, newlines, and spaces at the end of a line, and to keep all of the spaces in the value string, soresults in
"long space # test \\ \n newline "
. This change is not backwards compatible, but it is unlikely that anyone relied on that feature.UnitTests/StoreTest
has been rewritten to test for many edge cases and the new features.I personally think that we should stop abusing
Store
for JSON data and instead either implement a customJSON
object that stores values as a tree in memory, or just use the newjson_encode
andjson_decode
functions for (de-)serialization.I have checked that libDAQInterface Example works with the new
Store
, but I think is worth testing this code on some real codebase before merging this pull request. Can someone do that?This pull request fixes #19, #22, #25. #16 appears to be not relevant anymore.