com.jporm.commons.json.jackson2.Jackson2JsonService Maven / Gradle / Ivy
The newest version!
/*******************************************************************************
* Copyright 2017 Francesco Cina'
*
* 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 com.jporm.commons.json.jackson2;
import java.io.IOException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.jporm.commons.json.JsonService;
public class Jackson2JsonService implements JsonService {
private final ObjectMapper objectMapper;
public Jackson2JsonService() {
objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
objectMapper.registerModule(new Jdk8Module());
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
}
public Jackson2JsonService(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
@Override
public String toJson(T object) {
try {
return objectMapper.writeValueAsString(object);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
@Override
public T fromJson(Class clazz, String json) {
try {
return objectMapper.readValue(json, clazz);
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy