-
-
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
to_json / from_json with nested type #1648
Comments
Sorry, hit return while typing, I now updated the question. Let me check if I can build the unit test with my build environment. |
All tests are passing! |
I tested the release version as well, same behavior. |
The main issue was that you had to swap the #include "json.hpp"
#include <string>
#include <vector>
#include <fstream>
using nlohmann::json;
struct SignalConfiguration
{
std::string Name;
};
struct AppConfiguration
{
uint32_t Length;
std::vector<SignalConfiguration> SignalConfigurations;
};
void from_json(const json& J, SignalConfiguration& SC)
{
J.at("Name").get_to(SC.Name);
}
void from_json(const json& J, AppConfiguration& AC)
{
J.at("Length").get_to(AC.Length);
J.at("SignalConfigurations").get_to(AC.SignalConfigurations);
}
AppConfiguration AppConfig;
bool readConfigFile(std::string ConfigPath)
{
std::ifstream ConfigFile;
ConfigFile.open(ConfigPath);
if (!ConfigFile) {
return false;
}
json ConfigObj;
ConfigFile >> ConfigObj;
AppConfig = ConfigObj.get<AppConfiguration>();
return true;
}
int main()
{
readConfigFile("config.json");
} |
@btut Does #1648 (comment) fix the issue? |
|
I have add const,but still compile error
}; #endif // MAP_H |
The functions need to be inside the |
They are, I believe.
|
Ah, I missed an opening brace from the constructor. Reading code in unindented proportional fonts is hard. :)
|
Yeah, I cheated by formatting it in an editor. :-P |
I really wish GH had a "start of code block" markup instead of requiring 4 spaces at the start of each line. |
It does. 3 backticks on a single line to start and end a block. I never use leading spaces. And you can optionally specify the language for syntax highlighting: ```
```cpp struct foo {}; ```json {
"foo": 42
} |
Hmm, I thought I had tried that before. I use that all the time on Slack. Thanks for the reminder. |
Thanks! it compile ok,but convert error when field in json is null
};
};
};
}; } `void parseMap(string f){
}` { |
the above code throw error when run: How can set default value ,such as 0 or null when key is not exist in json . in this json key laserRD is not exist,I want set Node.laserRD=0 |
You'll have to check if the key exists first. See: if(j.contains("key")) j.at("key").get_to(key); (Remember to either default initialize your fields or add an |
Hi,
I have a struct that contains a vector of different structs and am unable to parse it using this library.
The readme lists an example for to_json and from_json function definitions, is such an example available in doc/examples? I would love to see some MWE I could adapt for this.
I tried to define to_json and from_json (I actually only need from_json so to_json is left blank) as in the readme, but could not get it to work:
`
#ifndef CONFIGURATION_H
#define CONFIGURATION_H
#include "json.hpp"
#include
using nlohmann::json;
struct SignalConfiguration {
std::string Name;
};
struct AppConfiguration {
uint32_t Length;
std::vector SignalConfigurations;
};
void to_json(json& J, const AppConfiguration& AC) {
//This direction is not needed
(void)J;
(void)AC;
}
void to_json(json& J, const SignalConfiguration& SC) {
//This direction is not needed
(void)J;
(void)SC;
}
void from_json(json& J, const AppConfiguration& AC) {
J.at("Length").get_to(AC.Length);
J.at("SignalConfigurations").get_to(AC.SignalConfigurations);
}
void from_json(json& J, const SignalConfiguration& SC) {
J.at("Name").get_to(SC.Name);
}
AppConfiguration AppConfig;
bool readConfigFile(std::string ConfigPath) {
std::ifstream ConfigFile;
ConfigFile.open(ConfigPath);
if (!ConfigFile) {
return false;
}
json ConfigObj;
ConfigFile >> ConfigObj;
AppConfig = ConfigFile;
return true;
}
#endif // CONFIGURATION_H
`
This yields a lot of errors:
In file included from main.cpp:1: ./configuration.h:32:34: error: no matching member function for call to 'get_to' J.at("SignalConfigurations").get_to(AC.SignalConfigurations); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~ ./json.hpp:15481:17: note: candidate template ignored: requirement 'detail::has_from_json<basic_json_t, const vector<SignalConfiguration, allocator<SignalConfiguration> > >::value' was not satisfied [with ValueType = const std::vector<SignalConfiguration, std::allocator<SignalConfiguration> >] ValueType & get_to(ValueType& v) const noexcept(noexcept( ^ ./json.hpp:15493:11: note: candidate template ignored: could not match 'T [N]' against 'const std::vector<SignalConfiguration>' Array get_to(T (&v)[N]) const ^ In file included from main.cpp:1: ./configuration.h:52:13: error: no viable overloaded '=' AppConfig = ConfigFile; ./configuration.h:13:8: note: candidate function (the implicit move assignment operator) not viable: no known conversion from 'std::ifstream' (aka 'basic_ifstream<char>') to 'AppConfiguration' for 1st argument struct AppConfiguration { ^ ./configuration.h:13:8: note: candidate function (the implicit copy assignment operator) not viable: no known conversion from 'std::ifstream' (aka 'basic_ifstream<char>') to 'const AppConfiguration' for 1st argument struct AppConfiguration { ^ In file included from main.cpp:1: In file included from ./configuration.h:4: ./json.hpp:1576:17: error: cannot assign to variable 'val' with const-qualified type 'const unsigned int &' val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>()); ~~~ ^ ./json.hpp:1665:16: note: in instantiation of function template specialization 'nlohmann::detail::from_json<nlohmann::basic_json<std::map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long, unsigned long, double, std::allocator, adl_serializer>, const unsigned int, 0>' requested here return from_json(j, val); ^ ./json.hpp:2222:9: note: in instantiation of function template specialization 'nlohmann::detail::from_json_fn::operator()<nlohmann::basic_json<std::map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long, unsigned long, double, std::allocator, adl_serializer>, const unsigned int>' requested here ::nlohmann::from_json(std::forward<BasicJsonType>(j), val); ^ ./json.hpp:15484:36: note: in instantiation of function template specialization 'nlohmann::adl_serializer<const unsigned int, void>::from_json<const nlohmann::basic_json<std::map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long, unsigned long, double, std::allocator, adl_serializer> &, const unsigned int>' requested here JSONSerializer<ValueType>::from_json(*this, v); ^ ./configuration.h:31:20: note: in instantiation of function template specialization 'nlohmann::basic_json<std::map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long, unsigned long, double, std::allocator, adl_serializer>::get_to<const unsigned int, 0>' requested here J.at("Length").get_to(AC.Length); ^ ./json.hpp:1570:56: note: variable 'val' declared const here void from_json(const BasicJsonType& j, ArithmeticType& val) ~~~~~~~~~~~~~~~~^~~ ./json.hpp:1581:17: error: cannot assign to variable 'val' with const-qualified type 'const unsigned int &' val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>()); ~~~ ^ ./json.hpp:1570:56: note: variable 'val' declared const here void from_json(const BasicJsonType& j, ArithmeticType& val) ~~~~~~~~~~~~~~~~^~~ ./json.hpp:1586:17: error: cannot assign to variable 'val' with const-qualified type 'const unsigned int &' val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>()); ~~~ ^ ./json.hpp:1570:56: note: variable 'val' declared const here void from_json(const BasicJsonType& j, ArithmeticType& val) ~~~~~~~~~~~~~~~~^~~ ./json.hpp:1591:17: error: cannot assign to variable 'val' with const-qualified type 'const unsigned int &' val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::boolean_t*>()); ~~~ ^ ./json.hpp:1570:56: note: variable 'val' declared const here void from_json(const BasicJsonType& j, ArithmeticType& val) ~~~~~~~~~~~~~~~~^~~ ./json.hpp:1388:7: error: no viable overloaded '=' s = *j.template get_ptr<const typename BasicJsonType::string_t*>(); ~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ./json.hpp:1665:16: note: in instantiation of function template specialization 'nlohmann::detail::from_json<nlohmann::basic_json<std::map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long, unsigned long, double, std::allocator, adl_serializer>, const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, 0>' requested here return from_json(j, val); ^ ./json.hpp:2222:9: note: in instantiation of function template specialization 'nlohmann::detail::from_json_fn::operator()<nlohmann::basic_json<std::map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long, unsigned long, double, std::allocator, adl_serializer>, const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >' requested here ::nlohmann::from_json(std::forward<BasicJsonType>(j), val); ^ ./json.hpp:15484:36: note: in instantiation of function template specialization 'nlohmann::adl_serializer<const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, void>::from_json<const nlohmann::basic_json<std::map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long, unsigned long, double, std::allocator, adl_serializer> &, const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >' requested here JSONSerializer<ValueType>::from_json(*this, v); ^ ./configuration.h:36:18: note: in instantiation of function template specialization 'nlohmann::basic_json<std::map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long, unsigned long, double, std::allocator, adl_serializer>::get_to<const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, 0>' requested here J.at("Name").get_to(SC.Name); ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/basic_string.h:664:7: note: candidate function not viable: 'this' argument has type 'const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >', but method is not marked const operator=(const basic_string& __str) ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/basic_string.h:809:8: note: candidate function not viable: 'this' argument has type 'const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >', but method is not marked const operator=(const _Tp& __svt) ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/basic_string.h:703:7: note: candidate function not viable: 'this' argument has type 'const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >', but method is not marked const operator=(const _CharT* __s) ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/basic_string.h:714:7: note: candidate function not viable: 'this' argument has type 'const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >', but method is not marked const operator=(_CharT __c) ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/basic_string.h:732:7: note: candidate function not viable: 'this' argument has type 'const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >', but method is not marked const operator=(basic_string&& __str) ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/basic_string.h:795:7: note: candidate function not viable: 'this' argument has type 'const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >', but method is not marked const operator=(initializer_list<_CharT> __l) ^ 7 errors generated.
I am using clang-6.0 on ubuntu 19.04 with C++17.
I am using the most recent single-header include (aa4c45e).
The text was updated successfully, but these errors were encountered: