首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何筛选/转换使用jsonfx序列化的对象属性?

如何筛选/转换使用jsonfx序列化的对象属性?
EN

Stack Overflow用户
提问于 2015-09-25 13:08:20
回答 1查看 808关注 0票数 0

我使用JsonFX将实例序列化为json,我需要忽略一些属性,并对其他属性进行预处理。我该怎么做?医生对此不太清楚。

我的课看起来有点像这门课:

代码语言:javascript
复制
public class Primitive{
    public string type;
    public float[] vertices;
    public int[] indices;
    public int[] edgeIndices;
    public Scene scene;

在输出json时,我需要忽略scene属性(这是整个3d场景),并使用某种压缩方案对verticesindicesedgeIndices进行预处理。

很明显,当我收到json时,我需要对称的操作。

但是怎么做呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-25 18:56:13

您可以使用[JsonIgnore]JsonResolverStrategy

代码语言:javascript
复制
public class Scene
{
    public string Name { get; set; } // Or whatever
}

public class Primitive
{
    public string type;
    public float[] vertices;
    public int[] indices;
    public int[] edgeIndices;
    [JsonFx.Json.JsonIgnore]
    public Scene scene;
}

public class TestClass
{
    public static void Test()
    {
        var primitive = new Primitive
        {
            type = "some type",
            vertices = new[] { 1.0f, 2.0f },
            indices = new[] { 1, 2 },
            edgeIndices = new[] { 0, 1 },
            scene = new Scene { Name = "Should Not Be Serialized" }
        };

        var writer = new JsonWriter(new DataWriterSettings(new JsonResolverStrategy()));

        var json = writer.Write(primitive);

        Debug.WriteLine(json);  // Prints {"type":"some type","vertices":[1,2],"indices":[1,2],"edgeIndices":[0,1]}
        Debug.Assert(!json.Contains("Should Not Be Serialized")); // No assert
    }
}

或者,您可以使用数据契约属性DataContractResolverStrategy

代码语言:javascript
复制
public class Scene
{
    public string Name { get; set; } // Or whatever
}

[DataContract]
public class Primitive
{
    [DataMember]
    public string type;
    [DataMember]
    public float[] vertices;
    [DataMember]
    public int[] indices;
    [DataMember]
    public int[] edgeIndices;
    [IgnoreDataMember]
    public Scene scene;
}

public class TestClass
{
    public static void Test()
    {
        var primitive = new Primitive
        {
            type = "some type",
            vertices = new[] { 1.0f, 2.0f },
            indices = new[] { 1, 2 },
            edgeIndices = new[] { 0, 1 },
            scene = new Scene { Name = "Should Not Be Serialized" }
        };

        var writer = new JsonWriter(new DataWriterSettings(new DataContractResolverStrategy()));

        var json = writer.Write(primitive);

        Debug.WriteLine(json);
        Debug.Assert(!json.Contains("Should Not Be Serialized")); // No assert
    }
}

(但是请注意,DataContractResolverStrategy在.Net 3.5中是坏的,所以如果您停留在旧版本上,就不能使用它。)

更新

序列化期间“转换”属性的一种方法是使用代理属性,例如:

代码语言:javascript
复制
public class Primitive
{
    public string type;

    public float[] vertices;

    [JsonFx.Json.JsonIgnore]
    public int[] indices;

    [JsonFx.Json.JsonName("indices")]
    public string compressedIndices
    {
        get
        {
            return indices.Base64Compress();
        }
        set
        {
            indices = CompressionExtensions.Base64DecompressIntArray(value);
        }
    }

    public int[] edgeIndices;

    [JsonFx.Json.JsonIgnore]
    public Scene scene;
}

public static class CompressionExtensions
{
    public static string Base64Compress(this IEnumerable<int> values)
    {
        if (values == null)
            return null;
        using (var stream = new MemoryStream())
        {
            using (var compressor = new DeflateStream(stream, CompressionMode.Compress, true))
            {
                var _buffer = new byte[4];
                foreach (var value in values)
                {
                    _buffer[0] = (byte)value;
                    _buffer[1] = (byte)(value >> 8);
                    _buffer[2] = (byte)(value >> 16);
                    _buffer[3] = (byte)(value >> 24);
                    compressor.Write(_buffer, 0, 4);
                }
            }
            return Convert.ToBase64String(stream.GetBuffer(), 0, checked((int)stream.Length)); // Throw an exception on overflow.
        }
    }

    public static int[] Base64DecompressIntArray(string base64)
    {
        if (base64 == null)
            return null;
        var list = new List<int>();
        var m_buffer = new byte[4];
        using (var stream = new MemoryStream(Convert.FromBase64String(base64)))
        {
            using (var compressor = new DeflateStream(stream, CompressionMode.Decompress))
            {
                while (compressor.Read(m_buffer, 0, 4) == 4)
                {
                    list.Add((int)(m_buffer[0] | m_buffer[1] << 8 | m_buffer[2] << 16 | m_buffer[3] << 24));
                }
            }
        }
        return list.ToArray();
    }
}

然后写和读如下:

代码语言:javascript
复制
        var primitive = new Primitive
        {
            type = "some type",
            vertices = new[] { 1.0f, 2.0f },
            indices = Enumerable.Range(0, 10000).Select(i => 0).ToArray(),
            edgeIndices = new[] { 0, 1 },
            scene = new Scene { Name = "Should Not Be Serialized" }
        };

        var writer = new JsonWriter(new DataWriterSettings(new JsonResolverStrategy()));

        var json = writer.Write(primitive);

        var reader = new JsonReader(new DataReaderSettings(new JsonResolverStrategy()));
        var primitiveBack = reader.Read<Primitive>(json);

        Debug.Assert(primitiveBack.indices.SequenceEqual(primitive.indices));
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32782959

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档