dropbox / json11

A tiny JSON library for C++11.
MIT License
2.55k stars 613 forks source link

No suport for long datatype #82

Closed Shravan40 closed 7 years ago

Shravan40 commented 7 years ago

Hi

I am using json11 in my project. And when i am trying to return a std::vector of type long double, json11 does not support it.

vector<long double>nav =  select_from_fund_type(name);
json11::Json json(nav);
return crow::response(json.dump());

And here is the error i am receiving

error: no matching function for call to ‘json11::Json::Json(std::vector&)’ json11::Json json(nav); ^ In file included from main.cpp:6:0: /usr/local/include/json11.hpp:119:5: note: candidate: template<class V, typename std::enable_if<std::is_constructible<json11::Json, typename V::value_type>::value, int>::type > json11::Json::Json(const V&) Json(const V & v) : Json(array(v.begin(), v.end())) {} ^~~~ /usr/local/include/json11.hpp:119:5: note: template argument deduction/substitution failed: /usr/local/include/json11.hpp:118:26: error: no type named ‘type’ in ‘struct std::enable_if<false, int>’ int>::type = 0> ^ /usr/local/include/json11.hpp:118:26: note: invalid template non-type parameter /usr/local/include/json11.hpp:113:5: note: candidate: template<class M, typename std::enable_if<(std::is_constructible<std::basic_string, typename M::key_type>::value && std::is_constructible<json11::Json, typename M::mapped_type>::value), int>::type > json11::Json::Json(const M&) Json(const M & m) : Json(object(m.begin(), m.end())) {} ^~~~ /usr/local/include/json11.hpp:113:5: note: template argument deduction/substitution failed: /usr/local/include/json11.hpp:111:9: error: no type named ‘key_type’ in ‘class std::vector’ std::is_constructible<std::string, typename M::key_type>::value


         && std::is_constructible<Json, typename M::mapped_type>::value,
         ^~~~~~
/usr/local/include/json11.hpp:111:9: error: no type named ‘mapped_type’ in ‘class std::vector<long double>’
/usr/local/include/json11.hpp:112:26: note: invalid template non-type parameter
             int>::type = 0>
                          ^
/usr/local/include/json11.hpp:106:5: note: candidate: template<class T, class> json11::Json::Json(const T&)
     Json(const T & t) : Json(t.to_json()) {}
     ^~~~
/usr/local/include/json11.hpp:106:5: note:   template argument deduction/substitution failed:
/usr/local/include/json11.hpp:105:41: error: ‘to_json’ is not a member of ‘std::vector<long double>’
     template <class T, class = decltype(&T::to_json)>
artwyman commented 7 years ago

I think that's intentional. Since Json only supports double for storing numbers, it's not possible to convert "long double" -> Json without loss of precision, and thus we won't want to do it implicitly. Constructing a Json from a vector looks for T to be a type from which Json has a constructor, or a type which has a "to_json()" method, neither of which applies to long double. You need to do the static-cast yourself (probably in a loop) if you want to force the conversion.