我正在尝试将从python客户端生成的MessagePack消息反序列化为.net对象。 DateTimes遇到了一些麻烦。有趣的是,停留在C#领域也不起作用。
Newtonsoft能够正确反序列化json。尽管MessagePack的ToJson返回有效的Json,但反序列化对象失败。
[DataContract]
public class TestClass
{
[DataMember]
public string TestPropertyA { get; set; }
[DataMember]
public string TestPropertyB { get; set; }
[DataMember]
public DateTime EventTimeStamp { get; set; }
}
//Move from Json string to an object
String jsonStringFromPython = "{\"TestPropertyA\":\"Hello\",\"TestPropertyB\":\"World\",\"EventTimeStamp\":\"2019-05-02T16:04:30.7812850Z\"}";
TestClass anObject = JsonConvert.DeserializeObject<TestClass>(jsonStringFromPython);
//Generate byte arrays for Messagepack
Byte[] arrFromAString = MessagePackSerializer.FromJson(jsonStringFromPython);
Byte[] arrFromAnObj = MessagePackSerializer.Serialize<TestClass>(anObject);
CompositeResolver.RegisterAndSetAsDefault(PrimitiveObjectResolver.Instance, ContractlessStandardResolver.Instance);
//Message pack understands the object-generated byte array
var backtoobject = MessagePackSerializer.Deserialize<TestClass>(arrFromAnObj);
//Message pack DOES NOT UNDERSTAND the string-generated byte array and fails with code is invalid. code:188 format:fixstr'
var backtoobject2 = MessagePackSerializer.Deserialize<TestClass>(arrFromAString);
上面的代码的最后一行失败,但是如果我从字符串/对象中删除DateTime属性,则所有代码都可以正常工作。