gpt4 book ai didi

c# - Json.Net:如何忽略反序列化 JSON 的数组中的空元素

转载 作者:行者123 更新时间:2023-12-01 23:54:51 27 4
gpt4 key购买 nike

我有这个 JSON:

{
"Variable1": "1",
"Variable2": "50000",
"ArrayObject": [null]
}

我有这个 stub :

public class Class1
{
public string Variable1 { get; set; }
public string Variable2 { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public List<ArrayObject> ArrayObject { get; set; }
}

public class ArrayObject
{
public string VariableArray1 { get; set; }
public string VariableArray2 { get; set; }
}

我想忽略数组中的空元素,最好使用 json 设置或某种转换器。因此在这种情况下结果应该是一个空数组或 null。

这是我一直在努力实现的代码。

class Program
{
static void Main(string[] args)
{
string json = @"{
""Variable1"": ""1"",
""Variable2"": ""50000"",
""ArrayObject"": [null]
}";

var settings = new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore,
};

Class1 class1 = JsonConvert.DeserializeObject<Class1>(json, settings);

Console.WriteLine(class1.ArrayObject == null);
Console.WriteLine(class1.ArrayObject.Count());

foreach (var item in class1.ArrayObject)
{
Console.WriteLine(item.VariableArray1);
Console.WriteLine(item.VariableArray2);
Console.WriteLine("#######################");
}
}

public class Class1
{
public string Variable1 { get; set; }
public string Variable2 { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public List<ArrayObject> ArrayObject { get; set; }
}

public class ArrayObject
{
public string VariableArray1 { get; set; }
public string VariableArray2 { get; set; }
}
}

我认为使用 NullValueHandling = NullValueHandling.Ignore 可以让它工作。显然不是。有什么想法吗?

更新:我需要一个全局解决方案,我不想修改项目中的每个 View 模型。

最佳答案

设置NullValueHandling = NullValueHandling.Ignore在反序列化过程中不会自动过滤 JSON 数组中的空值,因为这样做会导致数组中的剩余项被重新索引,从而使可能存储在序列化图中其他位置的任何数组索引无效.

如果您不关心保留数组索引并希望在反序列化过程中从数组中过滤空值,您将需要实现一个 custom JsonConverter 例如:

public class NullFilteringListConverter<T> : JsonConverter<List<T>>
{
public override List<T> ReadJson(JsonReader reader, Type objectType, List<T> existingValue, bool hasExistingValue, JsonSerializer serializer)
{
if (reader.MoveToContentAndAssert().TokenType == JsonToken.Null)
return null;
var list = existingValue as List<T> ?? (List<T>)serializer.ContractResolver.ResolveContract(objectType).DefaultCreator();
serializer.Populate(reader, list);
list.RemoveAll(i => i == null);
return list;
}

public override bool CanWrite => false;
public override void WriteJson(JsonWriter writer, List<T> value, JsonSerializer serializer) => throw new NotImplementedException();
}

public static partial class JsonExtensions
{
public static JsonReader MoveToContentAndAssert(this JsonReader reader)
{
if (reader == null)
throw new ArgumentNullException();
if (reader.TokenType == JsonToken.None) // Skip past beginning of stream.
reader.ReadAndAssert();
while (reader.TokenType == JsonToken.Comment) // Skip past comments.
reader.ReadAndAssert();
return reader;
}

public static JsonReader ReadAndAssert(this JsonReader reader)
{
if (reader == null)
throw new ArgumentNullException();
if (!reader.Read())
throw new JsonReaderException("Unexpected end of JSON stream.");
return reader;
}
}

并将其应用到您的模型中,如下所示:

public class Class1
{
public string Variable1 { get; set; }
public string Variable2 { get; set; }
[JsonConverter(typeof(NullFilteringListConverter<ArrayObject>))]
public List<ArrayObject> ArrayObject { get; set; }
}

或者,在设置中添加如下:

var settings = new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore,
Converters = { new NullFilteringListConverter<ArrayObject>() },
};

注意事项:

  • 由于您没有询问在序列化过程中过滤空值,所以我没有实现它,但是通过更改 CanWrite => true; 很容易实现并替换 WriteJson()与:

     public override void WriteJson(JsonWriter writer, List<T> value, JsonSerializer serializer) => serializer.Serialize(writer, value.Where(i => i != null));

演示 fiddle herehere .

更新

我需要一个全局的解决方案。如果你需要自动过滤所有null所有可能的值 List<T>每个模型中的对象,以下JsonConverter将完成这项工作:

public class NullFilteringListConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
if (objectType.IsArray || objectType == typeof(string) || objectType.IsPrimitive)
return false;
var itemType = objectType.GetListItemType();
return itemType != null && (!itemType.IsValueType || Nullable.GetUnderlyingType(itemType) != null);
}

object ReadJsonGeneric<T>(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var list = existingValue as List<T> ?? (List<T>)serializer.ContractResolver.ResolveContract(objectType).DefaultCreator();
serializer.Populate(reader, list);
list.RemoveAll(i => i == null);
return list;
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.MoveToContentAndAssert().TokenType == JsonToken.Null)
return null;
var itemType = objectType.GetListItemType();
var method = typeof(NullFilteringListConverter).GetMethod("ReadJsonGeneric", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
try
{
return method.MakeGenericMethod(new[] { itemType }).Invoke(this, new object[] { reader, objectType, existingValue, serializer });
}
catch (Exception ex)
{
// Wrap the TargetInvocationException in a JsonSerializerException
throw new JsonSerializationException("Failed to deserialize " + objectType, ex);
}
}

public override bool CanWrite => false;
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) => throw new NotImplementedException();
}

public static partial class JsonExtensions
{
internal static Type GetListItemType(this Type type)
{
// Quick reject for performance
if (type.IsPrimitive || type.IsArray || type == typeof(string))
return null;
while (type != null)
{
if (type.IsGenericType)
{
var genType = type.GetGenericTypeDefinition();
if (genType == typeof(List<>))
return type.GetGenericArguments()[0];
}
type = type.BaseType;
}
return null;
}
}

并将其添加到设置中,如下所示:

var settings = new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore,
Converters = { new NullFilteringListConverter() },
};

Class1 class1 = JsonConvert.DeserializeObject<Class1>(json, settings);

使用此转换器,添加 [JsonConverter(typeof(NullFilteringListConverter<ArrayObject>))]ArrayObject不再需要。请注意 所有 List<T>每当使用这些设置时,反序列化图中的实例可能会重新编制索引!确保您真的想要这样做,因为更改索引在其他地方引用的项目的索引的副作用可能包括数据损坏(不正确的引用)而不是比直接ArgumentOutOfRangeException .

演示 fiddle #3 here .

关于c# - Json.Net:如何忽略反序列化 JSON 的数组中的空元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62940751/

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