-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add type & value fields to primitive json types #5
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For custom types, I take it that the value and type fields must be added in the custom converter function then?
Happy to approve once tests are updated.
src/json_export.cpp
Outdated
@@ -16,19 +16,23 @@ bool JsonExporter::toJson(const Any& any, nlohmann::json& dst) const | |||
|
|||
if(any.isString()) | |||
{ | |||
dst = any.cast<std::string>(); | |||
dst["type"] = "string"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be good to pull out "type" and "value" as constants here.
(I've been paranoid about typoing things in situations like this recently)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also should "type" be "__type"? Or is the difference in name so that you know to pick out the "value" json field?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good calls, it should be __type to align
I see, calling |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok some more questions:
Would it be helpful to have boolean support? (Do these get encoded as string types?)
Does the add_field
function when defining a custom converter take care of converting primitives to this format? (I suppose this could be answered with a test case).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Many objects are just strings on the blackboard and then get converted when they are loaded with getInput. Bools are always strings on the blackboard and get converted here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that I'm adding tests, I went ahead and tried to add one for a bool, and it turns out that the behavior tree implementation of Any
just casts bools to int64_t
s so it's not possible to to have a type == typeid(bool)
case here
00fc055
to
067a07b
Compare
067a07b
to
d320cbf
Compare
d320cbf
to
b46aee1
Compare
src/json_export.cpp
Outdated
return Entry{ BT::Any(source.get<double>()), BT::TypeInfo::Create<double>() }; | ||
} | ||
if(source.is_boolean()) | ||
if(!source.contains("__type")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe I saw that nlohmann::json
has a find
function.
if(!source.contains("__type")) | |
auto source_type_it = source.find(kTypeField); | |
if(source_type_it == source.end()) |
Then you can replace usage of source[kTypeField]
with source_type_it
below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like the only other usage of kTypeField in this function is to lookup if the type is in the available converters (type_names_
) so I wouldn't be able to reuse this iterator. But yeah definitely better to reuse the kTypeField const than re-write the string again
41115b8
to
342bb1b
Compare
This aligns with how custom types are represented by the JsonExporter. * Update both toJson & fromJson functions in the JsonExporter * Add & update tests Co-authored by: David Sobek <[email protected]>
342bb1b
to
878321a
Compare
Before these changes, primitive types do not have a "__type" field when the blackboard is converted to json like in the screenshot:
After these changes:
resolves https://github.com/PickNikRobotics/moveit_pro/issues/10488