https://github.com/emahtab/serialization-and-deserialization
Serialization Deserialization in Java
https://github.com/emahtab/serialization-and-deserialization
jackson-databind java serialization-deserialization serialize
Last synced: about 1 year ago
JSON representation
Serialization Deserialization in Java
- Host: GitHub
- URL: https://github.com/emahtab/serialization-and-deserialization
- Owner: eMahtab
- Created: 2024-11-04T05:16:32.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-11-27T13:03:19.000Z (over 1 year ago)
- Last Synced: 2025-02-02T03:19:10.807Z (over 1 year ago)
- Topics: jackson-databind, java, serialization-deserialization, serialize
- Homepage:
- Size: 23.4 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Example 1 : Serialization and Deserialization using Java API
## Step 1 : implement the Serializable interface
```java
import java.io.Serializable;
public class Person implements Serializable {
private static final long serialVersionUID = 1L; // recommended for Serializable classes
private String name;
private int age;
private String address;
public Person(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getAddress() {
return address;
}
@Override
public String toString() {
return "Person{name='" + name + "', age=" + age + ", address='" + address + "'}";
}
}
```
### Step 2 : Serialize (write) the Serializable object to a file
```java
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;
public class SerializeExample {
public static void main(String[] args) {
Person person = new Person("Alice", 30, "123 Main St, Wonderland");
try (FileOutputStream fileOut = new FileOutputStream("person.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut)) {
out.writeObject(person);
System.out.println("Person object has been serialized to person.ser");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
### Step 3 : Deserialize (read) the object from the file
```java
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.IOException;
public class DeserializeExample {
public static void main(String[] args) {
try (FileInputStream fileIn = new FileInputStream("person.ser");
ObjectInputStream in = new ObjectInputStream(fileIn)) {
Person person = (Person) in.readObject();
System.out.println("Deserialized Person object: " + person);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
```
# Example 2 : Serialization and Deserialization using external libraries e.g. Jackson
```xml
com.fasterxml.jackson.core
jackson-databind
2.15.2
```
```java
// User.java
public class User {
private String id;
private String name;
private int age;
// Constructors
public User() {
}
public User(String id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
// Getters and Setters
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "name=" + name + ", id=" + id + ", age=" + age;
}
}
// Jackson.java
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Jackson {
public static void main(String[] args) {
ObjectMapper objectMapper = new ObjectMapper();
User user = new User("1", "Mahtab Alam", 31);
try {
String jsonAsString = objectMapper.writeValueAsString(user);
System.out.println(jsonAsString);
System.out.println(jsonAsString.length());
String str = "{\"id\":\"1\",\"name\":\"Mahtab Alam\",\"age\":31}";
System.out.println("STR :" + str);
System.out.println(str.length());
System.out.println(jsonAsString.equals(str)); // true
User obj = objectMapper.readValue(str, User.class);
System.out.println(obj);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
```
### Code Execution Output :
```
{"id":"1","name":"Mahtab Alam","age":31}
40
STR :{"id":"1","name":"Mahtab Alam","age":31}
40
true
name=Mahtab Alam, id=1, age=31
```
## Some notes regarding storing Serialized Java object JSON as a String
1. The backslash (\) is an escape character in Java.
2. Placing a backslash before the double quote (\") tells the compiler that the double quote is part of the string, not the end of it.
3. When backslash is just one, it does not count towards the String length, length() function