5

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());
13
  • 2
    Can you show the code you're using for the serialization and deserialization? Commented May 8, 2016 at 17:49
  • What is the full ToString() output of the exception including the exception type, message, traceback inside Json.NET and InnerException, if any? Commented May 8, 2016 at 17:55
  • 1
    I think this problem just occurs because I'm passing this JSON array as a command line parameter to a child process Commented May 8, 2016 at 19:15
  • 1
    That's probably it then. You didn't escape the \" character around the 0.1.335.5973 so Command Prompt ate it somehow, leading Json.NET to try to parse it as a numeric token. See superuser.com/questions/206436/…. Commented May 8, 2016 at 19:43
  • 2
    I think I will write the data as a file into temp folder and just pass the path to the file to the child process. Should be the easiest way Commented May 8, 2016 at 19:46

1 Answer 1

-1

I suggest that you should check your input, because following code will throw exception about invalid number:

JsonConvert.DeserializeObject<ErrorData>("{'AssemblyVersion':0.1.333.5973,'Exception':{}}");

But if you quote AssemblyVersion value, then deserialization will succeed:

JsonConvert.DeserializeObject<ErrorData>("{'AssemblyVersion':'0.1.333.5973','Exception':{}}");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.