Extend jsonString R6 class to support ordered json type #2
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 PR extends
jsonString
R6 constructor with an optionordered_json
to preserve the insertion order of object keys using the typenlohmann::ordered_json
from the vendor library. For convenience, a new R6 classojsonString
was added which represents anordered_json
type and inherits fromjsonString
.For reference: Order of object keys and object order.
Example using
ordered_json
to preserve the insertion order:Code Changes:
At C++ level, I created a new Rcpp class
oJsonString
to represent theordered_json
type. Practically, it is a replication of what has been done withJsonString
Rcpp class with a minor code modification inaddProperty
method where I use theemplace_back
and notemplace
method because it couldn't compile.At R level, the R6 constructor gets a new option
ordered_json
to select between preserving the order or not (default).Internally, using the
ordered_json
argument we can switch to the appropriate Rcpp classJsonString
oroJsonString
at initialisation.For user's convience, a new R6 class
ojsonString
was added as a wrapper that simply inherits fromjsonString
and defaults toordered_json.
In addition, a new private state variable
.is_ordered_json
is introduced intended for methods that need to inherit the json type i.e., when usingcopy
,unflatten
,update
etc.Also, I added a private method
getPtr()
to abstract away a common pattern found across methods; and extend it a bit to guard against mixing json types . e.g., when adding a property from default json type to ordered json will raise an error. See below :Lastly,
jsonString
R6 interface has two new read only active fields:json_type
andis_ordered_json
.Notes
I am working on some unit tests for
ojsonString
that can be used forjsonString
tooIs it worth adding an abstract R6 base class so that both
ojsonString
andjsonString
to inherit from?closes #1