简体   繁体   English

如何使字段成为瞬态,以便在使用编组时不对Java对象进行序列化?

[英]How can I make a field transient, so Java objects don't get serialized when using marshal?

I have a JRuby class which contains an instance variable that is a Java object. 我有一个JRuby类,其中包含一个实例变量,该实例变量是Java对象。 As you may know, JRuby has a lot of trouble serializing Java objects, and therefore I would just like to skip over the object. 您可能知道,JRuby在序列化Java对象时遇到很多麻烦,因此我只想跳过该对象。

How might this be achieved? 如何实现?

You probably would have to write your own custom Marshal function to exclude the Java instance variable. 您可能必须编写自己的自定义Marshal函数才能排除Java实例变量。 For a very simple example, see http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Reflection_ObjectSpace_and_Distributed__Ruby_Custom_Serialization_Strategy.html 有关一个非常简单的示例,请参见http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Reflection_ObjectSpace_and_Distributed__Ruby_Custom_Serialization_Strategy.html

You would need to change the code to custom Marshal all but the Java instance: there is no generic functionality to exclude, only define the whole marshal method on class level. 您需要将代码更改为自定义Marshal(除了Java实例之外的所有代码):没有要排除的通用功能,仅在类级别定义整个marshal方法。 You could use reflection to get a list of instance variables, remove your unwanted ones and marshal the remaining instance vars. 您可以使用反射来获取实例变量的列表,删除不需要的变量并封送其余的实例变量。 Not that difficult, but not a one-liner any more. 没那么困难,但不再是一线好事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 导出Java中的Jar库-如何捆绑其依赖项,以便在使用它时不会出现NoClassDefFound / ClassNotFound错误? - Exporting a Jar library in Java — how to bundle its dependencies so I don't get NoClassDefFound / ClassNotFound errors when using it? 如果我没有在Java中将不可序列化的字段标记为瞬态怎么办? - What happens if i don't mark a non-serializable field as transient in java? 为什么不能在Java中序列化Constructors对象? - Why can't Constructors objects be serialized in Java? 我可以这样做,所以我不需要添加空间来获得总和吗? - Can I make it so I don't have to add a space to get the sum? 我可以将序列化的java对象转换为文本吗? - can I converted serialized java objects to text? 如何使用Jackson来指定某些要序列化为JSON的字段? - How can I specify certain field to be serialized into JSON using Jackson? Java:通用类型转换,如何解决此问题,以免抑制未经检查的警告 - Java: Generic type casting, how can I fix this so I don't suppress the unchecked warning 如何从DefaultModelList创建列表,以便可以使用JAXB编组为XML? - How to create a list from a DefaultModelList so I can marshal to XML using JAXB? 为什么静态瞬态场被序列化? - why static transient field is serialized? 我没有在Java中使用“this”和“that”的概念 - I don't get the concept of using “this” and “that” in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM