如何让ReadJson返回"默认"行为 - 好像CanConvert返回false

时间:2016-12-01 14:34:27

标签: c# .net json serialization json.net

  • 我创建了JsonConverter
  • 的实现
  • CanConvert始终返回true。
  • ReadJson我希望有时只使用"默认"行为,好像CanConvert返回false并且我的ReadJson从未被调用过。
  • 其他各种帖子都建议我做以下几点变化:

existingValue = existingValue ?? serializer
                                     .ContractResolver
                                     .ResolveContract(objectType)
                                     .DefaultCreator();
serializer.Populate(reader, existingValue);
  • 但是,这会在NullReferenceException上抛出.DefaultCreator()
  • existingValue始终为null
  • 从序列化程序返回的ContractResolver是我自己的。它扩展了json.net的内置CamelCasePropertyNamesContractResolver并简单地覆盖了方法CreateConstructorParametersCreatePropertyFromConstructorParameter

我如何告诉json.net - "开玩笑,我不知道如何制作这个东西,做任何你想做的事情,我告诉过你我无法做到这一点。创造它"

请注意,我已将问题简化为讨论。我期待有人会回复#34;只需CanCreate返回false"事实上,在许多情况下,我可以而且应该创建对象。

1 个答案:

答案 0 :(得分:1)

从您的问题和评论中,听起来有些情况下您希望转换器读取但不能写入,以及其他您希望它写入但不能读取的情况。您已通过将功能划分为两个转换器然后让每个转换器的CanConvert方法在适当的时间返回true或false来解决问题。这当然是一种可行的方法,似乎对你有用,这很棒。但是,我想提供另一种解决方案。

除了CanConvert方法之外,基础JsonConverter还提供了两个虚拟布尔属性,您可以覆盖它们:CanReadCanWrite。 (两者都默认返回true。)这些属性直接控制序列化程序是否为特定转换器调用ReadJsonWriteJson。因此,例如,如果CanRead返回false,则不会调用ReadJson,并且将使用默认的读取行为,即使CanConvert返回true也是如此。这使您可以非常巧妙地设置非对称转换器。例如,您可能希望将疯狂的JSON格式反序列化为更合理的对象结构,但是当您再次序列化它时,您不想回到疯狂的JSON格式 - 您只需要想要默认的序列化。在这种情况下,您将覆盖转换器中的CanWrite以始终返回false。然后你可以将WriteJson的实现留空或让它抛出NotImplementedException;永远不会被召唤。

您的案例听起来有点复杂,但您仍然应该能够操纵CanReadCanWrite属性来达到理想的效果。下面是一个人为的例子,它显示了我们如何根据情境变量打开和关闭ReadJsonWriteJson方法。

public class Program
{
    public static void Main(string[] args)
    {
        string json = @"{""keys"":[""foo"",""fizz""],""values"":[""bar"",""bang""]}";

        CustomConverter converter = new CustomConverter();
        JsonSerializerSettings settings = new JsonSerializerSettings();
        settings.Converters.Add(converter);

        // Here we are reading a JSON object containing two arrays into a dictionary
        // (custom read) and then writing out the dictionary JSON (standard write)
        Console.WriteLine("--- Situation 1 (custom read, standard write) ---");
        converter.Behavior = ConverterBehavior.CustomReadStandardWrite;
        json = DeserializeThenSerialize(json, settings);

        // Here we are reading a simple JSON object into a dictionary (standard read)
        // and then writing out a new JSON object containing arrays (custom write)
        Console.WriteLine("--- Situation 2 (standard read, custom write) ---");
        converter.Behavior = ConverterBehavior.StandardReadCustomWrite;
        json = DeserializeThenSerialize(json, settings);
    }

    private static string DeserializeThenSerialize(string json, JsonSerializerSettings settings)
    {
        Console.WriteLine("Deserializing...");
        Console.WriteLine(json);
        var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(json, settings);
        foreach (var kvp in dict)
        {
            Console.WriteLine(kvp.Key + ": " + kvp.Value);
        }

        Console.WriteLine("Serializing...");
        json = JsonConvert.SerializeObject(dict, settings);
        Console.WriteLine(json);
        Console.WriteLine();

        return json;
    }
}

enum ConverterBehavior { CustomReadStandardWrite, StandardReadCustomWrite }

class CustomConverter : JsonConverter
{
    public ConverterBehavior Behavior { get; set; }

    public override bool CanConvert(Type objectType)
    {
        return typeof(IDictionary<string, string>).IsAssignableFrom(objectType);
    }

    public override bool CanRead
    {
        get { return Behavior == ConverterBehavior.CustomReadStandardWrite; }
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        Console.WriteLine("ReadJson was called");

        // Converts a JSON object containing a keys array and a values array
        // into a Dictionary<string, string>
        JObject jo = JObject.Load(reader);
        return jo["keys"].Zip(jo["values"], (k, v) => new JProperty((string)k, v))
                         .ToDictionary(jp => jp.Name, jp => (string)jp.Value);
    }

    public override bool CanWrite
    {
        get { return Behavior == ConverterBehavior.StandardReadCustomWrite; }
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        Console.WriteLine("WriteJson was called");

        // Converts a dictionary to a JSON object containing 
        // a keys array and a values array from the dictionary
        var dict = (Dictionary<string, string>)value;
        JObject jo = new JObject(new JProperty("keys", new JArray(dict.Keys)),
                                 new JProperty("values", new JArray(dict.Values)));
        jo.WriteTo(writer);
    }
}

输出:

--- Situation 1 (custom read, standard write) ---
Deserializing...
{"keys":["foo","fizz"],"values":["bar","bang"]}
ReadJson was called
foo: bar
fizz: bang
Serializing...
{"foo":"bar","fizz":"bang"}

--- Situation 2 (standard read, custom write) ---
Deserializing...
{"foo":"bar","fizz":"bang"}
foo: bar
fizz: bang
Serializing...
WriteJson was called
{"keys":["foo","fizz"],"values":["bar","bang"]}

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