Last active
July 25, 2019 04:23
-
-
Save abachar/fc3d4a50a3d6ac1f65f8d243242818a4 to your computer and use it in GitHub Desktop.
Java serialization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package consulting.crafters.serialization; | |
import com.esotericsoftware.kryo.Kryo; | |
import com.esotericsoftware.kryo.io.Input; | |
import com.esotericsoftware.kryo.io.Output; | |
import com.fasterxml.jackson.core.JsonProcessingException; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.databind.SerializationFeature; | |
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | |
import com.github.javafaker.Faker; | |
import lombok.AllArgsConstructor; | |
import lombok.Data; | |
import lombok.NoArgsConstructor; | |
import lombok.val; | |
import java.io.ByteArrayInputStream; | |
import java.io.ByteArrayOutputStream; | |
import java.time.LocalDate; | |
import java.util.List; | |
import java.util.stream.IntStream; | |
import static java.time.ZoneId.systemDefault; | |
import static java.util.stream.Collectors.toList; | |
import static org.apache.commons.io.FileUtils.byteCountToDisplaySize; | |
import static org.assertj.core.api.Assertions.assertThat; | |
public class Main { | |
private final static ObjectMapper objectMapper; | |
private final static Kryo kryo; | |
static { | |
objectMapper = new ObjectMapper() | |
.registerModule(new JavaTimeModule()) | |
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); | |
kryo = new Kryo(); | |
kryo.register(Person.class); | |
} | |
public static void main(String[] args) throws Exception { | |
val faker = new Faker(); | |
val people = new People(IntStream.range(1, 50000) | |
.mapToObj(it -> new Person( | |
faker.name().firstName(), | |
faker.name().lastName(), | |
faker.date().birthday(20, 60).toInstant().atZone(systemDefault()).toLocalDate(), | |
new Address( | |
faker.address().streetAddress(), | |
faker.address().zipCode(), | |
faker.address().city() | |
) | |
)) | |
.collect(toList())); | |
val jsonBytes = serializeUsingJackson(people); | |
val kryoOut = serializeUsingKryo(people); | |
System.out.println("Size of jackson serialized = " + byteCountToDisplaySize(jsonBytes.length)); // = 7 MB | |
System.out.println("Size of kyro serialized = " + byteCountToDisplaySize(kryoOut.length)); // = 2 MB | |
val jsonPeople = deserializeUsingJackson(jsonBytes); | |
val kryoPeople = deserializeUsingKryo(kryoOut); | |
assertThat(jsonPeople).isEqualTo(people); | |
assertThat(kryoPeople).isEqualTo(people); | |
} | |
private static byte[] serializeUsingJackson(People people) throws JsonProcessingException { | |
return objectMapper | |
.writeValueAsString(people) | |
.getBytes(); | |
} | |
private static People deserializeUsingJackson(byte[] bytes) throws Exception { | |
return objectMapper.readValue(bytes, People.class); | |
} | |
private static byte[] serializeUsingKryo(People people) { | |
val out = new ByteArrayOutputStream(); | |
val kryoOutput = new Output(out); | |
kryo.writeObject(kryoOutput, people); | |
kryoOutput.flush(); | |
kryoOutput.close(); | |
return out.toByteArray(); | |
} | |
private static People deserializeUsingKryo(byte[] bytes) { | |
val input = new Input(new ByteArrayInputStream(bytes)); | |
val result = kryo.readObject(input, People.class); | |
input.close(); | |
return result; | |
} | |
} | |
@Data | |
@NoArgsConstructor | |
@AllArgsConstructor | |
class People { | |
private List<Person> people; | |
} | |
@Data | |
@NoArgsConstructor | |
@AllArgsConstructor | |
class Person { | |
private String firstName; | |
private String lastName; | |
private LocalDate birthday; | |
private Address address; | |
} | |
@Data | |
@NoArgsConstructor | |
@AllArgsConstructor | |
class Address { | |
private String street; | |
private String zipCode; | |
private String city; | |
} | |
// compile group: 'org.projectlombok', name: 'lombok', version:'1.16.20' | |
// compile group: 'com.esotericsoftware', name: 'kryo', version:'4.0.2' | |
// compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version:'2.9.4' | |
// compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version:'2.9.4' | |
// compile group: 'commons-io', name: 'commons-io', version:'2.3' | |
// compile group: 'com.github.javafaker', name: 'javafaker', version:'0.16' | |
// compile group: 'org.assertj', name: 'assertj-core', version:'3.11.1' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment