[BAEL-4268]Different Serialization Approaches for Java (#10928)
* [BAEL-4268]Different Serialization Approaches for Java Code Changes * [BAEL-4268]Different Serialization Approaches for Java Code formatting changes * [BAEL-4268]Different Serialization Approaches for Java Added user proto file * [BAEL-4268]Different Serialization Approaches for Java updated pom.xml * [BAEL-4268]Different Serialization Approaches for Java Fixed indentation in pom.xml * [BAEL-4268]Different Serialization Approaches for Java Updated pom.xml * [BAEL-4268]Different Serialization Approaches for Java Fixed indentation in pom.xml Co-authored-by: MeenaGawande <MeenaGawande@users.noreply.github.com>
This commit is contained in:
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.serialization.protocols;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.apache.thrift.TException;
|
||||
import org.apache.thrift.protocol.TBinaryProtocol;
|
||||
import org.apache.thrift.protocol.TProtocol;
|
||||
import org.apache.thrift.transport.TMemoryBuffer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class ApacheThriftSerializationUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenUsingThriftForSerialization_ThenDataIsSameAfterDeserialization() throws TException {
|
||||
|
||||
User user = new User();
|
||||
user.setId(2);
|
||||
user.setName("Greg");
|
||||
|
||||
TMemoryBuffer trans = new TMemoryBuffer(4096);
|
||||
TProtocol proto = new TBinaryProtocol(trans);
|
||||
|
||||
proto.writeI32(user.getId());
|
||||
proto.writeString(user.getName());
|
||||
|
||||
int userId = proto.readI32();
|
||||
String userName = proto.readString();
|
||||
|
||||
assertEquals(2, userId);
|
||||
assertEquals("Greg", userName);
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.serialization.protocols;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class GoogleProtocolBufferUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenUsingProtocolBuffersSerialization_ThenObjectIsTheSameAfterDeserialization() throws IOException {
|
||||
|
||||
String filePath = "src/test/resources/protocols/usersproto";
|
||||
|
||||
UserProtos.User user = UserProtos.User.newBuilder().setId(1234).setName("John Doe").build();
|
||||
FileOutputStream fos = new FileOutputStream(filePath);
|
||||
user.writeTo(fos);
|
||||
|
||||
UserProtos.User deserializedUser = UserProtos.User.newBuilder().mergeFrom(new FileInputStream(filePath)).build();
|
||||
|
||||
assertEquals(1234, deserializedUser.getId());
|
||||
assertEquals("John Doe", deserializedUser.getName());
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package com.baeldung.serialization.protocols;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
public class GsonSerializationUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenSerializedUsingGson_ThenObjectIsSameAfterDeserialization() {
|
||||
|
||||
User user = new User();
|
||||
user.setId(1);
|
||||
user.setName("Mark");
|
||||
|
||||
String filePath = "src/test/resources/protocols/gson_user.json";
|
||||
|
||||
try (Writer writer = new FileWriter(filePath)) {
|
||||
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
gson.toJson(user, writer);
|
||||
|
||||
assertTrue(Files.exists(Paths.get(filePath)));
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
try {
|
||||
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
User deserializedUser = gson.fromJson(new FileReader(filePath), User.class);
|
||||
|
||||
assertEquals(1, deserializedUser.getId());
|
||||
assertEquals("Mark", deserializedUser.getName());
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
package com.baeldung.serialization.protocols;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class JacksonSerializationUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenUsingJacksonAPIForJSONSerialization_thenDeserializeCorrectObject() throws IOException {
|
||||
|
||||
User user = new User();
|
||||
user.setId(1);
|
||||
user.setName("Mark Jonson");
|
||||
|
||||
String filePath = "src/test/resources/protocols/jackson_user.json";
|
||||
|
||||
File file = new File(filePath);
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.writeValue(file, user);
|
||||
|
||||
User deserializedUser = mapper.readValue(new File(filePath), User.class);
|
||||
|
||||
assertEquals(1, deserializedUser.getId());
|
||||
assertEquals("Mark Jonson", deserializedUser.getName());
|
||||
}
|
||||
|
||||
public static List<User> populateListOfUsers() {
|
||||
User user1 = new User();
|
||||
user1.setId(1);
|
||||
user1.setName("Mark Jonson");
|
||||
|
||||
User user2 = new User();
|
||||
user2.setId(2);
|
||||
user2.setName("Johny Beth");
|
||||
|
||||
User user3 = new User();
|
||||
user3.setId(3);
|
||||
user3.setName("Eliza Green");
|
||||
|
||||
User user4 = new User();
|
||||
user4.setId(4);
|
||||
user4.setName("Monica Doe");
|
||||
|
||||
List<User> users = new ArrayList<>();
|
||||
users.add(user1);
|
||||
users.add(user2);
|
||||
users.add(user3);
|
||||
users.add(user4);
|
||||
|
||||
return users;
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.serialization.protocols;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class JavaNativeSerializationUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenUsingJavaNativeSerialization_ThenObjectIsTheSameAfterDeserialization() throws IOException, ClassNotFoundException {
|
||||
|
||||
User user = new User();
|
||||
user.setId(1);
|
||||
user.setName("Mark");
|
||||
|
||||
String filePath = "src/test/resources/protocols/user.txt";
|
||||
|
||||
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
|
||||
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
|
||||
objectOutputStream.writeObject(user);
|
||||
objectOutputStream.flush();
|
||||
objectOutputStream.close();
|
||||
|
||||
FileInputStream fileInputStream = new FileInputStream(filePath);
|
||||
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
|
||||
User deserializedUser = (User) objectInputStream.readObject();
|
||||
objectInputStream.close();
|
||||
|
||||
assertEquals(1, deserializedUser.getId());
|
||||
assertEquals("Mark", deserializedUser.getName());
|
||||
}
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
package com.baeldung.serialization.protocols;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.esotericsoftware.yamlbeans.YamlReader;
|
||||
import com.esotericsoftware.yamlbeans.YamlWriter;
|
||||
|
||||
public class YAMLSerializationUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenUsingYAMLBeansForSerialization_thenDeserializeCorrectMap() throws IOException {
|
||||
|
||||
String filePath = "src/test/resources/protocols/yamlbeans_user.yaml";
|
||||
|
||||
YamlWriter writer = new YamlWriter(new FileWriter(filePath));
|
||||
writer.write(populateUserMap());
|
||||
writer.close();
|
||||
|
||||
YamlReader reader = new YamlReader(new FileReader(filePath));
|
||||
Object object = reader.read();
|
||||
reader.close();
|
||||
|
||||
assertTrue(object instanceof Map);
|
||||
Map<String, User> deserializedUsers = (Map<String, User>) object;
|
||||
|
||||
assertEquals(4, deserializedUsers.size());
|
||||
assertEquals("Mark Jonson", (deserializedUsers.get("User1").getName()));
|
||||
assertEquals(1, (deserializedUsers.get("User1").getId()));
|
||||
}
|
||||
|
||||
private Map<String, User> populateUserMap() {
|
||||
|
||||
User user1 = new User();
|
||||
user1.setId(1);
|
||||
user1.setName("Mark Jonson");
|
||||
|
||||
User user2 = new User();
|
||||
user2.setId(2);
|
||||
user2.setName("Johny Beth");
|
||||
|
||||
User user3 = new User();
|
||||
user3.setId(3);
|
||||
user3.setName("Eliza Green");
|
||||
|
||||
User user4 = new User();
|
||||
user4.setId(4);
|
||||
user4.setName("Monica Doe");
|
||||
|
||||
Map<String, User> users = new LinkedHashMap<>();
|
||||
users.put("User1", user1);
|
||||
users.put("User2", user2);
|
||||
users.put("User3", user3);
|
||||
users.put("User4", user4);
|
||||
|
||||
return users;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"id": 1,
|
||||
"name": "Mark"
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
{"id":1,"name":"Mark Jonson"}
|
||||
@@ -0,0 +1,11 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package protobuf;
|
||||
|
||||
option java_package = "com.baeldung.serialization.protocols";
|
||||
option java_outer_classname = "UserProtos";
|
||||
|
||||
message User {
|
||||
int32 id = 1;
|
||||
string name = 2;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1 @@
|
||||
Ò John Doe
|
||||
@@ -0,0 +1,13 @@
|
||||
!java.util.LinkedHashMap
|
||||
User1: !com.baeldung.serialization.protocols.User
|
||||
id: 1
|
||||
name: Mark Jonson
|
||||
User2: !com.baeldung.serialization.protocols.User
|
||||
id: 2
|
||||
name: Johny Beth
|
||||
User3: !com.baeldung.serialization.protocols.User
|
||||
id: 3
|
||||
name: Eliza Green
|
||||
User4: !com.baeldung.serialization.protocols.User
|
||||
id: 4
|
||||
name: Monica Doe
|
||||
Reference in New Issue
Block a user