gpt4 book ai didi

c# - 使用 Newtonsoft Json.Net 反序列化为 IEnumerable 类

转载 作者:IT王子 更新时间:2023-10-29 04:38:31 26 4
gpt4 key购买 nike

我有一个项目目前正在使用 Json.Net 来处理像这样的 Json 反序列化类:

public class Foo {
public Guid FooGuid { get; set; }
public string Name { get; set; }
public List<Bar> Bars { get; set; }
}

public class Bar {
public Guid BarGuid { get; set; }
public string Description { get; set; }
}

到目前为止一切正常。

为了让迭代更简单,我做了 Foo类(class)工具IEnumerable<Bar>像这样:

public class Foo : IEnumerable<Bar> {
public Guid FooGuid { get; set; }
public string Name { get; set; }
public List<Bar> Bars { get; set; }

public IEnumerator<Bar> GetEnumerator()
{
return Bars.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}

public class Bar {
public Guid BarGuid { get; set; }
public string Description { get; set; }
}

但是它无法反序列化对象。该错误令人困惑,因为它说它无法反序列化 FooGuid字段,但删除了 IEnumerable界面再次工作。

在模拟器或设备的 MonoTouch 和 MonoDroid 环境中,问题是相同的。

关于如何让它发挥作用的任何线索?


添加代码以重现此问题:

public static class Tests {
public static void SerializeAndDeserializeFoo() {
// Serialize and deserialize Foo class
var element = new Foo
{
FooGuid = Guid.NewGuid(),
Name = "Foo name",
Bars = new List<Bar> {
new Bar { BarGuid = Guid.NewGuid(), Description = "Bar description" },
new Bar { BarGuid = Guid.NewGuid(), Description = "Bar description" },
new Bar { BarGuid = Guid.NewGuid(), Description = "Bar description" }
}
};

var serializedObject = JsonConvert.SerializeObject(element);
Console.WriteLine("Serialized Foo element: {0}", serializedObject);

// Exception if Foo implements IEnumerable
var deserializedObject = JsonConvert.DeserializeObject<Foo>(serializedObject);
Console.WriteLine("Foo deserialization worked!");
}

public static void SerializeAndDeserializeEnumerableFoo() {
// Serialize and deserialize Foo class
var element = new EnumerableFoo
{
FooGuid = Guid.NewGuid(),
Name = "Foo name",
Bars = new List<Bar> {
new Bar { BarGuid = Guid.NewGuid(), Description = "Bar description" },
new Bar { BarGuid = Guid.NewGuid(), Description = "Bar description" },
new Bar { BarGuid = Guid.NewGuid(), Description = "Bar description" }
}
};

var serializedObject = JsonConvert.SerializeObject(element);
Console.WriteLine("Serialized EnumerableFoo element: {0}", serializedObject);

try {
// Exception if Foo implements IEnumerable
var deserializedObject = JsonConvert.DeserializeObject<EnumerableFoo>(serializedObject);
Console.WriteLine("EnumerableFoo deserialization worked!");
}
catch (Exception e){
Console.WriteLine("EnumerableFoo deserialization failed!");
throw;
}
}
}

public class EnumerableFoo : IEnumerable<Bar> {
public Guid FooGuid { get; set; }
public string Name { get; set; }
public List<Bar> Bars { get; set; }

public IEnumerator<Bar> GetEnumerator()
{
return Bars.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}

public class Foo {
public Guid FooGuid { get; set; }
public string Name { get; set; }
public List<Bar> Bars { get; set; }
}

public class Bar {
public Guid BarGuid { get; set; }
public string Description { get; set; }
}

示例项目:https://www.dropbox.com/s/27i58aiz71dylkw/IEnumerableJson.zip

最佳答案

来自Json.Net documentation :

IEnumerable, Lists and Arrays

.NET lists (types that inherit from IEnumerable) and .NET arrays are converted to JSON arrays. Because JSON arrays only support a range of values and not properties, any additional properties and fields declared on .NET collections are not serialized. In situations where a JSON array is not wanted the JsonObjectAttribute can be placed on a .NET type that implements IEnumerable to force the type to be serialized as a JSON object instead.

换句话说,因为你的类实现了IEnumerable<T> , Json.Net认为是一个列表。为了解决这个问题,只需用 [JsonObject] 装饰你的类(class)属性。这将强制 Json.Net 将其序列化和反序列化为普通类,这正是您在本例中想要的。

[JsonObject]
public class EnumerableFoo : IEnumerable<Bar>
{
...
}

关于c# - 使用 Newtonsoft Json.Net 反序列化为 IEnumerable 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19080326/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com