实现定制JsonWriter (JSON.net)
本文关键字:JSON net JsonWriter 实现 | 更新日期: 2023-09-27 18:12:57
我一直致力于通过以下示例
实现这个自定义JSON写入器。我重写了WriteValue
方法,这样我就不会打印json的值,而是打印Type.ToString()
并隐藏该项的实际值。
我用这个方法有麻烦:
private void WriteValueElement(string elementName, JTokenType type)
{
_writer.WriteStartElement(elementName);
_writer.WriteAttributeString("type", type.ToString());
}
特别是这个语句_writer.WriteStartElement(elementName);
我只是不知道这个WriteStartElement在哪里。这个方法签名在baseClass中不存在,而且在示例
如果您想通过子类化JsonTextWriter
或JsonWriter
来做到这一点,您将需要覆盖所有WriteValue
方法并重新映射正在写入的值。有许多方法使用这个名称,并且由于Newtonsoft理论上可以在以后的版本中添加新方法(例如,对于BigNumber
),这可能会成为维护的麻烦。
一个更简单的解决方案可能是序列化到JToken
,然后像这样重新映射值:
public static class JsonExtensions
{
public static IList<JToken> ChildrenTokenList(this JContainer container)
{
return container;
}
public static IEnumerable<JToken> DescendantsAndSelf(this JToken node)
{
// Small wrapper adding this method to all JToken types.
if (node == null)
return Enumerable.Empty<JToken>();
var container = node as JContainer;
if (container != null)
return container.DescendantsAndSelf();
else
return new[] { node };
}
public static JToken MapValues(this JToken root, Func<JValue, JValue> map)
{
if (map == null || root == null)
throw new ArgumentNullException();
if (root is JValue)
return map((JValue)root);
else
{
root = root.DeepClone();
foreach (var container in root.DescendantsAndSelf().OfType<JContainer>())
{
var children = container.ChildrenTokenList();
for (int i = 0, n = children.Count; i < n; i++)
if (children[i] is JValue)
children[i] = map((JValue)children[i]);
}
return root;
}
}
public static JToken MapValuesToValueTypes(this JToken root)
{
return root.MapValues(v => (v.Type == JTokenType.Null ? (JValue)null : (JValue)v.Type.ToString()));
}
}
如果你坚持将writer子类化,原型实现将是这样的:
public class ValueTypeJsonWriter : JsonTextWriter
{
public ValueTypeJsonWriter(TextWriter textWriter)
: base(textWriter)
{
}
private void WriteValueType(object value)
{
if (value == null)
base.WriteNull();
else
{
var token = JToken.FromObject(value);
base.WriteValue(token.Type.ToString());
}
}
public override void WriteValue(object value)
{
WriteValueType(value);
}
public override void WriteValue(bool value)
{
WriteValueType(value);
}
public override void WriteValue(bool? value)
{
WriteValueType(value);
}
public override void WriteValue(byte value)
{
WriteValueType(value);
}
public override void WriteValue(byte? value)
{
WriteValueType(value);
}
public override void WriteValue(byte[] value)
{
WriteValueType(value);
}
public override void WriteValue(char value)
{
WriteValueType(value);
}
public override void WriteValue(char? value)
{
WriteValueType(value);
}
public override void WriteValue(DateTime value)
{
WriteValueType(value);
}
public override void WriteValue(DateTime? value)
{
WriteValueType(value);
}
public override void WriteValue(DateTimeOffset value)
{
WriteValueType(value);
}
public override void WriteValue(DateTimeOffset? value)
{
WriteValueType(value);
}
public override void WriteValue(decimal value)
{
WriteValueType(value);
}
public override void WriteValue(decimal? value)
{
WriteValueType(value);
}
public override void WriteValue(double value)
{
WriteValueType(value);
}
public override void WriteValue(double? value)
{
WriteValueType(value);
}
public override void WriteValue(float value)
{
WriteValueType(value);
}
public override void WriteValue(float? value)
{
WriteValueType(value);
}
public override void WriteValue(Guid value)
{
WriteValueType(value);
}
public override void WriteValue(Guid? value)
{
WriteValueType(value);
}
public override void WriteValue(int value)
{
WriteValueType(value);
}
public override void WriteValue(int? value)
{
WriteValueType(value);
}
public override void WriteValue(long value)
{
WriteValueType(value);
}
public override void WriteValue(long? value)
{
WriteValueType(value);
}
public override void WriteValue(sbyte value)
{
WriteValueType(value);
}
public override void WriteValue(sbyte? value)
{
WriteValueType(value);
}
public override void WriteValue(short value)
{
WriteValueType(value);
}
public override void WriteValue(short? value)
{
WriteValueType(value);
}
public override void WriteValue(string value)
{
WriteValueType(value);
}
public override void WriteValue(TimeSpan value)
{
WriteValueType(value);
}
public override void WriteValue(TimeSpan? value)
{
WriteValueType(value);
}
public override void WriteValue(uint value)
{
WriteValueType(value);
}
public override void WriteValue(uint? value)
{
WriteValueType(value);
}
public override void WriteValue(ulong value)
{
WriteValueType(value);
}
public override void WriteValue(ulong? value)
{
WriteValueType(value);
}
public override void WriteValue(Uri value)
{
WriteValueType(value);
}
public override void WriteValue(ushort value)
{
WriteValueType(value);
}
public override void WriteValue(ushort? value)
{
WriteValueType(value);
}
}
原型小提琴。