我正在使用JSON.net,我正在尝试序列化和反序列化Distance object from the opensource UnitClassLibrary。目前,我有一个序列化为以下JSON的对象:
{
"ThirtySecondsOfAnInch": 454,
"SixteenthsOfAnInch": 227,
"EighthsOfAnInch": 113.5,
"QuartersOfAnInch": 56.75,
"HalvesOfAnInch": 28.375,
"Inches": 14.1875,
"Feet": 1.1822916666666667,
"Yards": 0.3940972222222222,
"Miles": 0.00022391887626262627,
"Millimeters": 360.36249999999995,
"Centimeters": 36.03625,
"Meters": 0.3603625,
"Kilometers": 0.0003603625,
"Architectural": "1'2 3/16\""
}
我可以使用此类中的任何一个并将其转换为距离对象。例如,使用最后一个架构字符串,我可以使用此构造函数:
Distance newDistance = new Distance("1'2 3/16\"");
或者,使用32英寸,我可以这样做:
Distance newDistance = new Distance(DistanceType.ThirtySecond, 454.0);
但是,我不确定编写JsonConstructor(JSON.net可以专门使用的构造函数的类型)在传递带有JsonConvert.DeserializeObject<Distance>(json);
的JSON字符串时获取任何类型的输出的最佳方法。
如何编写构造函数来获取新的距离对象?
答案 0 :(得分:1)
这是一个让你入门的JsonConverter示例。
public class DistanceConverter : JsonConverter
{
private readonly IDictionary<string, DistanceType> _distanceTypeMap;
public DistanceConverter()
{
_distanceTypeMap = new Dictionary<string, DistanceType>
{
{"Meters", DistanceType.Meter},
{"Yards", DistanceType.Yard}
};
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
Distance distance = value as Distance;
if (distance == null)
{
writer.WriteNull();
return;
}
writer.WriteStartObject();
foreach (KeyValuePair<string, DistanceType> pair in _distanceTypeMap)
{
writer.WritePropertyName(pair.Key);
writer.WriteValue(distance.GetValue(pair.Value));
}
writer.WriteEndObject();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
Distance result = null;
while (reader.Read())
{
var key = reader.Value;
string value = reader.ReadAsString();
if (result == null && key != null)
{
DistanceType distanceType;
if (_distanceTypeMap.TryGetValue(key.ToString(), out distanceType))
{
double parsedValue = JToken.Parse(value).Value<double>();
result = new Distance(distanceType, parsedValue);
}
}
}
return result;
}
public override bool CanConvert(Type objectType)
{
return typeof(Distance) == objectType;
}
}
目前这有问题。即使我在反序列化中创建对象,我也无法理解为什么我会得到零距离。以下是一些测试结果:
更新:我在再次阅读时修复了错误。它目前有效,但是当您将其他单位添加到字典中时,您可能会遇到异常。
public class DistanceConverterTests
{
private JsonSerializerSettings _jsonSerializerSettings;
[SetUp]
public void Setup()
{
_jsonSerializerSettings = new JsonSerializerSettings
{
Converters = new List<JsonConverter> { new DistanceConverter() }
};
}
[Test]
public void DeserializeTest()
{
string json = File.ReadAllText("data.json");
var distance = JsonConvert.DeserializeObject<Distance>(json, _jsonSerializerSettings);
distance.Meters.Should().BeInRange(0.360, 0.361);
}
[Test]
public void SerializeTest()
{
Distance distance = new Distance(DistanceType.Meter, 2.20);
string json = JsonConvert.SerializeObject(distance, _jsonSerializerSettings);
Console.WriteLine(json);
}
}