I have this array:
{
"AssemblyVersion":"0.1.333.5973",
"Exception":
{
// [...]
}
}
which is serialized from this class:
public class ErrorData
{
public string AssemblyVersion { get; private set; }
public Exception Exception { get; private set; }
public ErrorData(string assemblyVersion, Exception ex)
{
AssemblyVersion = assemblyVersion;
Exception = ex;
}
public override string ToString()
{
return JsonConvert.SerializeObject(this);
}
}
Serializing it back to an object produces the following exception:
Newtonsoft.Json.JsonReaderException: Input string '0.1.335.5973' is not a valid number. Path 'AssemblyVersion', line 1, position 29.
at Newtonsoft.Json.JsonTextReader.ParseNumber(ReadType readType)
at Newtonsoft.Json.JsonTextReader.ReadStringValue(ReadType readType)
at Newtonsoft.Json.JsonTextReader.ReadAsString()
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader, JsonContract contract, Boolean hasConverter)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ResolvePropertyAndCreatorValues(JsonObjectContract contract, JsonProperty containerProperty, JsonReader reader, Type objectType)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObjectUsingCreatorWithParameters(JsonReader reader, JsonObjectContract contract, JsonProperty containerProperty, ObjectConstructor`1 creator, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewObject(JsonReader reader, JsonObjectContract objectContract, JsonProperty containerMember, JsonProperty containerProperty, String id, Boolean& createdFromNonDefaultCreator)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)
In a nutshell I'm serializing an ErrorData instance as a json string with the overridden ToString()
method, passing the result as a command line argument to a child process which is deserializing it back to an ErrorData instance.
What am I missing? It seems like the AssemblyVersion string is seen as a number instead of a string.
Edit:
I'm deserializing it this way:
ErrorData errorData = JsonConvert.DeserializeObject<ErrorData>(args[0]);
Serializing is already shown in the code above, I implemented it into ToString() method.
I can verify that it happens because I'm passing the array as a command line arg. This code here works:
ErrorData errorData = Json.DeserializeObject<ErrorData>(new ErrorData("", new Exception()).ToString());