带有Stream类型成员的对象上的JSON.NET序列化? [英] JSON.NET Serialization on an object with a member of type Stream?

查看:84
本文介绍了带有Stream类型成员的对象上的JSON.NET序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望这是一个我忽略的简单解决方法.我有一个传递到事件处理程序中的对象,我想使用JSON.NET将该对象序列化,如下所示:

Hopefully this is an easy fix that I have overlooked. I have an object passed into an event handler that I want to serialize that object using JSON.NET, like so:

public void OnEvent(IEventObject foo)
{
    // Serialize foo to string/disk here?
    var data = JsonConvert.SerializeObject(foo, Formatting.Indented);
}

似乎foo的一个或多个成员是流.我已经认识到Streams不可序列化,因为它们是对数据的抽象,而不是对数据本身的抽象.这是有道理的.

It appears that one or more of foo's members are streams. I already recognize that Streams are not serializable since they are an abstraction over data and not the data itself. This makes sense.

无论如何,我都不知道如何序列化该对象:

I do not know how to serialize this object anyway by either:

  • a)将流转换为数据并对其进行序列化
  • b)忽略流并序列化其余成员

对此的一个重要警告是,我无权访问IEventObject或其实现,因此无法使用属性标志标记这些对象中的任何一个.

One big caveat to this is that I do not have access to IEventObject or its implementations, so I cannot mark up any of these objects with attribute flags.

我想出的唯一解决方案是将该对象包装在我自己的类中,对其进行适当的标记,然后对其进行序列化.稍后,我将反序列化回自己的类,并将其转换为原始对象.我不喜欢这种方法,因为它涉及到额外的对象和转换步骤,并且在可能的情况下希望避免这种情况.

The only solution I have come up with is to wrap this object in my own class, mark it up appropriately, and serialize that. Later I would deserialize back into my own class, and convert it into the original object. I don't like this approach since it involves an extra object and conversion step, and would like to avoid it if possible.

推荐答案

默认情况下,Json.NET将尝试序列化流的属性,这不是很有用.您可以通过创建自己的合同解析器来修改行为.这是一个完全忽略所有Stream的示例:

By default, Json.NET will try to serialize the properties of the stream, which isn't very useful. You can modify the behavior by creating your own contract resolver. Here's an example that ignores all Streams entirely:

public class IgnoreStreamsResolver : DefaultContractResolver
{
    protected override JsonProperty CreateProperty(
        MemberInfo member,
        MemberSerialization memberSerialization
    )
    {
        JsonProperty property = base.CreateProperty(member, memberSerialization);
        if (typeof(Stream).IsAssignableFrom(property.PropertyType))
        {
            property.Ignored = true;
        }
        return property;
    }
}

使用方式如下:

var bytes = new byte[] { 1, 2, 3 };
var eo = new EventObject { OtherValue = 2, MyStream = new MemoryStream(bytes) };
var s = JsonConvert.SerializeObject(eo,
  new JsonSerializerSettings { ContractResolver = new IgnoreStreamsResolver() });
// {"OtherValue":2}

通过修改 JsonProperty 的其他属性,可以进行其他更改.看起来对您来说最有用的一个是Converter,它可以让您指定自己的类来定义如何序列化Stream(例如,将其转换为byte[]并将其序列化为base64).

By modifying other properties of the JsonProperty, you can make other changes. The one that looks like it might be most useful to you is Converter, which would let you specify your own class to define how to serialize the Stream (e.g. convert it to a byte[] and serialize that as base64).

所有这些操作都无需对接口或实现类进行任何更改.

All of this is done without any changes to the interface or implementing class.

这篇关于带有Stream类型成员的对象上的JSON.NET序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆