#Understanding Serialization in details

1 messages ยท Page 1 of 1 (latest)

vernal knoll
#

Regarding the topic serialization:
In a normal Java program when you design a class and instantiate its object, the object is in RAM(JVM) and it is treated as instructions.
When you want to serialize it (ie convert it to character stream and transfer it to a file or network), you want to tell java to see it as data and not instruction now.
Am I getting it right?
I am a CS major student so I want to look it things that way, maybe I'm very incorrect, please correct me.

cinder aspenBOT
#

<@&987246399047479336> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question ๐Ÿค–. Maybe it helps! In any case, a human is on the way ๐Ÿ‘. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#
TJ-Bot
Regarding the topic serialization: In a normal Java program when you design a class and instantiate its object, the object is in RAM(JVM) and it is treated as instructions. When you want to serialize it (ie convert it to character stream and transfer it

Serialization in Java is the process of converting an object into a byte stream, which can be saved to a file, sent over a network, or stored in a database. This allows the object's state to be saved and recreated later. To serialize an object, it must implement the Serializable interface.

Some key points to understand about serialization:

  1. Serialized objects can be deserialized back into their original form.
  2. Not all objects can be serialized, as transient fields and static fields are not serialized by default.
  3. Custom serialization can be implemented by providing readObject() and writeObject() methods in the class.

Useful links:

royal sun
#

The class live in ram as data, when you serialize it, you first need to decide the format, then your goal it to transform your object in that format and back.

midnight beacon
#

suppose u have an instance of a class like this one:

#
Person p = new Person("John", 20);
soft surge
midnight beacon
#

great, in ur RAM u now have a java object floating around with name John and age 20

#

but what if u want to persist it, maybe as a file

#

so u decide to write some code like maybe:

#
String text = "name: " + p.getName() + ", age: " + p.getAge();
Files.write(Path.of("person.txt"), text);
#

now u got a file person.txt with content

name: John, age: 20
#

great, u just serialized ur java object

#

now its content is in a file

#

easy, there is no magic to the idea of serialization

#

next u might want to be able to also read that file back into an object

#

so u would write some javacode that reads this file and recreates a person instance from its contents

#
String text = Files.read(Path.of("person.txt"));
String[] parts = text.split(", ");
String name = parts[0].split("name: ")[1];
String age = Integer.parseInt(parts[1].split("age: ")[1]);

Person p = new Person(name, age);
#

great, now uve got ur john back as java object

#

going beyond this simple example the main choice is which data format u want to pick for ur serialization

#

in this case we made a simple text file with a custom format

#

in practice u would typically choose something more widespread

#

CSV:

john;20
#

JSON:

{
  "name": "John",
  "age": 20
}
#

maybe XML:

<person>
  <name>John</name>
  <age>20</age>
</person>
#

maybe some binary formats like javas built-in serialization

#

or protobuf

#

or bson

#

there are many ways to go forward

#

but the idea is always the same

#

๐Ÿ™‚

#

and applications also go beyond persisting ur data as file

#

for example if u want to send data from one computer to another through "the internet"

#

then we often do that with HTTP, for example in rest-apis

#

so u need to make ur java object into something that can be send around and understood by other systems as well

#

(most common choice for that use case is JSON)

vernal knoll
royal sun
midnight beacon
#

it doesnt serialize the methods, it serializes state

#

so the instances fields

#

i mean, just look at the example

#

the writing and reading part is no magic. its simple text based stuff