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
jsonStringR6 constructor with an optionordered_jsonto preserve the insertion order of object keys using the typenlohmann::ordered_jsonfrom the vendor library. For convenience, a new R6 classojsonStringwas added which represents anordered_jsontype and inherits fromjsonString.For reference: Order of object keys and object order.
Example using
ordered_jsonto preserve the insertion order:Code Changes:
At C++ level, I created a new Rcpp class
oJsonStringto represent theordered_jsontype. Practically, it is a replication of what has been done withJsonStringRcpp class with a minor code modification inaddPropertymethod where I use theemplace_backand notemplacemethod because it couldn't compile.At R level, the R6 constructor gets a new option
ordered_jsonto select between preserving the order or not (default).Internally, using the
ordered_jsonargument we can switch to the appropriate Rcpp classJsonStringoroJsonStringat initialisation.For user's convience, a new R6 class
ojsonStringwas added as a wrapper that simply inherits fromjsonStringand defaults toordered_json.In addition, a new private state variable
.is_ordered_jsonis introduced intended for methods that need to inherit the json type i.e., when usingcopy,unflatten,updateetc.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,
jsonStringR6 interface has two new read only active fields:json_typeandis_ordered_json.Notes
I am working on some unit tests for
ojsonStringthat can be used forjsonStringtooIs it worth adding an abstract R6 base class so that both
ojsonStringandjsonStringto inherit from?closes #1