Java Serialize List

Java Serialize List 5,5/10 2735 reviews
Active1 year, 2 months ago

What is the Java analogue of .NET's XML serialization?

Raedwald

How to serialize HashMap in java By Chaitanya Singh Filed Under: Java Collections HashMap class is serialized by default which means we need not to implement Serializable interface in order to make it eligible for Serialization. Given an list of user defined objects, we would like to convert list of pojo objects to JSON (and JSON to list of objects). We will use the Google gson library to achieve the conversion. We will create Person class and we will perform the following operations with Person class. Convert List of Person objects to JSON. FileToSave.txt FILE content after serialization as byte stream. As printed on the console below, we can observe that our serialization and deserialization methods in JAVA program were able to serialize and deserialize the data value object, respectively.

28.8k23 gold badges100 silver badges171 bronze badges
Dmitry ShechtmanDmitry Shechtman
3,1784 gold badges22 silver badges25 bronze badges

closed as off-topic by Samuel LiewSep 27 '18 at 9:51

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • 'Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.' – Samuel Liew
If this question can be reworded to fit the rules in the help center, please edit the question.

11 Answers

2008 AnswerThe 'Official' Java API for this is now JAXB - Java API for XML Binding. See Tutorial by Oracle. The reference implementation lives at http://jaxb.java.net/

2018 Update Note that the Java EE and CORBA Modules are deprecated in SE in JDK9 and to be removed from SE in JDK11. Therefore, to use JAXB it will either need to be in your existing enterprise class environment bundled by your e.g. app server, or you will need to bring it in manually.

CheekysoftCheekysoft
27.2k17 gold badges63 silver badges82 bronze badges

XStream is pretty good at serializing object to XML without much configuration and money! (it's under BSD license).

We used it in one of our project to replace the plain old java-serialization and it worked almost out of the box.

Boern
3,5173 gold badges35 silver badges56 bronze badges
Barak SchillerBarak Schiller

You may want to look at the Simple XML Serialization project. It is the closest thing I've found to the System.Xml.Serialization in .Net.

Basil Bourque
137k38 gold badges452 silver badges643 bronze badges
ARKBANARKBAN
1,9514 gold badges18 silver badges22 bronze badges

JAXB is part of JDK standard edition version 1.6+. So it is FREE and no extra libraries to download and manage.A simple example can be found here

XStream seems to be dead. Last update was on Dec 6 2008.Simple seems as easy and simpler as JAXB but I could not find any licensing information to evaluate it for enterprise use.

so_mvso_mv
3,2544 gold badges25 silver badges39 bronze badges

Worth mentioning that since version 1.4, Java had the classes java.beans.XMLEncoder and java.beans.XMLDecoder. These classes perform XML encoding which is at least very comparable to XML Serialization and in some circumstances might do the trick for you.

If your class sticks to the JavaBeans specification for its getters and setters, this method is straightforward to use and you don't need a schema. With the following caveats:

Java Serialize Object

  • As with normal Java serialization
    • coding and decoding run over a InputStream and OutputStream
    • the process uses the familar writeObject and readObject methods
  • In contrast to normal Java serialization
    • the encoding but also decoding causes constructors and initializers to be invoked
    • encoding and decoding work regardless if your class implements Serializable or not
    • transient modifiers are not taken into account
    • works only for public classes, that have public constructors

For example, take the following declaration:

Executing this code:

Would result in the following file:

MishaxMishax
2,7815 gold badges27 silver badges54 bronze badges

XMLBeans works great if you have a schema for your XML. It creates Java objects for the schema and creates easy to use parse methods.

John MeagherJohn Meagher
13k13 gold badges48 silver badges56 bronze badges

If you're talking about automatic XML serialization of objects, check out Castor:

Castor is an Open Source data binding framework for Java[tm]. It's the shortest path between Java objects, XML documents and relational tables. Castor provides Java-to-XML binding, Java-to-SQL persistence, and more.

TheoTheo
116k16 gold badges121 silver badges161 bronze badges

Usually I use jaxb or XMLBeans if I need to create objects serializable to XML. Now, I can see that XStream might be very useful as it's nonintrusive and has really simple api. I'll play with it soon and probably use it. The only drawback I noticed is that I can't create object's id on my own for cross referencing.

Java serialize list of objects to string

@Barak Schiller
Thanks for posting link to XStream!

Bartosz Bierkowski

Serialize Json List Java

Bartosz Bierkowski
2,6411 gold badge17 silver badges17 bronze badges
CheesoCheeso
139k78 gold badges412 silver badges647 bronze badges

if you want a structured solution (like ORM) then JAXB2 is a good solution.

If you want a serialization like DOT NET then you could use Long Term Persistence of JavaBeans Components

The choice depends on use of serialization.

Andrew Thompson
155k29 gold badges172 silver badges363 bronze badges
m.genovam.genova
user4067649user4067649

Not the answer you're looking for? Browse other questions tagged javaxmlserialization or ask your own question.

Active6 years, 2 months ago

As an exercise, I am creating a list of Books as a LinkedList and using the Comparator interface to sort them by author or title.First, I create a class of books and ensure that it will print to screen the way I want it to:

Java Serialize Object

Next, I created a LinkedList that takes Object Book:

A class that implements Comparator is created that sorts according to title/author and displays them on a JTextArea inside of my main frame. All of that is working just fine, but there's one glaring bug..I cannot save the file!

I've tried a class that implements Serializable that takes the LinkedList as a argument and then writes a .obj file. When I load it, it fails. It creates the file, but I usually get a NotSerializableException. I also tried saving the file as .ser as I was told that this could make it easier to save it, but this failed on loading, as well.

Does anyone know a good method for serializing a LinkedList using a BufferedReader? Or is there another approach to this?Thank you in advance for your time reading through this question, and also thank you for any advice, comments, or answers that you can provide. Thanks, guys.

Java Serialize List To String

Java serialize list jackson

Addition:This is the entire code:

Jeremy Johnson
Jeremy JohnsonJeremy Johnson

Java Serialize List To Json

3611 gold badge3 silver badges17 bronze badges

1 Answer

make your Book class Serializable like this

The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.So you don't need to implement any method just declaring is enough.
according to java docs

Range rover sport owners manual. Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.

update to resolve current issue
You have implemented de-serialization process wrongly.This is the correct implementation.Try this you will get desired result.

PrabhakerPrabhaker

Java Serialize List Of Free

6,8721 gold badge14 silver badges22 bronze badges

Java Serialize List Of Objects

Not the answer you're looking for? Browse other questions tagged javaserializationlinked-list or ask your own question.