为什么System.Version在JSON字符串不正确反序列化? [英] Why System.Version in JSON string does not deserialize correctly?
问题描述
上下文:我需要通过包含大量的属性/字段(以UI层从中间层层)的对象。在属性列表,一个是没有得到从JSON字符串格式正确类型的反序列化版本。我有一个选择的JSON格式基于XML的JSON序列化字符串将返回短字符串的结果。
Context: I need to pass an object containing a large number of properties/fields (to UI Layer from Middle Tier Layer). Among this list of properties, one is of type Version which is not getting deserialized correctly from JSON string format. I have a chosen JSON format over XML as JSON serialization to string will return short string result.
问题:
System.Version不能正确反序列化。我已经尝试了两种不同的.NET库。以下是各代码段:
Problem: System.Version does not get deserialized correctly. I have tried two different .NET Libraries. Following are the code snippets for each:
代码片段1 ServiceStack .NET库:
Code Snippet 1 using ServiceStack .NET library:
var version = new Version(1, 2, 3, 0);
string reportJSON = JsonSerializer.SerializeToString<Version>(version);
//{"Major":1,"Minor":2,"Build":3,"Revision":0,"MajorRevision":0,"MinorRevision":0}
Version report2 = JsonSerializer.DeserializeFromString<Version>(reportJSON);
string reportJSON2 = JsonSerializer.SerializeToString<Version>(report2);
//{"Major":0,"Minor":0,"Build":-1,"Revision":-1,"MajorRevision":-1,"MinorRevision":-1}
代码段2使用的 Newtonsoft .NET库但同样的结果:
Code Snippet 2 using Newtonsoft .NET library but with same result:
var version = new Version(1, 2, 3, 0);
string reportJSON = JsonConvert.SerializeObject(version);
//{"Major":1,"Minor":2,"Build":3,"Revision":0,"MajorRevision":0,"MinorRevision":0}
Version report2 = JsonConvert.DeserializeObject<Version>(reportJSON);
string reportJSON2 = JsonConvert.SerializeObject(report2);
//{"Major":0,"Minor":0,"Build":-1,"Revision":-1,"MajorRevision":-1,"MinorRevision":-1}
如何解决这一问题?或其他JSON.NET库可以正常工作?
How to fix this? Or Which other JSON.NET library can work correctly?
推荐答案
的 Newtonsoft.Json
库提供了一组在 Newtonsoft.Json.Converters
命名空间的常见转换器,其中的 VersionConverter
您可以使用序列化和反序列化 System.Version
。
The Newtonsoft.Json
library provides a set of common converters in the Newtonsoft.Json.Converters
namespace, including a VersionConverter
you can use to serialize and deserialize System.Version
.
注意,你必须使用 VersionConverter
无论是序列化和反序列化,虽然 。结果
这是因为标准序列化会产生eg.:{\"Major\":1,\"Minor\":2,\"Build\":3,\"Revision\":0,\"MajorRevision 0,MinorRevision:0}
,而 VersionConverter
反序列化需要一个简单的字符串作为1.2.3
Note that you have to use the VersionConverter
both for serialization and deserialization, though.
That's because standard serialization would generate eg.:{"Major":1,"Minor":2,"Build":3,"Revision":0,"MajorRevision":0,"MinorRevision":0}
while VersionConverter
deserialization expects a simple string as in "1.2.3"
.
所以,用法是:
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
string s = JsonConvert.SerializeObject(version, new VersionConverter());
Version v = JsonConvert.DeserializeObject<Version>(s, new VersionConverter());
我不知道什么是第一个版本的 Newtonsoft.Json
,它包含转换器。该矿拥有它,它是5.0.6。
I'm not sure what's the first version of Newtonsoft.Json
that includes that converter. Mine has it and it's 5.0.6.
这篇关于为什么System.Version在JSON字符串不正确反序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!