【发布时间】:2016-05-08 17:40:09
【问题描述】:
我有这个数组:
{
"AssemblyVersion":"0.1.333.5973",
"Exception":
{
// [...]
}
}
从这个类序列化:
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);
}
}
将其序列化回对象会产生以下异常:
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)
简而言之,我使用重写的 ToString() 方法将 ErrorData 实例序列化为 json 字符串,将结果作为命令行参数传递给子进程,该子进程将其反序列化回 ErrorData 实例。
我错过了什么?似乎 AssemblyVersion 字符串被视为数字而不是字符串。
编辑:
我是这样反序列化的:
ErrorData errorData = JsonConvert.DeserializeObject<ErrorData>(args[0]);
上面的代码中已经显示了序列化,我在 ToString() 方法中实现了它。
我可以验证它是否发生,因为我将数组作为命令行参数传递。这段代码在这里有效:
ErrorData errorData = Json.DeserializeObject<ErrorData>(new ErrorData("", new Exception()).ToString());
【问题讨论】:
-
你能展示你用于序列化和反序列化的代码吗?
-
异常的完整
ToString()输出是什么,包括异常类型、消息、Json.NET 内部的回溯 和 InnerException(如果有)? -
我认为这个问题只是因为我将这个 JSON 数组作为命令行参数传递给子进程
-
大概就是这样。您没有转义
0.1.335.5973周围的 \" 字符,因此命令提示符以某种方式将其吃掉,导致 Json.NET 尝试将其解析为数字标记。请参阅 superuser.com/questions/206436/…。 -
我想我会将数据作为文件写入临时文件夹,然后将文件的路径传递给子进程。应该是最简单的方法