将成员转换为(反)序列化过程的另一种类型。 [英] Transforming a member into another type for (de)serialization process.

查看:93
本文介绍了将成员转换为(反)序列化过程的另一种类型。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,



假设我有一个包含内部敏感结构的类,其内容我不希望在收到对象时透露给我的用户我已经序列化了,但是他必须收到这些数据(一个代币......)



换句话说,我有兴趣替换这个具有串行加密字符串的结构,在反序列化类时将被转换为结构。每次在序列化中使用类时都会出现这种情况,因此我希望用某个属性来装饰类。



有没有标准的方法可以做到这一点?我知道JSON.NET有一个JsonConverterAttribute,但我更喜欢与序列化程序无关的类(我使用DataMemberAttribute)的方法,我不希望JSON.NET作为依赖项(或者作为我唯一支持的序列化程序... )



我试图忽略内部结构,并使用字符串属性来设置和获取内部结构。这是反序列化的适当解决方案,但我没有可序的序列化解决方案。



我还试图创建类似SerializationTransformationObject< tinternaltype,>的通用对象。 。它运作良好,但有一些美学上的缺点。我已经实现了自己的JsonConverter(目前只用于反序列化),我考虑过添加对前面提到的类型的支持。这样,我的Json Serializer自己解决了这个问题,而没有在我的代码中创建依赖关系。



我也考虑自己编译JSON.NET,并在我的中定义一个相同的属性抽象序列化库。



你会推荐什么?



谢谢!

Hey,

Lets say I have a class which contains an internal sensitive structure whose content I don't want to reveal to my user when he receives an object I've serialized, but it is essential that he would have receive this data (a token...)

In other words, I'm interested in replacing this structure with a seriailzed-encrypted string, which will be deconverted into the structure when deserializing the class. This behaviour is expected everytime the class is used in serialization, therefore I would expect to decorate the class with a certain attribute.

Is there a standard way to do that? I know that JSON.NET has a "JsonConverterAttribute", but I prefer the approach of serializer-agnostic classes (I use DataMemberAttribute), and I don't want JSON.NET as a dependency (or as my sole supported serializer...)

I have tried ignoring the internal structure, and using a string property, that sets and gets the internal structure. It was an adequate solution for deserialization, but I had no passable solution for serialization.

I have also tried to create a generic object something like SerializationTransformationObject<tinternaltype,>. It worked fine, but had some esthetic disadvantages. I'm implementing my own JsonConverter already (currently for Deserialization only), I've considered adding support for the forementioned type. That way, my Json Serializer solves the problem himself, without creating dependencies in my code.

I also considered compiling JSON.NET myself, and defining an identical attribute in my abstract serialization library.

What would you recommend?

Thanks!

推荐答案

继承 ISerializable 然后实现自定义序列化。您需要做的就是使用您想要的任何加密来加密数据。然后,您可以使用私钥/公钥加密系统对另一端的数据进行反序列化。



这就是我要做的事情。





见此 [ ^ ]



AES加密 [ ^ ]



您始终可以将字节流转换为Base-64字符串,然后加密字符串并将其作为纯文本发送, XML或JSON。
Inherit ISerializable and then implement custom serialization. All you need to do then is to encrypt the data using whatever encryption you want. You can then use a private/public key encryption system to deserialize the data on the other side.

That's what I would do anyway.


See this[^]

AES encryption[^]

You can always convert the byte stream to a Base-64 string, then encrypt the string and send it over as plain text, XML, or JSON.


这篇关于将成员转换为(反)序列化过程的另一种类型。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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