com.codepoetics.octarine.json.serialisation.ReflectiveRecordSerialiser Maven / Gradle / Ivy
package com.codepoetics.octarine.json.serialisation;
import com.codepoetics.octarine.records.Key;
import com.codepoetics.octarine.records.Record;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import java.io.IOException;
import java.util.Map;
import java.util.stream.Stream;
public class ReflectiveRecordSerialiser extends JsonSerializer {
public static ObjectMapper mapperWith(JsonSerializer>...extraSerialisers) {
ObjectMapper mapper = new ObjectMapper();
SimpleModule simpleModule = new SimpleModule("SimpleModule",
new Version(1,0,0,null));
simpleModule.addSerializer(new ReflectiveRecordSerialiser());
Stream.of(extraSerialisers).forEach(simpleModule::addSerializer);
mapper.registerModules(simpleModule);
return mapper;
}
public static String toString(Record record) throws JsonProcessingException {
return mapperWith().writeValueAsString(record);
}
@Override
public Class handledType() { return Record.class; }
@Override
public void serialize(Record o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeStartObject();
for (Map.Entry, Object> entry : o.values().entrySet()) {
jsonGenerator.writeFieldName(entry.getKey().name());
serializerProvider.defaultSerializeValue(entry.getValue(), jsonGenerator);
}
jsonGenerator.writeEndObject();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy