-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
Comments
In order to asses your issue, we need the following information:
|
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. 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.
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" }
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.
Yes
Yes
No compilation error. |
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. |
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? |
This would be doable, but I am afraid of a lot of edge cases that may come up during development. For instance, what should |
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. |
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".
The text was updated successfully, but these errors were encountered: