0
votes

Need to find if an array exists in an json objects using taocpp-json. It is easy to use optional for finding ints and boolean etc., but with if the value is an array, I cannot find out how to do it.

I tried:

v.optional<std::vector<tao::json::value>>("fields");

But this gives me compiler error!

On msvc: C:\PROGRA~2\MICROS~1\2017\PROFES~1\VC\Tools\MSVC\1416~1.270\bin\Hostx64\x64\cl.exe /nologo /TP -IC:\devel\cpp\vcpkg\installed\x64-windows\include /DWIN32 /D_WINDOWS /GR /EHsc /O2 /Ob2 /DNDEBUG -MD -std:c++17 /showIncludes /Fosrc\CMakeFiles\tabgen.dir\file_manager.cpp.obj /Fdsrc\CMakeFiles\tabgen.dir\ /FS -c ..\src\file_manager.cpp c:\devel\cpp\vcpkg\installed\x64-windows\include\tao\json\basic_value.hpp(1308): error C2440: 'return': cannot convert from 'void' to 'std::optional,std::allocator<_Ty>>>' with [ _Ty=tao::json::basic_value ] c:\devel\cpp\vcpkg\installed\x64-windows\include\tao\json\basic_value.hpp(1308): note: Expressions of type void cannot be converted to other types ..\src\file_manager.cpp(24): note: see reference to function template instantiation 'std::optional,std::allocator<_Ty>>> tao::json::basic_value::optional>,char[7]>(const K (&)) const' being compiled with [ _Ty=tao::json::basic_value, K=char [7] ] ..\src\file_manager.cpp(24): note: see reference to function template instantiation 'std::optional,std::allocator<_Ty>>> tao::json::basic_value::optional>,char[7]>(const K (&)) const' being compiled with [ _Ty=tao::json::basic_value, K=char [7] ] c:\devel\cpp\vcpkg\installed\x64-windows\include\tao\json\events/virtual_base.hpp(69): note: see reference to class template instantiation 'tao::basic_binary_view' being compiled C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\filesystem(2392): note: see reference to class template instantiation 'std::chrono::time_point' being compiled C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\type_traits(616): note: see reference to class template instantiation 'std::basic_string_view>' being compiled C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\xstring(2124): note: see reference to class template instantiation 'std::is_convertible>>' being compiled with [ _StringViewIsh=const wchar_t * ] C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\xstring(2122): note: see reference to variable template 'const bool conjunction_v > >,std::negation > >' being compiled C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\xstring(2281): note: see reference to alias template instantiation 'std::basic_string,std::allocator>::_Is_string_view_ish<_StringViewIsh>' being compiled with [ _StringViewIsh=const wchar_t * ]

Do you know how to check if a key exists that has a value of type array?

1
why don't you show the error, it probably contains useful information;-)AshleyWilkes

1 Answers

1
votes

Found a different way to do this, using object_t:

tao::json::value v = tao::json::parse_file("..\\json.json");
std::map<std::string, tao::json::value, std::less<>> o = v.get_object();

{
auto i = o.find("name");
if(i != std::end(o))
{
    std::cout << i->second.get_string();
}

auto i = o.find("NOT_FOUND");
if(i != std::end(o))
{
    std::cout << i->second.get_string();
}
else
{
    std::cout << "NOT_FOUND";
}
}
return 0;

This works find and will be a clean way to find if an object is missing or not.