I want to write a function which deserialise a json representation of array into a std::vector. The json library which i am using is part of Facebook's folly library. I would like to achieve somethings like the following, but unfortunately it's not working:
template<typename T>
static bool deserializeHelper(std::string fieldName, vector< T >& structField, const folly::dynamic& jsonObj) {
if(auto* jsonField = jsonObj.get_ptr(fieldName)){
if(jsonField->isArray()) {
for(auto& elem : *jsonField) {
if(elem.isInt()) {
structField.push_back(elem.asInt());
} else if(elem.isString()){
structField.push_back(elem.asString());
} else if(elem.isDouble()) {
structField.push_back(elem.asDouble());
} else if(elem.isBool()) {
structField.push_back(elem.asBool());
} else return false;
}
} else return false;
}
return true;
}
In the above code, jsonField is a representation of array field. So the code just try to loop through the array; then for each element; it will try to push back to generic vector: vector. The problem is that the code cannot be compiled because it will complain that it cannot cast from std::string to int; when T=int;
I am not sure how to write a generic function like that without the need of implement 4 method overloading functions. static bool deserializeHelper(std::string fieldName, vector< int >& structField, const folly::dynamic& jsonObj) ....
Thanks.