es.logongas.ix3.web.json.impl.JsonReaderImplJackson.java Source code

Java tutorial

Introduction

Here is the source code for es.logongas.ix3.web.json.impl.JsonReaderImplJackson.java

Source

/*
 * Copyright 2013 Lorenzo Gonzlez.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package es.logongas.ix3.web.json.impl;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import es.logongas.ix3.dao.DataSession;
import es.logongas.ix3.web.json.beanmapper.BeanMapper;
import es.logongas.ix3.web.json.JsonReader;

/**
 *
 * @author Lorenzo Gonzlez
 */
public class JsonReaderImplJackson implements JsonReader {
    private final Class clazz;
    private final ObjectMapper objectMapper;

    public JsonReaderImplJackson(Class clazz) {
        objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
        SimpleModule module = new SimpleModule();
        module.addDeserializer(java.util.Date.class, new DateDeserializer());
        objectMapper.registerModule(module);
        this.clazz = clazz;
    }

    @Override
    public Object fromJson(String json, DataSession dataSession) {
        try {
            return objectMapper.readValue(json, clazz);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    @Override
    public Object fromJson(String json, BeanMapper beanMapper, DataSession dataSession) {
        throw new UnsupportedOperationException("No se permite el uso de BeanMapper en esta implementacion");
    }
}