[英]can I converted serialized java objects to text?
I have some code for reading and writing objects in Java (using serialization) as well as some objects. 我有一些代码用于在Java中读取和编写对象(使用序列化)以及一些对象。
I would like to convert them to text, so someone else, from another platform can use these objects with my code (which is just provided as a package). 我想将它们转换为文本,因此来自其他平台的其他人可以将这些对象与我的代码一起使用(它只是作为包提供)。
I could write a whole parser for text format for the objects. 我可以为对象编写一个完整的文本格式解析器。 However, there is a deadline coming, and I was hoping it might be much easier to do it some other way, so that person can explore the objects and my Java code himself.
但是,有一个截止日期即将到来,我希望以其他方式更容易实现,这样人们就可以自己探索对象和我的Java代码。
So I guess, my question is: what is the easiest way to migrate from Java serialization to writing objects in convenient ascii form? 所以我想,我的问题是:从Java序列化迁移到以方便的ascii形式编写对象的最简单方法是什么? (though I suspect the answer is: write a parser yourself! :-))
(虽然我怀疑答案是:自己写一个解析器!:-))
I suggest you to use some standard format, such as YAML or aforementioned JSON or XML. 我建议你使用一些标准格式,例如YAML或前面提到的JSON或XML。 If your objects form a hierarchical structure without circular dependencies, I'd choose JSON and use Jackson JSON processor - is actively developed and easy to use.
如果你的对象形成一个没有循环依赖的层次结构,我选择JSON并使用Jackson JSON处理器 - 积极开发并易于使用。
If your objects have circular dependencies, I'd choose YAML, because it can handle then using references , so it'll work even if you have complex object structures. 如果你的对象有循环依赖,我选择YAML,因为它可以使用引用来处理,所以即使你有复杂的对象结构它也会工作。 SnakeYAML seems to be a good choice.
SnakeYAML似乎是一个不错的选择。
I was keen to test another library yamlbeans to see how it handles circular dependencies, so I made a small example for myself. 我热衷于测试另一个库yamlbeans以查看它如何处理循环依赖,所以我为自己做了一个小例子。 Here it is:
这里是:
// YamlEx.java
import java.io.*;
import java.util.*;
import com.esotericsoftware.yamlbeans.*;
public class YamlEx {
public static void main(String argv[])
throws Exception
{
Person p1 = new Person("John");
Person p2 = new Person("Paul");
Person p3 = new Person("Bob");
// create a circular dependencies, even to self (p1-p1)
p1.setFriends2(p2, p1, p3);
p2.setFriends2(p1);
p3.setFriends2(p1, p2);
// serialize
CharArrayWriter w = new CharArrayWriter();
YamlWriter writer = new YamlWriter(w);
writer.write(p1);
writer.close();
// print it to the console
System.out.println(w.toString());
// read it again
Reader r = new CharArrayReader(w.toCharArray());
YamlReader reader = new YamlReader(r);
p1 = reader.read(Person.class);
reader.close();
System.out.println(String.format("%s's friends: %s",
p1.name, p1.getFriends() ));
}
}
// Person.java
import java.util.*;
public class Person {
// A public field as a test.
public String name;
public Person() {
}
public Person(String name) {
this.name = name;
}
public String toString() {
return name;
}
// A set/get pair as a test.
private List<Person> friends0;
public List<Person> getFriends() {
return friends0;
}
public void setFriends(List<Person> p) {
this.friends0 = p;
}
public void setFriends2(Person... p) {
setFriends(Arrays.asList(p));
}
}
Works as expected: 按预期工作:
&1 !Person
name: John
friends: !java.util.Arrays$ArrayList
- &2 !Person
name: Paul
friends: !java.util.Arrays$ArrayList
- *1
- *1
- !Person
name: Bob
friends: !java.util.Arrays$ArrayList
- *1
- *2
John's friends: [Paul, John, Bob]
I'd suggest that your serialized object should be packed into Base64 string, then sent to another platform, decrypted from Base64 and casted to new object, just as described here: How to serialize an object into a string 我建议将序列化对象打包到Base64字符串中,然后发送到另一个平台,从Base64解密并转换为新对象,如下所述: 如何将对象序列化为字符串
I've seen many implementations of given problem and Base64 seems to be the easiest way. 我已经看到很多给定问题的实现,Base64似乎是最简单的方法。
I'd go for JSON , much easier and more human readable. 我会选择JSON ,更容易,更易读。 There are lot of JSON libraries for almost all languages.
几乎所有语言都有很多JSON库。 Especially for java i always prefer these two.
特别是对于java我总是喜欢这两个。
Where JACKSON has some performance advantages . JACKSON在哪些方面具有一些性能优势 。
There is simple guide too. 也有简单的指南 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.