1

Is an object's implementation of the Serializable interface in any way related to that object's ability to be serialized into JSON or XML? Is there a name for the text format that Java serialization uses? If not, should we not use the word "serialization" to describe exporting an object to JSON or XML, to avoid confusion? At AO, what uses are typical for each of these three serialization methods?

I know that JAXB is usually used to convert XML to Java, not the other way around, but I heard that the reverse is possible too.

1
  • 1
    Is there a name for the text format that Java serialization uses? Java does not serialize to text, so no, it is nameless. Commented Apr 8, 2016 at 21:04

2 Answers 2

5

Serialization simply refers to exporting an object from a process-specific in-memory format to an inter-process format that can be read and understood by a different process or application. It may be text, or it may be binary, it doesn't matter. It's all serialization. The reverse processes (reading and parsing a serialized inter-process format into an in-memory, in-process format) is called deserialization.

In that sense, serializing an object into an ObjectStream is just as much serialization as serializing it to JSON or XML. ObjectStream serialization is very difficult to understand/parse by non-java (including humans. It is not "human-readable"), but is used because it can be done on pretty much any object without any special markup.

JSON/XML on the other hand require extra work to tell the parser how to map them to and from JSON/XML, but are very portable - pretty much every language can understand JSON/XML, including humans - it is "human-readable".

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help!
1

One purpose of serialization of Java objects is being able to write them to a (binary) file from which some Java program can read them back, getting the same objects into its memory. This usage is usually limited to Java applications, writing and reading, although some non-Java app might be written to understand the binary format.

Another frequently used serialization of Java objects is to write them to a text (or binary) file from which some (note the absence of: Java) program can read and reconstruct an object or data structure equivalent to the POJO. This, of course, also works in the reverse direction. (I'm adding "binary", because there are some binary formats not defined by Java that are architecture-independent, e.g., ASN.1.)

And, yes, JAXB works either way, but there are some difficulties if the XML is rather "outlandish", i.e., far away from what JAXB can handle or handle easily. But if you can design either the XML Schema or the Java classes, it works very well. JAXB being part of the JDK, you might prefer using it over other serializations if you need to go from Java to X or back. There are other languange binding for XML.

4 Comments

This says that the output format is hex, can you comment?
"hex" doesn't occur anywhere else here, except in your comment. It usually means "data represented by 0-9,A-F, two per byte." I don't see that here, either.
Maybe you didn't spot the link in my comment. On the linked page, under the header "The serialized format of an object", the file displays in hex; but perhaps it usually outputs in binary, and the article author purposefully converted to hex for the sake of space.
@wcr4 You can "output" anything "in hex(adecimal characters)". - You are confusing the actual representation with some display format. If the actual representation of integer 42 is one byte, you might display the byte as "2A" ("hexadecimal characters") or "00101010" ("binary characters") or "42" or "052" ("octal characters") or ... - Note that it says "displayed in hexadecimal format" in that article.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.