首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JsonReader.ReadRawValue在哪?

JsonReader.ReadRawValue在哪?
EN

Stack Overflow用户
提问于 2020-08-21 21:58:26
回答 1查看 169关注 0票数 0

我正在实现一个自定义的JsonConverter。

WriteJson的实现中,我使用函数JsonWriter.WriteRawValue()以自己的方式编写对象。

ReadJson的实现中,我希望以自己的方式读取和管理相同的值,并希望找到函数JsonReader.ReadRawValue(),但它不在那里。

我是不是遗漏了什么?

谢谢

EN

回答 1

Stack Overflow用户

发布于 2020-08-27 01:26:59

我自己解决的。

对我来说,这似乎很奇怪,我是第一个需要这个功能的人。

您可以在下面看到支持读取字段原始值的ReadJson和函数的实现。

尽管如此,我还是针对相当复杂的数据结构测试了代码,我不能说它涵盖了任何可能的用例。

代码语言:javascript
复制
    public override YourCustomType ReadJson(JsonReader reader, Type objectType, YourCustomType existingValue, bool hasExistingValue, JsonSerializer serializer)
    {
        // read the value of my field as a JObject
        JObject someJsonValue = (JObject)serializer.Deserialize(reader, typeof(object));
        
        // get the raw-value from the JObject
        string rawValue = ReadAnyRawValue(someJsonValue);
        
        // do whatever processing with the raw-value
        YourCustomType returnObj; // create the return object
        return returnObj;
    }

    // It returns the raw-value of the passed-in token. 
    // The passed-in token must NOT be of type Property. 
    // The token can be any Json value(if it's a Json value means it's not a Property).
    // Depending on the data-type of the value (of the token) the return value of this function can be: {...} or [...] or null or "some text" or any other primary data value
    private string ReadAnyRawValue(JToken anyValueToken)
    {
        // anyToken can be an object, it can be an array or it can be a primary value
        
        if(anyValueToken.Type == JTokenType.Property)
        {
            throw new ArgumentException($"The parameter {nameof(anyValueToken)} is not supposed to be of type JTokenType.Property. It must be a value.");
        }
        
        if(anyValueToken.Type == JTokenType.Object)
        {
            return ReadObjectRawValue(anyValueToken);
        }
        else if(anyValueToken.Type == JTokenType.Array)
        {
            return ReadArrayRawValue(anyValueToken);
        }
        else
        {
            // it's a primary value or null;
            
            if(anyValueToken.Type == JTokenType.Null)
            {
                return "null";
            }
            else if(anyValueToken.Type == JTokenType.String)
            {
                return @$"""{anyValueToken}""";
            }
            else
            {
                return @$"{anyValueToken}";
            }
        }
        
    }
    
    // It returns the property name and its raw-value in the format: "Name":RawValue
    // Depending on the data-type of the property, RawValue can be {...} or [...] or null or "some text" or any other primary data value
    // The passed-in token must be of type Property, that means an object that has a Name and a Value
    private string ReadPropertyRaw(JProperty jProp)
    {
        return @$"""{jProp.Name}"":{ReadAnyRawValue(jProp.Value)}";
    }
    
    // It returns a json object, that means {...}
    // The passed-in token must be of type Object
    private string ReadObjectRawValue(JToken objectToken)
    {
        if(objectToken.Type != JTokenType.Object)
        {
            throw new ArgumentException($"The parameter {nameof(objectToken)} is expected to be of type JTokenType.Object");
        }
        
        StringBuilder bld = new StringBuilder(500);
        bld.Append('{');
        
        JToken tmp = objectToken.First;

        bool isFirst = true;
        while (tmp != null)
        {
            
            if (!isFirst)
            {
                bld.Append(',');
            }

            if(tmp.Type == JTokenType.Property)
            {
                bld.Append(this.ReadPropertyRaw(tmp as JProperty));
            }
            else
            {
                bld.Append($"{tmp}");
            }
            tmp = tmp.Next;
            isFirst = false;
        }

        bld.Append('}');
        
        return bld.ToString();
    }
    
    // It returns a json array, that means: [...]
    // The passed-in token must be of type Array
    private string ReadArrayRawValue(JToken arrayToken)
    {
        if (arrayToken.Type != JTokenType.Array)
        {
            throw new ArgumentException($"The parameter {nameof(arrayToken)} is expected to be of type JTokenType.Array");
        }
        StringBuilder bld = new StringBuilder(500);
        bld.Append('[');
        var tmp = arrayToken.First;

        bool isFirst = true;
        while (tmp != null)
        {
            if (!isFirst)
            {
                bld.Append(',');
            }
            
            // the elements of an array always values (not properties)
            bld.Append(ReadAnyRawValue(tmp));
            
            tmp = tmp.Next;
            isFirst = false;
        }
        bld.Append(']');
        return bld.ToString();
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63524258

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档