Comments (6)
This is documented behavior, see https://json.nlohmann.me/api/macros/nlohmann_define_type_non_intrusive/#parameters
from json.
In this case, I need to use the adl_serializer
specialization everywhere? I see. Thank you for the swift response, I had indeed overlooked this limitation.
Would you be open to tracking this deficiency as a feature request/request for enhancement then?
from json.
Would you be open to tracking this deficiency as a feature request/request for enhancement then?
Sure - though I hope it does not mean adding another dozen of macros for this case...
from json.
This works just fine, but now structs containing this type cannot be json-converted using the macros anymore:
Godbolt link: https://godbolt.org/z/q8oxq61sx (gcc trunk, nlohmann_json trunk)
That's simply because you didn't implement the version of from_json
that takes the destination object by reference. You need that in order to support the get_to
from the macros. Also, you should use the templated version of the first parameter so that you can also support ordered_json
.
NLOHMANN_JSON_NAMESPACE_BEGIN
template <> struct adl_serializer<NonDefaultConstructible> {
template<typename BasicJsonType>
static void to_json(BasicJsonType &json_value, const NonDefaultConstructible &data) {
json_value = data.get_inner_value();
}
template<typename BasicJsonType>
static void from_json(BasicJsonType && j, NonDefaultConstructible &val)
{
// There is no setter for this member, so I had to create a new one and move it into the original.
// I assume in the original there is a setter, which would make this easier.
val = NonDefaultConstructible{j.template get<std::string>()};
}
};
NLOHMANN_JSON_NAMESPACE_END
@nlohmann It would probably be a good idea to update the docs examples to use the template BasicJsonType
.
from json.
template<typename BasicJsonType> static void from_json(BasicJsonType && j, NonDefaultConstructible &val)
Just curious, why the forwarding reference? When I try it, it seems to work with a const BasicJsonType&
too, and clangd gives me a warning that the forwarding reference is never forwarded inside the function body?
from json.
It should probably just be const&
.
from json.
Related Issues (20)
- Behavioral change of serializers in 3.11.3. Name lookup related. HOT 1
- Crash when construct a new json object HOT 2
- Is float valid number been limited? HOT 5
- git ref got interpreted as number HOT 1
- Wrong function name in documentation example HOT 2
- confused of paresr callback's behavior of null object: remove or discarded? HOT 1
- natvis not updated to 3.11.3 HOT 1
- Json parsed from raw string does not interpret lists of objects like json parsed from file HOT 2
- Cannot unflatten json object HOT 2
- [json.exception.parse_error.101] parse error at line 1, column 4520: syntax error while parsing value - invalid string: forbidden character after backslash; last read: '".\? HOT 2
- (official) example for parsing "custom data source" (implementing input iterator) does not compile HOT 1
- Unexpected conversion from 'true'/'false' to some integer types HOT 1
- CodeQL suppressions lack justification HOT 2
- Can't use nlohmann on Linux HOT 7
- Clone is long due to large history HOT 4
- iteration_proxy has limited usefulness in C++20 range views
- json.exception.type_error.302 HOT 3
- Error 302: type must be number, but is it number HOT 2
- nlohmann::ordered_json: not compatible with NLOHMANN_DEFINE_TYPE_INTRUSIVE
- when enum is used as key in a map the json format is not an expected format
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json.