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
Is it possible to add a state based serialization interface, where I can use the same interface of writing values independent whether it is an array or a normal json based value?
The current problem I have is a code duplication because of the current interface:
boolwrite_atomic_types_to_json(nlohmann::json& j, std::string name, const variant& var, const type& t)
{
if (t.is_arithmetic())
{
if (t == type::get<bool>())
j[name] = var.to_bool();
elseif (t == type::get<char>())
j[name] = var.to_bool();
elseif (t == type::get<int8_t>())
j[name] = var.to_int8();
elseif (t == type::get<int16_t>())
j[name] = var.to_int16();
//.... more code to followreturntrue;
}
When I do now the same for an array, I cannot reuse the code from above, I have to use push_back, to insert the values.
boolwrite_atomic_types_to_json_array(nlohmann::json& j, const variant& var, const type& t)
{
if (t.is_arithmetic())
{
if (t == type::get<bool>())
j.push_back(var.to_bool());
elseif (t == type::get<char>())
j.push_back(var.to_bool());
elseif (t == type::get<int8_t>())
j.push_back(var.to_int8());
elseif (t == type::get<int16_t>())
j.push_back(var.to_int16());
//... more code to followreturntrue;
}
There are two different interfaces for writing a value in JSON, this is okay. But for my use case
it would be nice to have something like this:
You see the writing of key and the value doesn't distinguish between an array or a normal key-value based json object. I can reuse the same writing routines. Does this make sense?
I am new to your library, maybe there is a workaround to get what I need with the current interface.
The text was updated successfully, but these errors were encountered:
As I described earlier, you can implement such functions with the public API. I do not think that adding theses functions to the library is a good idea as they cover a very special usecase. As there was no discussion so far, I decided to close the issue.
Hi Nils,
Is it possible to add a state based serialization interface, where I can use the same interface of writing values independent whether it is an array or a normal json based value?
The current problem I have is a code duplication because of the current interface:
When I do now the same for an array, I cannot reuse the code from above, I have to use
push_back
, to insert the values.There are two different interfaces for writing a value in JSON, this is okay. But for my use case
it would be nice to have something like this:
Will result in following:
That code snippet is actually from rapid-json.
You see the writing of key and the value doesn't distinguish between an array or a normal key-value based json object. I can reuse the same writing routines. Does this make sense?
I am new to your library, maybe there is a workaround to get what I need with the current interface.
The text was updated successfully, but these errors were encountered: