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 a the possibility to add dynamic json objects #1795

Closed
linux-fan-dave opened this issue Oct 15, 2019 · 6 comments
Closed

Add a the possibility to add dynamic json objects #1795

linux-fan-dave opened this issue Oct 15, 2019 · 6 comments
Labels
kind: enhancement/improvement 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

@linux-fan-dave
Copy link

When an object get's added to a json tree it is converted to values. The inja engine uses the tree as an input for the template engine. I would like to have a an "lambda" or custom object with functions that are executed when the value is accesed not when the node is inserted in the tree. This adds support for some "lazyness".

@nlohmann
Copy link
Owner

In order to asses your issue, we need the following information:

  • What is the issue you have?

  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?

  • What is the expected behavior?

  • And what is the actual behavior instead?

  • Which compiler and operating system are you using? Is it a supported compiler?

  • Did you use a released version of the library or the version from the develop branch?

  • If you experience a compilation error: can you compile and run the unit tests?

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label Oct 16, 2019
@linux-fan-dave
Copy link
Author

linux-fan-dave commented Oct 16, 2019

  • What is the issue you have?

I want to use the Json tree as an input for a template engine. The tree should not be instanciated (converted to json) when it is created (nodes added) but rather when the tree is walked from the template engine.
This means I want to have a lambda function (that produces a json node when called) beeing convertible to json. When the data is accessed the lambda is called and the json is returned.

The makers of the template engine mustache have implemented a json engine themselves. The user has the possibility to add a class or a lambda as a json node. This is possible as well with nlohmann_json but in contrast to the former in lohmann the object is beeing converted to json when it is added to the tree. In the former it is only converted when it is accesed.
This means in nlohmann I can not add a user defined object to the tree. Just a json representation of it. This does allow to create "dynamic" trees.

  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?

In this implementation of json it is possible to add a lambda function to a object. It is executed when the data is accessed.

mstch::map context{
  {"lambda", mstch::lambda{[]() -> mstch::node {
    return std::string{"World"};
  }}}
};

The user converts this tree to json -> the lambda is called and the following output is created.

{ "lambda": "World" }
  • And what is the actual behavior instead?

This is not possible in nlohmann. It is only possible to add an object to the tree whose functions are called when it is added to the tree.

  • Which compiler and operating system are you using? Is it a supported compiler?

Yes

  • Did you use a released version of the library or the version from the develop branch?

Yes

  • If you experience a compilation error: can you compile and run the unit tests?

No compilation error.

@nlohmann
Copy link
Owner

I have no idea how to realize this in the current implementation, as it is based on values. Adding arbitrary lambdas to it would bring a lot of complexity.

@nlohmann nlohmann added state: please discuss please discuss the issue or vote for your favorite option and removed state: needs more info the author of the issue needs to provide more details labels Oct 17, 2019
@linux-fan-dave
Copy link
Author

Why not add a std::function<base_json()> as value type. I do not think it needs to be added to base_json as template parameter -> that the factory function is saved in a std::function is an implementation detail. A constructor needs to be added to convert everything callable (lambdas, free functions ...) to the std::function type. On access of a value the lambda would be called and the returned data would replace the lambda. I will try to implement a pull request. Is there any interest on merging any of this functionality?

@nlohmann
Copy link
Owner

This would be doable, but I am afraid of a lot of edge cases that may come up during development. For instance, what should is_number() return? Should be function be called for this and is_number() on its result? How would JSON pointers work when "entering" a dynamic part? What about code like j["foo"]["bar"] if j["foo"] is a function? A lot of this can be solved, but it may always be some kind of special case.

@stale
Copy link

stale bot commented Nov 20, 2019

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 20, 2019
@stale stale bot closed this as completed Nov 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: enhancement/improvement 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