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

Is there a nice way to associate external values with json elements? #1256

Closed
timprepscius opened this issue Sep 24, 2018 · 7 comments
Closed
Labels
state: please discuss please discuss the issue or vote for your favorite option state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@timprepscius
Copy link

I am contemplating caching deserializations of json subtrees within the json tree.
Is there a clean way of doing this without modifying your code?

json j = { ... };
auto myPrivateDeserialization = makeMyPrivateDeserialization(j["bigtreeofdata"]);
j["bigtreeofdata"].privateDeserialization = myPrivateDeserialization;

@timprepscius
Copy link
Author

Well, no response, so I would like to modify my question to be:

What is the best way to modify your library to allow for custom values bound to elements.
I'm thinking to modify your class basic_json class to derive from a custom class. I would like that to be a template parameter, but it seems like this might be difficult to do, because there are so many template parameters already.

using caching_json = ...::basic_json<caching_class>;

I think I would also need to change swap, to call a base class swap if there is one, I guess.

Would this be the best way to go about it? Can you think of a better way?

@nlohmann nlohmann added the state: help needed the issue needs help to proceed label Sep 27, 2018
@nlohmann
Copy link
Owner

I am currently not aware of a nice means to extend the class. Any help appreciated.

@nlohmann
Copy link
Owner

Maybe related: #1201.

@timprepscius
Copy link
Author

We can't extend it via deriving from basic_json and then declaring the manipulating the template parameter of the allocator, because the object_t map is a map of basic_json and not a map of basic_json*.

I think the above statement is true. I could be wrong.

This means that in order to associate external values we must either 1. have another template parameter, "customization field", 2. or change basic_json's derivation.

I think the above statement is true. I could be wrong. There are other options of using the deserialization listener of the json to create a parallel structure, but I am trying to avoid that, at least in theory.

  1. Template parameter. I don't see why there could not be a template parameter of "external_value" and have it default to an empty struct typedef.

On the copy, swap methods of the json, you would need to copy and swap the external values as well. This would be trivial, and incorporating different structures, one would merely need to specialize whatever template method is used to do this copy/move/etc.

For an empty structure external value, I would assume the compiler would optimize these extra instructions out, and indeed, I would guess that the memory size of the json class would not change.

I'm guessing it would be maybe 10-20 extra lines of code.

  1. Deriving from a template parameter base type. For my special case, I can only personally see draw backs for this. While it will allow for a more interesting json specializations, it will cause the hassle of worrying about if that move/copy/etc needs be virtual or not, when to call what methods.

Those are my thoughts at the moment.

@nlohmann
Copy link
Owner

So idea 1 would just mean that you add an arbitrarily-typed (public?) member variable to the basic_json class?

@timprepscius
Copy link
Author

Yep. Seems dirty. Lol. But if it were an empty struct by default... I would guess it would be optimized out.

@nlohmann nlohmann added state: please discuss please discuss the issue or vote for your favorite option and removed state: help needed the issue needs help to proceed labels Oct 23, 2018
@stale
Copy link

stale bot commented Nov 22, 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 Nov 22, 2018
@stale stale bot closed this as completed Nov 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: please discuss please discuss the issue or vote for your favorite option 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