Problem method:
template <typename T>
void SerializableScalar<T>::deserialize(const Json::Value& token)
{
if (isDeserilizationPossible(token)){
if (token.isInt())
{
myValue = token.asInt();
}
if (token.isDouble())
{
myValue = token.asDouble();
}
if (token.isString())
{
myValue = token.asString().c_str();
}
if (token.isBool())
{
myValue = token.asBool();
}
}
}
Where token can hold types string, int, double, bool.
and then i create object and use deserialize method
Json::Value token(55);
When i create object:
SerializableScalar<int> object;
object.deserialize(token);
I get compile errors because when I created object of T = int; in my deserialize method i cannot convert string to int even when json doesn't hold value of string , couse compiler inspect all branches.... And now i am asking you for advice. Is there any solution for this? I tried to overload deserialize method, its working but I don't want separate 4 methods, I am looking for something cleaner .
IsDeserilizationPossible method
template <typename T>
bool SerializableScalar<T>::isDeserilizationPossible(const Json::Value& token)
{
if (!token.isArray() && !token.isObject() && !token.empty())
{
myIsDeserialize = true;
return true;
}
throw std::exception("Some exception");
}
Additional info:
In cLass is just empty constructor and variable myValue holding type of T. point is deserialize Json, set value of type T. Client know the type he wants get so he use getMethod to get value of Serializable object; Thats it , i tried specialization already it works fine but i was just curious if is there something more i can use , so i don't need to overload method.
Errors: :
error C2440: '=' : cannot convert from 'const char *' to 'double'
error C2440: '=' : cannot convert from 'const char *' to 'int'