在Json.NET序列化中有没有办法区分" null因为不存在"和" null因为null"?

时间:2015-03-13 03:12:52

标签: c# asp.net json asp.net-web-api json.net

我正在使用ASP.NET webapi代码库,我们在很大程度上依赖于通过JSON.NET自动支持将消息体的JSON反序列化为.NET对象。

作为构建我们其中一个资源的补丁支持的一部分,我非常希望区分JSON对象中不存在的可选属性与明确为null的相同属性。我的意图是使用第一个“不要改变那里有什么”而不是“删除这个东西。”

有没有人知道是否可以标记我的C#DTO,以便在反序列化时JSON.NET可以告诉我它是哪种情况?现在他们只是空出来,我不知道为什么。

相反,如果有人能够提出一个更好的设计,不需要我这样做,同时仍然支持补丁动词,我很乐意听取你的建议。

作为具体示例,请考虑将传递给put的这个有效负载:

{
  "field1": "my field 1",
  "nested": {
    "nested1": "something",
    "nested2": "else"
  }
}

现在,如果我只想更新field1,我应该能够将其作为HTTP补丁发送:

{
  "field1": "new field1 value"
}

并且嵌套值将保持不变。但是,如果我发送了这个:

{
  "nested": null
}

我想知道这意味着我应该明确删除嵌套数据。

5 个答案:

答案 0 :(得分:16)

如果你使用Json.Net的LINQ-to-JSON API(JTokens,JObjects等)来解析JSON,你可以区分null值和一个简单的字段。 #39; t存在于JSON中。例如:

JToken root = JToken.Parse(json);

JToken nested = root["nested"];
if (nested != null)
{
    if (nested.Type == JTokenType.Null)
    {
        Console.WriteLine("nested is set to null");
    }
    else
    {
        Console.WriteLine("nested has a value: " + nested.ToString());
    }
}
else
{
    Console.WriteLine("nested does not exist");
}

小提琴:https://dotnetfiddle.net/VJO7ay

<强>更新

如果您使用Web API反序列化为具体对象,您仍然可以通过创建自定义JsonConverter来处理您的DTO,从而使用上述概念。问题是,在反序列化期间,您的DTO上需要有一个位置来存储字段状态。我建议使用这样一个基于字典的方案:

enum FieldDeserializationStatus { WasNotPresent, WasSetToNull, HasValue }

interface IHasFieldStatus
{
    Dictionary<string, FieldDeserializationStatus> FieldStatus { get; set; }
}

class FooDTO : IHasFieldStatus
{
    public string Field1 { get; set; }
    public BarDTO Nested { get; set; }
    public Dictionary<string, FieldDeserializationStatus> FieldStatus { get; set; }
}

class BarDTO : IHasFieldStatus
{
    public int Num { get; set; }
    public string Str { get; set; }
    public bool Bool { get; set; }
    public decimal Dec { get; set; }
    public Dictionary<string, FieldDeserializationStatus> FieldStatus { get; set; }
}

然后,自定义转换器将使用上述LINQ-to-JSON技术来读取要反序列化的对象的JSON。对于目标对象中的每个字段,它会向该对象添加一个项目FieldStatus字典,指示字段是否具有值,是否显式设置为null或JSON中不存在。以下是代码的外观:

class DtoConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return (objectType.IsClass && 
                objectType.GetInterfaces().Any(i => i == typeof(IHasFieldStatus)));
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        var jsonObj = JObject.Load(reader);
        var targetObj = (IHasFieldStatus)Activator.CreateInstance(objectType);

        var dict = new Dictionary<string, FieldDeserializationStatus>();
        targetObj.FieldStatus = dict;

        foreach (PropertyInfo prop in objectType.GetProperties())
        {
            if (prop.CanWrite && prop.Name != "FieldStatus")
            {
                JToken value;
                if (jsonObj.TryGetValue(prop.Name, StringComparison.OrdinalIgnoreCase, out value))
                {
                    if (value.Type == JTokenType.Null)
                    {
                        dict.Add(prop.Name, FieldDeserializationStatus.WasSetToNull);
                    }
                    else
                    {
                        prop.SetValue(targetObj, value.ToObject(prop.PropertyType, serializer));
                        dict.Add(prop.Name, FieldDeserializationStatus.HasValue);
                    }
                }
                else
                {
                    dict.Add(prop.Name, FieldDeserializationStatus.WasNotPresent);
                }
            }
        }

        return targetObj;
    }

    public override bool CanWrite
    {
        get { return false; }
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

上述转换器适用于实现IHasFieldStatus接口的任何对象。 (请注意,除非您打算在序列化上执行自定义操作,否则不需要在转换器中实现WriteJson方法。由于CanWrite返回false,因此在序列化期间不会使用转换器。)

现在,要在Web API中使用转换器,您需要将其插入配置中。将其添加到您的Application_Start()方法:

var config = GlobalConfiguration.Configuration;
var jsonSettings = config.Formatters.JsonFormatter.SerializerSettings;
jsonSettings.C‌​onverters.Add(new DtoConverter());

如果您愿意,可以使用[JsonConverter]这样的属性来装饰每个DTO,而不是在全局配置中设置转换器:

[JsonConverter(typeof(DtoConverter))]
class FooDTO : IHasFieldStatus
{
    ...
}

使用转换器基础结构后,您可以在反序列化后查询DTO上的FieldStatus字典,以查看任何特定字段的发生情况。这是一个完整的演示(控制台应用程序):

public class Program
{
    public static void Main()
    {
        ParseAndDump("First run", @"{
            ""field1"": ""my field 1"",
            ""nested"": {
                ""num"": null,
                ""str"": ""blah"",
                ""dec"": 3.14
            }
        }");

        ParseAndDump("Second run", @"{
            ""field1"": ""new field value""
        }");

        ParseAndDump("Third run", @"{
            ""nested"": null
        }");
    }

    private static void ParseAndDump(string comment, string json)
    {
        Console.WriteLine("--- " + comment + " ---");

        JsonSerializerSettings settings = new JsonSerializerSettings();
        settings.Converters.Add(new DtoConverter());

        FooDTO foo = JsonConvert.DeserializeObject<FooDTO>(json, settings);

        Dump(foo, "");

        Console.WriteLine();
    }

    private static void Dump(IHasFieldStatus dto, string indent)
    {
        foreach (PropertyInfo prop in dto.GetType().GetProperties())
        {
            if (prop.Name == "FieldStatus") continue;

            Console.Write(indent + prop.Name + ": ");
            object val = prop.GetValue(dto);
            if (val is IHasFieldStatus)
            {
                Console.WriteLine();
                Dump((IHasFieldStatus)val, "  ");
            }
            else
            {
                FieldDeserializationStatus status = dto.FieldStatus[prop.Name];
                if (val != null) 
                    Console.Write(val.ToString() + " ");
                if (status != FieldDeserializationStatus.HasValue)
                    Console.Write("(" + status + ")");
                Console.WriteLine();
            }
        }
    }   
}

输出:

--- First run ---
Field1: my field 1 
Nested: 
  Num: 0 (WasSetToNull)
  Str: blah 
  Bool: False (WasNotPresent)
  Dec: 3.14 

--- Second run ---
Field1: new field value 
Nested: (WasNotPresent)

--- Third run ---
Field1: (WasNotPresent)
Nested: (WasSetToNull)

小提琴:https://dotnetfiddle.net/xyKrg2

答案 1 :(得分:3)

通过查看Json.NET源代码,我发现它支持填充后缀为“ Specified”的bool属性,以指示该属性是否包含在数据中:

class MyClass
{
    public string Field1 { get; set; }

    public Nested Nested { get; set; }
    public bool NestedSpecified { get; set; }
}

class Nested
{
    public string Nested1 { get; set; }
    public string Nested2 { get; set; }
}

输入:

{
  "field1": "my field 1",
  "nested": {
    "nested1": "something",
    "nested2": "else"
  }
}

结果实例:

MyClass { Field1="my field 1", Nested=Nested { Nested1="something", Nested2="else" }, NestedSpecified=true }

输入:

{
  "field1": "new field1 value"
}

结果实例:

MyClass { Field1="new field1 value", Nested=null, NestedSpecified=false }

输入:

{
  "nested": null
}

结果实例:

MyClass { Field1=null, Nested=null, NestedSpecified=true }

我在Json.NET文档中找不到此功能,但是好像已经到过since 2010

答案 2 :(得分:2)

您可以向JSON对象和(最有可能的)DTO添加一些元数据。它需要额外的处理,但是非常透明,并且明确地完成了你需要的东西(假设你可以命名新字段,以便你知道它不会与实际数据发生冲突)。

{
  "deletedItems": null,
  "field1": "my field 1",
  "nested": {
    "deletedItems": null,
    "nested1": "something",
    "nested2": "else"
  }
}
{
  "deletedItems": "nested",
  "field1": "new value",
  "nested": null
}

或者,您可以添加&#34; isDeleted&#34;每个字段的属性,如果你的对象模型更好地适应,但听起来比删除的字段列表要多得多。

答案 3 :(得分:0)

我不想劫持这个问题,但我在这里发布了一个略有不同的解决方法:https://stackoverflow.com/a/31489835/1395758

方法是用一个结构替换你的反序列化类型中的字段,该结构将通过IsSet属性自动跟踪值(甚至为null)。

答案 4 :(得分:0)

我想到的最优雅的解决方案是突然显现出来的:

using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace testJsonDeserializer
{
    class Program
    {
        static void Main(string[] args)
        {
            // this operator has the password set to meow. 
            Operator originalOperator = new Operator
            {
                OperatorGuid = Guid.Parse("3bb1dc84-2963-4921-a567-fb2e7475623d"),
                UserName = "noortje@peterhuppertz.net",
                Password = "meow",
                PropertyThatWillBeNulled = "noortje@peterhuppertz.net",
            };

            // this json EXPLICITLY sets the PropertyThatWillBeNulled to null, but omits the Password property, making it null IMPLICITLY. 
            string json =
                "{ \"OperatorGuid\":\"3bb1dc84-2963-4921-a567-fb2e7475623d\", \"UserName\": \"noortje@peterhuppertz.net\", \"Email\": null }";
            // What a PATCH would want for the target object is to leave implicit Nulls unchanged, but explicit nulls set to null. 

            Operator patchedOperator = JsonConvert.DeserializeObject<Operator>(json);
            // At this stage, our patched operator has the password set to null. We do not want that; we want to keep whatever is stored in originalOperator

            Operator opToStore = MapJsonToOperator(patchedOperator, originalOperator, json);

            Console.WriteLine("Our patched operator:");
            Console.WriteLine($"Guid: {opToStore.OperatorGuid}");
            Console.WriteLine($"UserName: {opToStore.UserName}");
            Console.WriteLine($"Password: {opToStore.Password}");
            Console.WriteLine($"Email: {opToStore.PropertyThatWillBeNulled}");
            Console.ReadKey();
        }

        private static Operator MapJsonToOperator(Operator source, Operator original, string json)
        {
            Operator result = new Operator
            {
                OperatorGuid = source.OperatorGuid,
                UserName = source.UserName != null
                    // we check if the source property has a value, if so, we use that value.
                    ? source.UserName
                    // if it doesn't, we check the Json to see if the value is in there, explicitly set to NULL. If it is, we set it to NULL as well
                    : (IsNullValueExplicit(json, "UserName") ? null 
                        // if it is not in the json (making it implicitly null), we just leave the value as it was. 
                        : original.UserName),
                PropertyThatWillBeNulled = source.PropertyThatWillBeNulled != null
                    ? source.PropertyThatWillBeNulled
                    : (IsNullValueExplicit(json, "Email") ? null : original.PropertyThatWillBeNulled),
                Password = source.Password != null
                    ? source.Password
                    : (IsNullValueExplicit(json, "Password") ? null : original.Password),
            };

            return result;
        }

        static bool IsNullValueExplicit(string json, string fieldName)
        {
            JToken outer = JToken.Parse(json);
            JObject inner = outer.Value<JObject>();
            List<string> keys = inner.Properties().Select(p => p.Name).ToList();
            return keys.Contains(fieldName);
        }
    }

    public class Operator
    {
        public Guid OperatorGuid { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public string PropertyThatWillBeNulled { get; set; }
    }
}

我知道,这里有很多评论。也许我过度解释了...但是我认为我会谨慎一点。