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