ObjectMapper writeValueAsString changes bytebuffer pos - java

I am trying to serialize a complex object to string that somewhere contains bytebuffer inside by using ObjectMapper for logging the response.
This changes the cursor position inside the bytebuffer and simply corrupts the response.
Code snippet that i am using:
import org.codehaus.jackson.map.ObjectMapper;
private static final ObjectMapper MAPPER = new ObjectMapper();
public static String serializeToString(final Object obj) {
Preconditions.checkArgument(obj != null, "Object to be serialized is null");
try {
final String str = MAPPER.writeValueAsString(obj);
if (Strings.isNullOrEmpty(str)) {
log.warn("Serialized to null/empty string");
}
return str;
} catch (final JsonGenerationException e) {
throw new IllegalArgumentException("Json generation exception occured in de-serializing obj", e);
} catch (final JsonMappingException e) {
throw new IllegalArgumentException("Json mapping exception occured in de-serializing obj", e);
} catch (final IOException e) {
throw new IllegalArgumentException("IO exception occured in de-serializing obj", e);
}
}
I passed above method a complex object having bytebuffer inside.
I printed bytebuffer before and after calling above method.
public static void main(final String[] args) throws SecurityException, NoSuchMethodException {
final String x =
"Random data i am using for this test for byte buffer. Random data i am using for this test for byte buffer";
final byte[] byteArr = x.getBytes();
final ByteBuffer bb = ByteBuffer.wrap(byteArr);
System.out.println("before bytebuffer :" + bb);
String stringData = SerializerUtil.serializeToString(bb); // In real i am passing a complex structure having
// bytebuffer inside
System.out.println(stringData);
System.out.println("after bytebuffer :" + bb);
}
Output:
before bytebuffer :java.nio.HeapByteBuffer[pos=0 lim=106 cap=106]
{"short":21089,"char":"\u6e64","int":1869422692,"long":7022344510808023405,"float":2.0790493E-19,"double":6.687717052371733E223,"direct":false,"readOnly":false}
after bytebuffer :java.nio.HeapByteBuffer[pos=28 lim=106 cap=106]
This change in (pos=0 to pos=28)position simply corrupts the response sent. Do we have any way to convert this complex object to string without affecting the byteBuffer?
Any help much appreciated.

Obviously, you don't want to serialize the ByteBuffer property as another structured class, but just the content, as a string. One way to do that is to use a #JsonProperty annotation on a method to tell the mapper to use that method instead of trying to serialize the field directly. Assuming you have a bean like this:
class Stuff {
private ByteBuffer data;
public Stuff() {
}
public Stuff(ByteBuffer data) {
super();
this.data = data;
}
public ByteBuffer getData() {
return data;
}
public void setData(ByteBuffer data) {
this.data = data;
}
#JsonProperty(value = "data")
public String convertData() {
return new String(data.array());
}
#JsonProperty("data")
public void convertData(String s) {
data = ByteBuffer.wrap(s.getBytes());
}
}
The mapper will now use the convertData methods for serializeing and deserializing the ByteBuffer data property, and you can still use normal java bean property methods.
Update:
Since the serialized class cannot be changed, here is an alternative method using som advanced JACKSON stuff. First, create custom serializer and deserializer:
static class ByteBufferSerializer extends JsonSerializer<ByteBuffer> {
#Override
public void serialize(ByteBuffer value, JsonGenerator jgen,
SerializerProvider provider) throws IOException,
JsonProcessingException {
jgen.writeString(new String(value.array()));
}
}
static class ByteBufferDeserializer extends JsonDeserializer<ByteBuffer> {
#Override
public ByteBuffer deserialize(JsonParser jp,
DeserializationContext context) throws IOException,
JsonProcessingException {
return ByteBuffer.wrap(jp.getText().getBytes());
}
}
Then, create a Mixin interface to provide the annotations for the properties that we cannot provide in the real target class:
static interface Mixin {
#JsonSerialize(using = ByteBufferSerializer.class, contentAs = String.class)
ByteBuffer getData();
#JsonDeserialize(using = ByteBufferDeserializer.class, contentAs = String.class)
void setData(ByteBuffer data);
}
Further, create a Module used to configure the object mapper, and add the mixin interface:
static class MyModule extends SimpleModule {
public MyModule() {
super("ByteBuffer wrangling");
}
#Override
public void setupModule(SetupContext context) {
context.setMixInAnnotations(Stuff.class, Mixin.class);
}
}
And, finally, register the module with the mapper:
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new MyModule());
VoilĂ , piece of cake! :-)

Related

Jackson serialize Object to JSON to base64 (without endless loop)

Is there a simple way to serialize an object using Jackson to base64 encoded JSON? (object -> JSON -> base64)
I tried using a custom StdSerializer, but this (of course) results in a endless loop:
class MySerializer extends StdSerializer<Foo> {
public void serialize(Foo value, JsonGenerator gen, SerializerProvider provider) {
StringWriter stringWriter = new StringWriter();
JsonGenerator newGen = gen.getCodec().getFactory().createGenerator(stringWriter);
gen.getCodec().getFactory().getCodec().writeValue(newGen, value);
String json = stringWriter.toString();
String base64 = new String(Base64.getEncoder().encode(json.getBytes()));
gen.writeString(base64);
}
}
A workaround is to copy all fields to another class and use that class for the intermediate representation:
class TmpFoo {
public String field1;
public int field2;
// ...
}
class MySerializer extends StdSerializer<Foo> {
public void serialize(Foo value, JsonGenerator gen, SerializerProvider provider) {
TmpFoo tmp = new TmpFoo();
tmp.field1 = value.field1;
tmp.field2 = value.field2;
// etc.
StringWriter stringWriter = new StringWriter();
JsonGenerator newGen = gen.getCodec().getFactory().createGenerator(stringWriter);
gen.getCodec().getFactory().getCodec().writeValue(newGen, tmp); // here "tmp" instead of "value"
String json = stringWriter.toString();
String base64 = new String(Base64.getEncoder().encode(json.getBytes()));
gen.writeString(base64);
}
}
Creating a new ObjectMapper is not desired, because I need all registered modules and serializers of the default ObjectMapper.
I was hoping for some easier way of achieving this.
EDIT: Example
Step 1: Java Object
class Foo {
String field1 = "foo";
int field2 = 42;
}
Step 2: JSON
{"field1":"foo","field2":42}
Step 3: Base64
eyJmaWVsZDEiOiJmb28iLCJmaWVsZDIiOjQyfQ==
According to this site, there is a workaround to avoid this recursion problem:
When we define a custom serializer, Jackson internally overrides the
original BeanSerializer instance [...] our SerializerProvider finds
the customized serializer every time, instead of the default one, and
this causes an infinite loop.
A possible workaround is using BeanSerializerModifier to store the
default serializer for the type Folder before Jackson internally
overrides it.
If I understood the workaround correctly, your Serializer should look like this:
class FooSerializer extends StdSerializer<Foo> {
private final JsonSerializer<Object> defaultSerializer;
public FooSerializer(JsonSerializer<Object> defaultSerializer) {
super(Foo.class);
this.defaultSerializer = defaultSerializer;
}
#Override
public void serialize(Foo value, JsonGenerator gen, SerializerProvider provider) throws IOException {
StringWriter stringWriter = new StringWriter();
JsonGenerator tempGen = provider.getGenerator().getCodec().getFactory().createGenerator(stringWriter);
defaultSerializer.serialize(value, tempGen, provider);
tempGen.flush();
String json = stringWriter.toString();
String base64 = new String(Base64.getEncoder().encode(json.getBytes()));
gen.writeString(base64);
}
}
In addition to the serializer, a modifier is needed:
public class FooBeanSerializerModifier extends BeanSerializerModifier {
#Override
public JsonSerializer<?> modifySerializer(
SerializationConfig config, BeanDescription beanDesc, JsonSerializer<?> serializer) {
if (beanDesc.getBeanClass().equals(Foo.class)) {
return new FooSerializer((JsonSerializer<Object>) serializer);
}
return serializer;
}
}
Example module:
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.setSerializerModifier(new FooBeanSerializerModifier());
mapper.registerModule(module);
EDIT:
I've added flush() to flush the JsonGenerator tempGen.
Also, I've created a minimal test enviroment with JUnit, which verifies your Example with Foo: The github repo can be found here.
EDIT: Alternative 2
Another (simple) option is using a wrapper class with generics:
public class Base64Wrapper<T> {
private final T wrapped;
private Base64Wrapper(T wrapped) {
this.wrapped = wrapped;
}
public T getWrapped() {
return this.wrapped;
}
public static <T> Base64Wrapper<T> of(T wrapped) {
return new Base64Wrapper<>(wrapped);
}
}
public class Base64WrapperSerializer extends StdSerializer<Base64Wrapper> {
public Base64WrapperSerializer() {
super(Base64Wrapper.class);
}
#Override
public void serialize(Base64Wrapper value, JsonGenerator gen, SerializerProvider provider) throws IOException {
StringWriter stringWriter = new StringWriter();
JsonGenerator tempGen = provider.getGenerator().getCodec().getFactory().createGenerator(stringWriter);
provider.defaultSerializeValue(value.getWrapped(), tempGen);
tempGen.flush();
String json = stringWriter.toString();
String base64 = new String(Base64.getEncoder().encode(json.getBytes()));
gen.writeString(base64);
}
}
An example usecase would be:
final ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addSerializer(new Base64WrapperSerializer());
mapper.registerModule(module);
final Foo foo = new Foo();
final Base64Wrapper<Foo> base64Wrapper = Base64Wrapper.of(foo);
final String base64Json = mapper.writeValueAsString(base64Wrapper);
This example can be found in this GitHub (branch: wrapper) repo, verifing you BASE64 String from your foo example with JUnit testing.
Instead of creating new object you may convert existing one into map. Like in the example below
import static java.nio.charset.StandardCharsets.UTF_8;
public class FooSerializer extends StdSerializer<Foo> {
public FooSerializer() {
super(Foo.class);
}
#Override
public void serialize(Foo foo, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) {
try {
ObjectMapper mapper = (ObjectMapper) jsonGenerator.getCodec();
var map = toMap(foo); // if you need class info for deserialization than use toMapWithClassInfo
String json = mapper.writeValueAsString(map);
jsonGenerator.writeString(Base64.getEncoder().encodeToString(json.getBytes(UTF_8)));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static Map<String, Object> toMap(Object o) throws Exception {
Map<String, Object> result = new HashMap<>();
Field[] declaredFields = o.getClass().getDeclaredFields();
for (Field field : declaredFields) {
field.setAccessible(true);
result.put(field.getName(), field.get(o));
}
return result;
}
public static Map<String, Object> toMapWithClassInfo(Object obj) throws Exception {
Map<String, Object> result = new HashMap<>();
BeanInfo info = Introspector.getBeanInfo(obj.getClass());
for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
Method reader = pd.getReadMethod();
if (reader != null)
result.put(pd.getName(), reader.invoke(obj));
}
return result;
}
}
I'm providing 2 ways of converting into map: with and without class info. Choose the one, applicable to your problem.
To serialize object jackson search #JsonValue method. You can add encodedJsonString method annotated by #JsonValue in Foo class.
Try with this:
#Getter
#Setter
public class Foo implements Serializable {
private static final long serialVersionUID = 1L;
public String field1;
public int field2;
#JsonValue
public String toEncodedJsonString() {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
new ObjectOutputStream(baos).writeObject(this);
return org.apache.commons.codec.binary.Base64.encodeBase64String(baos.toByteArray());
}catch (Exception ex){
}
return null;
}
}

Jackson encode all properties to base64

I have more than one class that I want to serialize using Jackson to generate Json, for example
public class A{
int id;
String name;
Object database;
... getter and setter
}
I need to encode all value of the json to base64, so I configure the object mapper like this
public class Base64Serializer<T> extends StdSerializer<T> {
private static final long serialVersionUID = 1L;
protected Base64Serializer(Class<?> t, boolean f) {
super(t, f);
}
#Override
public void serialize(T value, JsonGenerator jsonGenerator, SerializerProvider arg2) throws IOException {
String ecnodedOutput = Base64.getEncoder().encodeToString(((String) value).getBytes());
jsonGenerator.writeString(ecnodedOutput);
}
}
//Using the base64 Serializer to configure Object mapper
SimpleModule module = new SimpleModule();
module.addSerializer(new Base64Serializer(String.class, false));
objectMapper.registerModule(module);
objectMapper.writerWithDefaultPrettyPrinter().writeValue(outputStream,intanceOfClassA);
The problem is it's only encode the String type as the serializer only accept one type, is there any method to encode all field values, (long, int, object, etc..) I mean to encode all value of the json field regarding it type of string or int??
You could use following:
Serializer
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Base64;
public class Base64Serializer<T extends Serializable> extends StdSerializer<T> {
private static final long serialVersionUID = 1L;
protected Base64Serializer(Class<?> t, boolean f) {
super(t, f);
}
#Override
public void serialize(T value, JsonGenerator jsonGenerator, SerializerProvider arg2) throws IOException {
String ecnodedOutput = Base64.getEncoder().encodeToString(serialize(value));
jsonGenerator.writeString(ecnodedOutput);
}
public static byte[] serialize(Object obj) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(out);
os.writeObject(obj);
return out.toByteArray();
}
}
Registration and test:
public class SerializerTest {
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
addSerializers(module, Serializable.class, int.class, double.class, float.class, char.class, byte.class, short.class);
objectMapper.registerModule(module);
System.out.println(objectMapper.writeValueAsString(new A(10, "test", Arrays.asList(10000L, "TTTT2"))));
}
private static void addSerializers(SimpleModule module, Class... classes) {
Arrays.stream(classes).forEach(c -> module.addSerializer(new Base64Serializer(c, false)));
}
}
Output:
{
"id": "rO0ABXNyABFqYXZhLmxhbmcuSW50ZWdlchLioKT3gYc4AgABSQAFdmFsdWV4cgAQamF2YS5sYW5nLk51bWJlcoaslR0LlOCLAgAAeHAAAAAK",
"name": "rO0ABXQABHRlc3Q=",
"database": "rO0ABXNyABpqYXZhLnV0aWwuQXJyYXlzJEFycmF5TGlzdNmkPL7NiAbSAgABWwABYXQAE1tMamF2YS9sYW5nL09iamVjdDt4cHVyABdbTGphdmEuaW8uU2VyaWFsaXphYmxlO67QCaxT1+1JAgAAeHAAAAACc3IADmphdmEubGFuZy5Mb25nO4vkkMyPI98CAAFKAAV2YWx1ZXhyABBqYXZhLmxhbmcuTnVtYmVyhqyVHQuU4IsCAAB4cAAAAAAAACcQdAAFVFRUVDI="
}
Just in case if someone is looking for converting any kind of object to Base64 string using something like Jackson or Gson. You could do something like this:
Convert your object to string using Jackson/Gson:
str = objectMapper.writeValueAsString(obj)
OR
str = gson.toJson(obj)
Now you get bytes from your string using
base64str = str.getBytes()
Use java's Base64 class to convert bytes to Base64 string:
Base64.getEncoder().encodeToString(base64str)

Custom BigInteger JSON serializer

I am trying to implement a custom JSON serializer class to display object BigInteger values as string in the JSON response.
I have implememented a custom serializer class
public class CustomCounterSerializer extends StdSerializer<BigInteger> {
private static final long serialVersionUID = 5440920327083673598L;
public CustomCounterSerializer() {
this(BigInteger.class);
}
public CustomCounterSerializer(Class<BigInteger> vc) {
super(vc);
}
#Override
public void serialize(BigInteger value, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException {
BigInteger valueJson = value==null ? BigInteger.valueOf(0) : value;
jsonGenerator.writeString(valueJson.toString());
}
}
The problem I have is that I want to handle the null object values using the overridden method and pass 0 to the JSON string and not null.
I have written a JUnit test for this:
public class CustomCounterSerializerTest {
private ObjectMapper objectMapper = new ObjectMapper();
#After
public void tearDown() {
objectMapper = null;
}
#Test
public void testCustomSerializerWithNullValues() throws JsonParseException, JsonMappingException, IOException {
MyObject obj = new MyObject();
obj.setNonNullValue(BigInteger.valueOf(1);
String obj_ = objectMapper.writeValueAsString(obj);
assertThat(obj_).isNotNull();
assertTrue(obj_.contains("\"nonNullValue\":\"" + BigInteger.valueOf(1).toString() + "\",\"nullValue\":\""+ BigInteger.valueOf(0).toString() +"\""));
}
}
It fails as it contains null and not nullValue:"0".
But the null value of the object always goes to no args constructor and even like this(BigInteger.class) does not use my method and prints null.
You need to tell Jackson that it should use your serializer
even for null values. You do this by using #JsonSerializer
also with the nullsUsing parameter.
In your MyObject class it would look like this:
#JsonSerialize(using = CustomCounterSerializer.class,
nullsUsing = CustomCounterSerializer.class)
private BigInteger nullValue;

Spring JSON Serializer and Deserializer is not getting called

I have written custom serializer and deserializer for com.google.common.collect.Table class. But it is not getting called while persisting that object in MongoDB. I am using Spring 4, Spring-MongoDB 1.9 and Jackson 2.8.4. Below are the class and configuration. Could you please let me know what's wrong with this. I want these class to be called while persisting and retrieving from MongoDB.
public class TableSeserializer extends StdSerializer<Table> {
/**
*
*/
private static final long serialVersionUID = 1L;
public TableSeserializer() {
this(Table.class);
}
public TableSeserializer(Class<Table> t) {
super(t);
}
#SuppressWarnings("unchecked")
#Override
public void serialize(Table value, JsonGenerator gen, SerializerProvider provider)
throws IOException {
gen.writeStartObject();
value.rowMap().forEach((i,map) ->{
try {
gen.writeNumber((int)i);
((Map)map).forEach((k,v)->{
try {
gen.writeStartObject();
Object object = ((Map)map).get(k);
if (object instanceof HTMLInputTag) {
HTMLInputTag inputTag = (HTMLInputTag) object;
gen.writeObjectField(inputTag.getId(),inputTag);
}else{
gen.writeObjectField(object.toString(),object);
}
gen.writeEndObject();
} catch (IOException e) {
e.printStackTrace();
}
});
} catch (IOException e) {
e.printStackTrace();
}
});
gen.writeEndObject();
}
}
public class TableDeserializer extends StdDeserializer<Table<Integer, String, HTMLInputTag>> {
/**
*
*/
private static final long serialVersionUID = 1L;
protected TableDeserializer(Class<?> vc) {
super(vc);
}
public TableDeserializer() {
this(null);
}
#Override
public Table<Integer, String, HTMLInputTag> deserialize(JsonParser jsonparser, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
String data = jsonparser.getText();
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(data);
return null;
}
}
Spring configuration
#Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
SimpleModule module = new SimpleModule();
module.addSerializer(Table.class, new TableSeserializer());
module.addDeserializer(Table.class, new TableDeserializer());
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder().modules(module);
converters.add(jacksonConverter(builder));
}
#Bean
MappingJackson2HttpMessageConverter jacksonConverter(Jackson2ObjectMapperBuilder builder) {
return new MappingJackson2HttpMessageConverter(builder.build());
}
Annotation in class
public class OrganizationAttributeMetaData extends CommonDomainAttributes implements Cloneable, Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String attributeName;
private int orgid;
private String uniquecode; //3 Character unique code to uniquely identify
#JsonDeserialize(using=TableDeserializer.class)
#JsonSerialize(using=TableSeserializer.class)
Table<Integer, String, HTMLInputTag> htmlAttributes = null; //row,identifier and HTML
}
You really mixed some things ;) MongoDB indeed stores data in JSON-like format (BSON - Binary JSON) but it has nothing to do with Jackson - which is used (like the way you showed) to serialize/deserialize objects sent over HTTP.
In Spring Data MongoDB you need to implement and configure custom converters. More about that can be read here.

(De-)Serialize Bean in a custom way at runtime

Let's imagine I have the following POJO:
class Pojo {
String s;
Object o;
Map<String, String> m;
}
And at runtime, I want default serialization / deserialization for all properties except one. Typically, I want to replace a field by its ID in a database when serializing, similarly to this other question.
For example, I want to replace o by a string obtained from an external mapping (for example: object1 <=> "123" and object2 <=> "456"):
serialization: read o and replace (so if o is object1, serialize as string "123")
deserialization: read "123", query some table to get the original value of o back (i.e. object1), recreate a Pojo object with o = object1.
I understand that Modules would be one way to do that but I'm not sure how to use them while keeping the automatic BeanSerializer/Deserializer for the properties that don't need to be changed.
Can someone give an example (even contrived) or an alternative approach?
Notes:
I can't use annotations or Mixins as the changes are unknown at compile time (i.e. any properties might be changed in a way that is not determinable).
This other question points to using a CustomSerializerFactory, which seems to do the job. Unfortunately, the official site indicates that it is not the recommended approach any more and that modules should be used instead.
Edit
To be a little clearer, I can do the following with Mixins for example:
ObjectMapper mapper = new ObjectMapper(MongoBsonFactory.createFactory());
mapper.addMixInAnnotations(Pojo.class, PojoMixIn.class);
ObjectReader reader = mapper.reader(Pojo.class);
DBEncoder dbEncoder = DefaultDBEncoder.FACTORY.create();
OutputBuffer buffer = new BasicOutputBuffer();
dbEncoder.writeObject(buffer, o);
with the following Mixin:
abstract class PojoMixIn {
#JsonIgnore Object o;
}
And then add the required string to the JSON content. But I would need to know at compile time that it is the o field that needs to be replaced, which I don't.
I think #JsonSerialize and #JsonDeserialize is what you need. These annotations give you control on the serialization/deserialization of particular fields. This question shows elegant way to combine them into one annotation.
UPD. For this complex scenario you could take a look at BeanSerializerModifier/BeanDeserializerModifier classes. The idea is to modify general BeanSerializer/BeanDeserializer with your custom logic for particular fields and let basic implementation to do other stuff. Will post an example some time later.
UPD2. As I see, one of the way could be to use changeProperties method and assign your own serializer.
UPD3. Updated with working example of custom serializer. Deserialization could be done in similar way.
UPD4. Updated example with full custom serialization/deserialization. (I have used jakson-mapper-asl-1.9.8)
public class TestBeanSerializationModifiers {
static final String PropertyName = "customProperty";
static final String CustomValue = "customValue";
static final String BaseValue = "baseValue";
// Custom serialization
static class CustomSerializer extends JsonSerializer<Object> {
#Override
public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
String customValue = CustomValue; // someService.getCustomValue(value);
jgen.writeString(customValue);
}
}
static class MyBeanSerializerModifier extends BeanSerializerModifier {
#Override
public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BasicBeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) {
for (int i = 0; i < beanProperties.size(); i++) {
BeanPropertyWriter beanPropertyWriter = beanProperties.get(i);
if (PropertyName.equals(beanPropertyWriter.getName())) {
beanProperties.set(i, beanPropertyWriter.withSerializer(new CustomSerializer()));
}
}
return beanProperties;
}
}
// Custom deserialization
static class CustomDeserializer extends JsonDeserializer<Object> {
#Override
public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
// serialized value, 'customValue'
String serializedValue = jp.getText();
String baseValue = BaseValue; // someService.restoreOldValue(serializedValue);
return baseValue;
}
}
static class MyBeanDeserializerModifier extends BeanDeserializerModifier {
#Override
public BeanDeserializerBuilder updateBuilder(DeserializationConfig config, BasicBeanDescription beanDesc, BeanDeserializerBuilder builder) {
Iterator<SettableBeanProperty> beanPropertyIterator = builder.getProperties();
while (beanPropertyIterator.hasNext()) {
SettableBeanProperty settableBeanProperty = beanPropertyIterator.next();
if (PropertyName.equals(settableBeanProperty.getName())) {
SettableBeanProperty newSettableBeanProperty = settableBeanProperty.withValueDeserializer(new CustomDeserializer());
builder.addOrReplaceProperty(newSettableBeanProperty, true);
break;
}
}
return builder;
}
}
static class Model {
private String customProperty = BaseValue;
private String[] someArray = new String[]{"one", "two"};
public String getCustomProperty() {
return customProperty;
}
public void setCustomProperty(String customProperty) {
this.customProperty = customProperty;
}
public String[] getSomeArray() {
return someArray;
}
public void setSomeArray(String[] someArray) {
this.someArray = someArray;
}
}
public static void main(String[] args) {
SerializerFactory serializerFactory = BeanSerializerFactory
.instance
.withSerializerModifier(new MyBeanSerializerModifier());
DeserializerFactory deserializerFactory = BeanDeserializerFactory
.instance
.withDeserializerModifier(new MyBeanDeserializerModifier());
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializerFactory(serializerFactory);
objectMapper.setDeserializerProvider(new StdDeserializerProvider(deserializerFactory));
try {
final String fileName = "test-serialization.json";
// Store, "customValue" -> json
objectMapper.writeValue(new File(fileName), new Model());
// Restore, "baseValue" -> model
Model model = objectMapper.readValue(new File(fileName), Model.class);
} catch (IOException e) {
e.printStackTrace();
}
}
}

Categories

Resources