0

I am implementing a custom JsonConverter.

In the implementation of WriteJson I use the function JsonWriter.WriteRawValue() to write an object my way.

In the implementation of ReadJson, I want to read and manage the same value my way and I expected to find the function JsonReader.ReadRawValue(), but it's not there.

Am I missing something?

Thank you

dbc
  • 104,963
  • 20
  • 228
  • 340
  • 1
    hi Ernest, can you add a tag for which programming language you're using? – casenonsensitive Aug 21 '20 at 20:32
  • Is there any other way to get the raw value of a field? I found this work around, but it's not acceptable: ```var bodyJObject = (JObject)serializer.Deserialize(reader, typeof(object)); var bodyAsText = bodyJObject.ToString().Replace("\r\n","");``` The ```JObject.ToString()``` returns the value I need but with formatting. That ```string.Replace()``` is very dangerous. Can I ask the Newtonsoft team the favor to add the function ```ReadRawValue``` to the class ```JsonReader```? – Ernest Morariu Aug 24 '20 at 07:50
  • Related or duplicate: [Efficiently get full json string in JsonConverter.ReadJson()](https://stackoverflow.com/q/56944160/3744182). – dbc Apr 19 '21 at 14:44

1 Answers1

0

I resolved it myself.

To me it seems strange I am the first one who needs this feature.

You can see below the implementation of the ReadJson and the functions which support reading the raw-value of a field.

Nonetheless I tested the code against pretty complex data structures, I cannot claim it covers any possible use-case.

    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();
    }