JDKSerializabilityTest.java
package com.fasterxml.jackson.datatype.jdk8;
import java.io.*;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.databind.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class JDKSerializabilityTest extends ModuleTestBase
{
static class BooleanBean {
public Optional<Boolean> value;
public BooleanBean() { }
public BooleanBean(Boolean b) {
value = Optional.ofNullable(b);
}
}
@Test
public void testJDKSerializability() throws Exception {
final BooleanBean input = new BooleanBean(true);
ObjectMapper mapper = mapperWithModule();
String json1 = mapper.writeValueAsString(input);
// validate we can still use it to deserialize jackson objects
ObjectMapper thawedMapper = serializeAndDeserialize(mapper);
String json2 = thawedMapper.writeValueAsString(input);
assertEquals(json1, json2);
BooleanBean result = thawedMapper.readValue(json1, BooleanBean.class);
assertEquals(input.value, result.value);
}
private ObjectMapper serializeAndDeserialize(ObjectMapper mapper) throws Exception {
//verify serialization
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream outputStream = new ObjectOutputStream(byteArrayOutputStream);
outputStream.writeObject(mapper);
byte[] serializedBytes = byteArrayOutputStream.toByteArray();
//verify deserialization
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(serializedBytes);
ObjectInputStream inputStream = new ObjectInputStream(byteArrayInputStream);
Object deserializedObject = inputStream.readObject();
return (ObjectMapper) deserializedObject;
}
}