You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Goes to structures, and string arrays, in the C way, throwing exceptions.
According to the normal mode of understanding, a string is also an array, and we can force to specify, whether he is a string, or an array of strings.
Or, whether we want to use the array approach to strings or the string object approach to strings.
The design of these structures requires the use of 'char array' or else a second copy of the code would have to be wrapped for C to call, since C does not exist for std::string.
So I think the 'char []' array of strings in structs is legal.
Reproduction steps
Paste the Minimal code example code into the console for debugging, and it will directly trigger the assertion issue
Expected vs. actual results
I hope that the string array 'char' can be copied correctly
Minimal code example
namespace ns
{
using json = typename::nlohmann::basic_json<nlohmann::ordered_map>;
// a simple struct to model a person
struct person
{
char name[256];
int age;
};
void to_json(json& j, const person& p)
{
j = json{ {"name", p.name}, {"age", p.age} };
}
void from_json(const json& j, person& p)
{
j.at("name").get_to(p.name);
j.at("age").get_to(p.age);
}
}
int main() {
ns::person p{ "Ned Flanders", 60 };
ns::json j = p;
std::cout <<j << std::endl; auto p2 = j.get<ns::person>();}
Error messages
/// @brief access specified array element with bounds checking
/// @sa https://json.nlohmann.me/api/basic_json/at/
const_reference at(size_type idx) const
{
// at only works for arrays
if (JSON_HEDLEY_LIKELY(is_array()))
{
JSON_TRY
{
return m_value.array->at(idx);
}
JSON_CATCH (std::out_of_range&)
{
// create better exception explanation
JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", *this));
}
}
else
{
JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), *this));
}
}
### Compiler and operating system
VS2022 MSVC 17.1.0
### Library version
JSON for Modern C++ version 3.10.5
### Validation
- [X] The bug also occurs if the latest version from the [`develop`](https://github.com/nlohmann/json/tree/develop) branch is used.
- [X] I can successfully [compile and run the unit tests](https://github.com/nlohmann/json#execute-unit-tests).
The text was updated successfully, but these errors were encountered:
Description
Goes to structures, and string arrays, in the C way, throwing exceptions.
According to the normal mode of understanding, a string is also an array, and we can force to specify, whether he is a string, or an array of strings.
Or, whether we want to use the array approach to strings or the string object approach to strings.
The design of these structures requires the use of 'char array' or else a second copy of the code would have to be wrapped for C to call, since C does not exist for std::string.
So I think the 'char []' array of strings in structs is legal.
Reproduction steps
Paste the Minimal code example code into the console for debugging, and it will directly trigger the assertion issue
Expected vs. actual results
I hope that the string array 'char' can be copied correctly
Minimal code example
Error messages
The text was updated successfully, but these errors were encountered: