我正在实现一个自定义的 JsonConverter。
在实现中,WriteJson
我使用函数JsonWriter.WriteRawValue()
以我的方式编写对象。
在 的实现中ReadJson
,我想以我的方式读取和管理相同的值,并且我希望找到函数JsonReader.ReadRawValue()
,但它不存在。
我错过了什么吗?
谢谢
我正在实现一个自定义的 JsonConverter。
在实现中,WriteJson
我使用函数JsonWriter.WriteRawValue()
以我的方式编写对象。
在 的实现中ReadJson
,我想以我的方式读取和管理相同的值,并且我希望找到函数JsonReader.ReadRawValue()
,但它不存在。
我错过了什么吗?
谢谢
我自己解决了。
对我来说,我是第一个需要这个功能的人似乎很奇怪。
您可以在下面看到 ReadJson 的实现以及支持读取字段原始值的函数。
尽管如此,我还是针对非常复杂的数据结构测试了代码,但我不能声称它涵盖了任何可能的用例。
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();
}