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

Nesting additional data in json object #1328

Closed
Chrums opened this issue Oct 31, 2018 · 2 comments
Closed

Nesting additional data in json object #1328

Chrums opened this issue Oct 31, 2018 · 2 comments
Labels
state: needs more info the author of the issue needs to provide more details state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@Chrums
Copy link

Chrums commented Oct 31, 2018

v3.3.0
gcc 7.3.0

Inexperienced C++ user here, so I may just be overlooking something.
My goal is to nest additional, context specific data inside a json object, such that when a json reference is returned by a member function, the data is passed to the child.

To be more specific, I have a Scene class that is deserializing a json file to instantiate a prefab. Within this prefab json file, there are IDs that can be locally referenced, but when the Scene is deserializing it, it generates a new ID for each ID in the json, so that if a prefab is instantiated multiple times, each instance won't have the same IDs.

{
  "components": {
    "hierarchy": {
      "0": {
        "parent": null,
        "children": ["1"]
      },
      "1": {
        "parent": "0",
        "children": []
      }
    }
  }
}

So in the preceding example, 0 and 1 are the local IDs, and this deserialization "context" would create a map of local IDs to real IDs that would look something like...

{  
  "0": "89207bbc-3e9a-4310-897b-aef7628ee063",
  "1": "79322b6c-4a2b-ab91-897b-f8f7626edf64"
}

The Scene doesn't know how to deserialize Hierarchy components, so it calls deserialize on each component, passing in the data that it needs to deserialize itself, such as...

{
  "parent": null,
  "children": ["1"]
}

But now the Hierarchy component knows nothing of the 'real ID' that "1" maps to.

I tried creating a class extending json, and storing this map in there as a shared_ptr, and wrapping all of the accessors, but quickly realized this wouldn't work because they return references to json objects, and I can't return a reference to my type.

The end goal is to allow the deserialization for the Hierarchy component to look something along the lines of...

void deserialize(json j) {
  parent = j["parent"].get<optional<Entity>>();
  children = j["children"].get<vector<Entity>>();
}

Which would call a specialized template on the json object for optional and vector that would look in the ID map, and return an Entity with the context specific ID.

The only thing I can think of is modifying the entire basic_json class to hold a void* or something, that it passes down to each child, and then my wrapper would utilize that void* to store the context specific data.

@nlohmann
Copy link
Owner

nlohmann commented Nov 3, 2018

I am sorry, but I have not fully understood the issue.

  • Right now, it is not possible to extend the JSON class - each object from it models a single JSON value with a type like null, boolean, number, etc.
  • If you want to read/write other types from/to JSON, please have a look at this paragraph from the README. This can allow a deserialize function as you sketched it above.

Can you provide more information?

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label Nov 3, 2018
@stale
Copy link

stale bot commented Dec 3, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Dec 3, 2018
@stale stale bot closed this as completed Dec 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: needs more info the author of the issue needs to provide more details state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated
Projects
None yet
Development

No branches or pull requests

2 participants