【问题标题】:json.net custom jobject deserializationjson.net 自定义 jobject 反序列化
【发布时间】:2017-06-30 00:40:29
【问题描述】:

我正在尝试使用 JsonConvert.DeserializeObject(string) 将字符串反序列化为可以与 dynamic 一起使用以动态访问 json 文档的作业。但是我想避免知道文档的大小写,所以我可以输入

dynamic document = JsonConvert.DeserializeObject(someString);
Console.WriteLine(document.some.path.here.name);

让它在 {"Some":{"path":{"HERE":{"Name":"test"}}} 上工作 我不知道如何为 json.net 创建一个自定义类来为我做这件事,基本上消除了对作业对象的区分大小写(或者可能将所有属性转换为小写)

【问题讨论】:

标签: c# json json.net json-deserialization


【解决方案1】:

要将JToken层次结构中的所有属性递归转换为小写,可以使用以下扩展方法:

public static class JsonExtensions
{
    public static TJToken RenamePropertiesToLowerInvariant<TJToken>(this TJToken root) where TJToken : JToken
    {
        return root.RenameProperties(s => s.ToLowerInvariant());
    }

    public static TJToken RenameProperties<TJToken>(this TJToken root, Func<string, string> map) where TJToken : JToken
    {
        if (map == null)
            throw new ArgumentNullException();
        if (root == null)
            return null;
        if (root is JProperty)
        {
            return RenameReplaceProperty(root as JProperty, map, -1) as TJToken;
        }
        else
        {
            foreach (IList<JToken> obj in root.DescendantsAndSelf().OfType<JObject>())
                for (int i = obj.Count - 1; i >= 0; i--)
                    RenameReplaceProperty((JProperty)obj[i], map, i);
            return root;
        }
    }

    public static IEnumerable<JToken> DescendantsAndSelf(this JToken node)
    {
        if (node == null)
            return Enumerable.Empty<JToken>();
        var container = node as JContainer;
        if (container != null)
            return container.DescendantsAndSelf();
        else
            return new[] { node };
    }

    private static JProperty RenameReplaceProperty(JProperty property, Func<string, string> map, int index)
    {
        // JProperty.Name is read only so it will need to be replaced in its parent.
        if (property == null)
            return null;
        var newName = map(property.Name);
        if (newName == property.Name)
            return property;
        var value = property.Value;
        // Setting property.Value to null on the old property prevents the child JToken hierarchy from getting recursively cloned when added to the new JProperty
        // See https://github.com/JamesNK/Newtonsoft.Json/issues/633#issuecomment-133358110
        property.Value = null; 
        var newProperty = new JProperty(newName, value);
        IList<JToken> container = property.Parent;
        if (container != null)
        {
            if (index < 0)
                index = container.IndexOf(property);
            container[index] = newProperty;
        }
        return newProperty;
    }
}

那么做

dynamic document = JsonConvert.DeserializeObject<JToken>(someString).RenamePropertiesToLowerInvariant();

但是,JObject 区分大小写,并且没有提供构造函数来在其JPropertyKeyedCollection 中使用不区分大小写的比较器。而JObjectDynamicProxy.TryGetMember() 似乎是在做一个简单的查找而不是不区分大小写的搜索。

因此,除非您可以让this answer 工作,否则如果您需要不区分大小写的动态对象,您可以从How to set ExpandoObject's dictionary as case insensitive? 中替换ExpandoObject 之一,然后创建您自己的ExpandoObjectConverter 版本到反序列化您的替代扩展类型。

【讨论】:

  • 我担心 jobject 和我自己的 expando 对象之间的性能差异,但根据此 q 中提供的所有链接似乎是公认的路线
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-19
  • 2017-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多