Skip to content
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

Merged
merged 1 commit into from
Jan 30, 2025

Conversation

dyackzan
Copy link
Collaborator

@dyackzan dyackzan commented Jan 29, 2025

Before these changes, primitive types do not have a "__type" field when the blackboard is converted to json like in the screenshot:

Screenshot 2025-01-24 at 1 05 31 PM

After these changes:

  "View Workspace": {
    "acceleration_scale_factor": {
      "type": "string",
      "value": "1.0"
    },
    "controller_action_server": {
      "type": "string",
      "value": "/joint_trajectory_controller/follow_joint_trajectory"
    },
...                                                                                                                                                                                                                                                
    "pose_count": {                                                                                                                                                                                                                                                   
      "type": "int64_t",                                                                                                                                                                                                                                              
      "value": 2                                                                                                                                                                                                                                                      
    }
  }

resolves https://github.com/PickNikRobotics/moveit_pro/issues/10488

@dyackzan dyackzan requested a review from dsobek January 29, 2025 00:53
Copy link

@dsobek dsobek left a 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.

@@ -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";
Copy link

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)

Copy link

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?

Copy link
Collaborator Author

@dyackzan dyackzan Jan 29, 2025

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

@dsobek
Copy link

dsobek commented Jan 29, 2025

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.

I see, calling add_field in the body of BT_JSON_CONVERTER takes care of this already.

Copy link

@dsobek dsobek Jan 29, 2025

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).

Copy link
Collaborator Author

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

Copy link
Collaborator Author

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_ts so it's not possible to to have a type == typeid(bool) case here

@dyackzan dyackzan force-pushed the json-add-fields-to-primitive-types branch 2 times, most recently from 00fc055 to 067a07b Compare January 29, 2025 20:00
@dyackzan dyackzan requested a review from dsobek January 29, 2025 20:00
@dyackzan dyackzan force-pushed the json-add-fields-to-primitive-types branch from 067a07b to d320cbf Compare January 29, 2025 20:03
src/json_export.cpp Outdated Show resolved Hide resolved
@dyackzan dyackzan force-pushed the json-add-fields-to-primitive-types branch from d320cbf to b46aee1 Compare January 29, 2025 21:14
@dyackzan dyackzan requested a review from dsobek January 29, 2025 21:15
return Entry{ BT::Any(source.get<double>()), BT::TypeInfo::Create<double>() };
}
if(source.is_boolean())
if(!source.contains("__type"))
Copy link

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.

Suggested change
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.

Copy link
Collaborator Author

@dyackzan dyackzan Jan 29, 2025

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

src/json_export.cpp Outdated Show resolved Hide resolved
@dyackzan dyackzan force-pushed the json-add-fields-to-primitive-types branch 2 times, most recently from 41115b8 to 342bb1b Compare January 29, 2025 23:00
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]>
@dyackzan dyackzan force-pushed the json-add-fields-to-primitive-types branch from 342bb1b to 878321a Compare January 29, 2025 23:05
@dyackzan dyackzan requested a review from dsobek January 29, 2025 23:06
@dyackzan dyackzan merged commit 8cd34f4 into master Jan 30, 2025
13 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants