实现自定义JsonWriter(JSON.net)

时间:2015-09-16 15:07:15

标签: c# .net json json.net

我一直在按照this sample

来实现这个自定义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中,但在样本中也没有任何内容

1 个答案:

答案 0 :(得分:2)

如果要通过继承JsonTextWriterJsonWriter来执行此操作,则需要覆盖所有WriteValue方法并重新映射正在写入的值。这个名称有很多种方法,因为Newtonsoft理论上可以在以后的版本中添加新方法(例如BigNumber),这可能会成为维护麻烦。

更简单的解决方案可能是serialize to 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()));
    }
}

如果你坚持继承编写器,原型实现将如下所示:

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

原型fiddle