Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.vertigo.test.module;
- import java.io.IOException;
- import java.util.TimeZone;
- import com.fasterxml.jackson.annotation.JsonAutoDetect;
- import com.fasterxml.jackson.annotation.PropertyAccessor;
- import com.fasterxml.jackson.core.JsonGenerator;
- import com.fasterxml.jackson.core.JsonParser;
- import com.fasterxml.jackson.core.JsonProcessingException;
- import com.fasterxml.jackson.databind.DeserializationConfig;
- import com.fasterxml.jackson.databind.DeserializationContext;
- import com.fasterxml.jackson.databind.JsonDeserializer;
- import com.fasterxml.jackson.databind.JsonSerializer;
- import com.fasterxml.jackson.databind.MapperFeature;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.fasterxml.jackson.databind.SerializationFeature;
- import com.fasterxml.jackson.databind.SerializerProvider;
- import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
- import com.fasterxml.jackson.databind.annotation.JsonSerialize;
- import com.fasterxml.jackson.databind.cfg.ContextAttributes;
- import com.vertigo.model.json.serialization.opt.OptModule;
- public class ObjectMapperModule {
- private static final String TEH_MAPPER = "tehMapper";
- // @Override
- protected ObjectMapper configure() {
- ObjectMapper mapper = new ObjectMapper().setVisibility(PropertyAccessor.GETTER,
- JsonAutoDetect.Visibility.PUBLIC_ONLY);
- mapper.setTimeZone(TimeZone.getDefault());
- mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
- mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
- mapper.configure(MapperFeature.USE_ANNOTATIONS, true);
- mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
- mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
- mapper.configure(JsonParser.Feature.ALLOW_NUMERIC_LEADING_ZEROS, true);
- //mapper.registerModule(new JodaModule());
- mapper.registerModule(new OptModule());
- // bind(ObjectMapper.class).toInstance(mapper);
- return mapper;
- }
- public static class ModelFromString extends JsonDeserializer<Model> {
- @Override
- public Model deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
- Model m = new Model();
- ContextAttributes ats = ctxt.getConfig().getAttributes();
- System.out.println("om = " + ats.getAttribute(TEH_MAPPER));
- m.str = p.getText();
- return m;
- }
- }
- public static class ModelToString extends JsonSerializer<Model> {
- @Override
- public void serialize(Model value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
- gen.writeString(value.str);
- }
- }
- public static class Model<T> {
- public String str;
- public Model() {
- }
- }
- public static class UserM {
- @JsonSerialize(using = ModelToString.class)
- @JsonDeserialize(using = ModelFromString.class)
- Model model;
- public Model getModel() {
- return model;
- }
- public void setModel(Model rate) {
- this.model = rate;
- }
- }
- public static void main(String[] args) throws Exception {
- ObjectMapperModule omm = new ObjectMapperModule();
- ObjectMapper mapper = omm.configure();
- DeserializationConfig conf = mapper.getDeserializationConfig()
- .withAttribute(TEH_MAPPER, mapper);
- mapper.setConfig(conf);
- UserM userM = new UserM();
- Model model = new Model();
- model.str = "W33ha";
- userM.setModel(model);
- String s = mapper.writeValueAsString(userM);
- System.out.println("s = " + s);
- UserM userM1 = mapper.readValue(s, UserM.class);
- System.out.println("userM1 = " + userM1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement