簡體   English   中英

json.net 自定義作業反序列化

[英]json.net custom jobject deserialization

我正在嘗試使用 JsonConvert.DeserializeObject(string) 將字符串反序列化為一個作業對象,該作業對象可以與動態一起使用以動態訪問 json 文檔。 但是我想避免知道文檔的大小寫,所以我可以輸入

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

並讓它在{"Some":{"path":{"HERE":{"Name":"test"}}}我不知道如何為 json.net 創建一個自定義類對我來說,基本上消除了作業的區分大小寫(或者可能將所有屬性轉換為小寫)

要將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()似乎在做一個簡單的查找而不是不區分大小寫的搜索。

所以,除非你能得到這樣的回答來工作,如果你需要一個不區分大小寫的動態對象,你可以采取替代的一個ExpandoObject如何設置ExpandoObject的字典里不區分大小寫? 然后創建您自己的ExpandoObjectConverter版本來反序列化您的替代 expando 類型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM