nlohmann::adl_serializer::from_json¶
// (1)
template<typenameBasicJsonType,typenameTargetType=ValueType>
staticautofrom_json(BasicJsonType&&j,TargetType&val)noexcept(
noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j),val)))
->decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j),val),void())
// (2)
template<typenameBasicJsonType,typenameTargetType=ValueType>
staticautofrom_json(BasicJsonType&&j)noexcept(
noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j),detail::identity_tag<TargetType>{})))
->decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j),detail::identity_tag<TargetType>{}))
This function is usually called by the get() function of the basic_json class (either explicitly or via the conversion operators).
- This function is chosen for default-constructible value types.
- This function is chosen for value types which are not default-constructible.
Parameters¶
j(in)- JSON value to read from
val(out)- value to write to
Return value¶
- (none) -- the converted value is written to the output parameter
val. - the JSON value
jconverted toTargetType
Examples¶
Example: (1) Default-constructible type
The example below shows how a from_json function can be implemented for a user-defined type. This function is called by the adl_serializer when get<ns::person>() is called.
#include<iostream>
#include<nlohmann/json.hpp>
usingjson=nlohmann::json;
namespacens
{
// a simple struct to model a person
structperson
{
std::stringname;
std::stringaddress;
intage;
};
}// namespace ns
namespacens
{
voidfrom_json(constjson&j,person&p)
{
j.at("name").get_to(p.name);
j.at("address").get_to(p.address);
j.at("age").get_to(p.age);
}
}// namespace ns
intmain()
{
jsonj;
j["name"]="Ned Flanders";
j["address"]="744 Evergreen Terrace";
j["age"]=60;
autop=j.get<ns::person>();
std::cout<<p.name<<" ("<<p.age<<") lives in "<<p.address<<std::endl;
}
Output:
NedFlanders(60)livesin744EvergreenTerrace
Example: (2) Non-default-constructible type
The example below shows how a from_json is implemented as part of a specialization of the adl_serializer to realize the conversion of a non-default-constructible type.
#include<iostream>
#include<nlohmann/json.hpp>
usingjson=nlohmann::json;
namespacens
{
// a simple struct to model a person (not default constructible)
structperson
{
person(std::stringn,std::stringa,intaa)
:name(std::move(n)),address(std::move(a)),age(aa)
{}
std::stringname;
std::stringaddress;
intage;
};
}// namespace ns
namespacenlohmann
{
template<>
structadl_serializer<ns::person>
{
staticns::personfrom_json(constjson&j)
{
return{j.at("name"),j.at("address"),j.at("age")};
}
// Here's the catch! You must provide a to_json method! Otherwise, you
// will not be able to convert person to json, since you fully
// specialized adl_serializer on that type
staticvoidto_json(json&j,ns::personp)
{
j["name"]=p.name;
j["address"]=p.address;
j["age"]=p.age;
}
};
}// namespace nlohmann
intmain()
{
jsonj;
j["name"]="Ned Flanders";
j["address"]="744 Evergreen Terrace";
j["age"]=60;
autop=j.get<ns::person>();
std::cout<<p.name<<" ("<<p.age<<") lives in "<<p.address<<std::endl;
}
Output:
NedFlanders(60)livesin744EvergreenTerrace
See also¶
Version history¶
- Added in version 2.1.0.