Hi,
I was recently shown nlohmann::json and have found it to be very nice.
I have a query surrounding the necessity of from_json / to_json converters to be in a data structure's namespace. Primarily I was wondering what the benefit was of this decision? Below I've illustrated the consequences in an example that cannot easily be addressed with this constraint.
Example
Pulling share trading data from two web portals that publish in JSON. There is a common struct used in more agnostic code:
namespace shares
{
struct price_t
{
uint64_t time;
double open;
double high;
double low;
double close;
uint64_t volume;
};
}
One site published this data in JSON as an object:
{
"time": xxxx,
"open": xxxx,
"high": xxxx,
"low": xxxx,
"close": xxxx,
"volume": xxxx
}
The second as an array:
[ xxxx, xxxx, xxxx, xxxx, xxxx, xxxx ]
Given that I must define from_json in namespace shares, I cannot conveniently define multiple from_json converters, one for each schema.
Ideally I'd like to define the following:
namespace schema_1
{
void from_json( const nlohmann::json & value, shares::price_t & price )
{
price.time = value[ "time" ];
price.open = value[ "open" ];
price.high = value[ "high" ];
price.low = value[ "low" ];
price.close = value[ "close" ];
price.volume = value[ "volume" ];
}
}
And:
namespace schema_2
{
void from_json( const nlohmann::json & value, shares::price_t & price )
{
price.time = value[ 0 ];
price.open = value[ 1 ];
price.high = value[ 2 ];
price.low = value[ 3 ];
price.close = value[ 4 ];
price.volume = value[ 5 ];
}
}
Then use them like this:
shares::price_t read_schema_1( const std::string & url )
{
using schema_1::from_json;
return curl.read_json( url );
}
And:
shares::price_t read_schema_2( const std::string & url )
{
using schema_2::from_json;
return curl.read_json( url );
}
In general the necessity to place from_json and to_json in the the namespace of the data structure couples that data structure too tightly to an externally defined JSON schema. This results in negatively limiting its flexibility.
Unless there is some compelling reason for binding these conversion routines in this way, I'd suggest that this limitation be removed.
Hi,
I was recently shown nlohmann::json and have found it to be very nice.
I have a query surrounding the necessity of from_json / to_json converters to be in a data structure's namespace. Primarily I was wondering what the benefit was of this decision? Below I've illustrated the consequences in an example that cannot easily be addressed with this constraint.
Example
Pulling share trading data from two web portals that publish in JSON. There is a common struct used in more agnostic code:
namespace shares { struct price_t { uint64_t time; double open; double high; double low; double close; uint64_t volume; }; }One site published this data in JSON as an object:
{ "time": xxxx, "open": xxxx, "high": xxxx, "low": xxxx, "close": xxxx, "volume": xxxx }The second as an array:
Given that I must define from_json in namespace shares, I cannot conveniently define multiple from_json converters, one for each schema.
Ideally I'd like to define the following:
namespace schema_1 { void from_json( const nlohmann::json & value, shares::price_t & price ) { price.time = value[ "time" ]; price.open = value[ "open" ]; price.high = value[ "high" ]; price.low = value[ "low" ]; price.close = value[ "close" ]; price.volume = value[ "volume" ]; } }And:
namespace schema_2 { void from_json( const nlohmann::json & value, shares::price_t & price ) { price.time = value[ 0 ]; price.open = value[ 1 ]; price.high = value[ 2 ]; price.low = value[ 3 ]; price.close = value[ 4 ]; price.volume = value[ 5 ]; } }Then use them like this:
shares::price_t read_schema_1( const std::string & url ) { using schema_1::from_json; return curl.read_json( url ); }And:
shares::price_t read_schema_2( const std::string & url ) { using schema_2::from_json; return curl.read_json( url ); }In general the necessity to place from_json and to_json in the the namespace of the data structure couples that data structure too tightly to an externally defined JSON schema. This results in negatively limiting its flexibility.
Unless there is some compelling reason for binding these conversion routines in this way, I'd suggest that this limitation be removed.