-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Allow preventing implicit converions to basic_json #4324
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
docs/mkdocs/docs/api/macros/json_use_implicit_constructors.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# JSON_USE_IMPLICIT_CONSTRUCTORS | ||
|
||
```cpp | ||
#define JSON_USE_IMPLICIT_CONSTRUCTORS /* value */ | ||
``` | ||
|
||
When defined to `0`, implicit constructors are switched off. By default, implicit constructors are switched on. The | ||
value directly affects [the constructor of `basic_json`](../basic_json/basic_json.md). | ||
|
||
## Default definition | ||
|
||
By default, implicit constructors are enabled. | ||
|
||
```cpp | ||
#define JSON_USE_IMPLICIT_CONSTRUCTORS 1 | ||
``` | ||
|
||
## Notes | ||
|
||
!!! hint "CMake option" | ||
|
||
Implicit constructors can also be controlled with the CMake option | ||
[`JSON_ImplicitConstructors`](../../integration/cmake.md#json_legacydiscardedvaluecomparison) | ||
(`ON` by default) which defines `JSON_USE_IMPLICIT_CONSTRUCTORS` accordingly. | ||
|
||
## Examples | ||
|
||
??? example | ||
|
||
This is an example for an implicit construction: | ||
|
||
```cpp | ||
json j = "Hello, world!"; | ||
``` | ||
|
||
When `JSON_USE_IMPLICIT_CONSTRUCTORS` is defined to `0`, the code above no longer compiles. Instead, it must be | ||
written like this: | ||
|
||
```cpp | ||
json j = json("Hello, world!"); | ||
``` | ||
|
||
??? example | ||
|
||
Disabling constructors is particularly useful to avoid implicit allocations that can lead to invalid memory access: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Disabling implicit constructors |
||
|
||
```cpp | ||
const json & valueAt(const json & map, const std::string & key) { | ||
return map[key]; | ||
} | ||
const json::object_t & getObjectRef(const json & obj) { | ||
return obj.get_ref<const json::object_t &>(); | ||
} | ||
const json::string_t & getStringRef(const json & obj) { | ||
return obj.get_ref<const json::string_t &>(); | ||
} | ||
|
||
int main() { | ||
json j2 = { | ||
{"string", "foo"}, | ||
{"object", { | ||
{"currency", "USD"}, | ||
{"value", 42.99} | ||
}} | ||
}; | ||
|
||
json::object_t & objectRef = getObjectRef(valueAt(j2, "object")); | ||
json::string_t & nestedObjectRef = getStringRef(valueAt(objectRef, "currency")); | ||
std::cout << nestedObjectRef << '\n'; | ||
} | ||
``` | ||
|
||
This program causes undefined behavior when trying to access `nestedObjectRef`, because the second call to `valueAt` | ||
implicitly converts the `json::object_t` to a `json`, which is immediately discarded afterwards, causing | ||
`nestedObjectRef` to become a dangling reference. | ||
|
||
When `JSON_USE_IMPLICIT_CONSTRUCTORS` is defined to `0`, the code above no longer compiles. Instead, you must | ||
define an override for `valueAt` that takes `json::object_t`. | ||
|
||
```cpp | ||
const nlohmann::json & valueAt(const json::object_t & map, const std::string & key) { | ||
return map.at(key); | ||
} | ||
``` | ||
|
||
This will prevent the implicit construction of an intermediate object, and the code will work as expected. | ||
|
||
|
||
## See also | ||
|
||
- [**basic_json constructor**](../basic_json/basic_json.md) - get a value (implicit) | ||
|
||
## Version history | ||
|
||
- Added in version 3.11.4. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
constructors