kazuho / picojson

a header-file-only, JSON parser serializer in C++

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

consider exposing value::_type or provide a value::_type to std::string utility

weegreenblobbie opened this issue · comments

commented

I'm passing around some picojson objects/values and wanted to assert on type. As part of my message, I wanted to dump the type of value I got:

M_ASSERT_MSG(
    input.is<picojson::object>(),
     "Expecting a JSON object, got " << to_type_string(input)
);

As a workaround, I've written this in my code to do it:

std::string
to_type_string(const picojson::value & v)
{
    if(v.is<bool>()) return "bool";
    else
    if(v.is<picojson::null>()) return "null";
    else
    if(v.is<int64_t>()) return "int";
    else
    if(v.is<float64>()) return "float";
    else
    if(v.is<std::string>()) return "string";
    else
    if(v.is<picojson::array>()) return "array";
    else
    if(v.is<picojson::object>()) return "object";
    else
    if(v.is<picojson::object>()) return "object";
    return "unknown";
}