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)
- Initializing `json` by direct initialization and copy initialization invokes different constructors HOT 2
- why a single-object json file appears in an array HOT 2
- In highly nested functions, passing json into a function leads to a segmentation fault/bus error HOT 15
- Exception SIGSEGV - Segmentation violation signal on file parsing (v3.11.2, linux, doctest) HOT 5
- use of overloaded operator '<=>' is ambiguous HOT 9
- README.md overuses `template` keyword before `get` function HOT 2
- Parser and constructor resolve integer types differently HOT 3
- Exception thrown when dumping utf-8 characters when using std::string HOT 2
- 3.11.3: test suite fails in 4 units HOT 8
- Deprecation warning on save action in check-amalgamation CI step HOT 1
- Build failure on macOS Sonoma 14.1.1 HOT 4
- very accidental: SIGABRT when parsing json string with allow_exceptions=false HOT 7
- `to_json` is erroneously converting enums with underlying unsigned types to signed numbers
- Exception when trying to insert my json object inside json file HOT 4
- Issue with including <version> HOT 2
- stack overflow while fuzzing parse-cbor-fuzzer HOT 3
- Parsing simple zeroes throws type exception HOT 1
- Newer serialization macros not listed in README
- Cannot use std::format on nholman::json objects HOT 8
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.