io.micronaut.jackson.serialize.JacksonObjectSerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of micronaut-jackson-databind Show documentation
Show all versions of micronaut-jackson-databind Show documentation
Core components supporting the Micronaut Framework
/*
* Copyright 2017-2020 original authors
*
* 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
*
* https://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 io.micronaut.jackson.serialize;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.micronaut.context.annotation.Bean;
import io.micronaut.core.serialize.ObjectSerializer;
import io.micronaut.core.serialize.exceptions.SerializationException;
import io.micronaut.core.type.Argument;
import io.micronaut.jackson.JacksonConfiguration;
import jakarta.inject.Singleton;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Optional;
/**
* An implementation of the {@link ObjectSerializer} interface for Jackson.
*
* @author Graeme Rocher
* @since 1.0
*/
@Singleton
@Bean(typed = JacksonObjectSerializer.class) // do not implement ObjectSerializer
public class JacksonObjectSerializer implements ObjectSerializer {
private final ObjectMapper objectMapper;
/**
* @param objectMapper To read/write JSON
*/
public JacksonObjectSerializer(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
@Override
public Optional serialize(Object object) throws SerializationException {
try {
return Optional.of(objectMapper.writeValueAsBytes(object));
} catch (JsonProcessingException e) {
throw new SerializationException("Error serializing object to JSON: " + e.getMessage(), e);
}
}
@Override
public void serialize(Object object, OutputStream outputStream) throws SerializationException {
try {
objectMapper.writeValue(outputStream, object);
} catch (IOException e) {
throw new SerializationException("Error serializing object to JSON: " + e.getMessage(), e);
}
}
@Override
public Optional deserialize(byte[] bytes, Class requiredType) throws SerializationException {
try {
return Optional.ofNullable(objectMapper.readValue(bytes, requiredType));
} catch (IOException e) {
throw new SerializationException("Error deserializing object from JSON: " + e.getMessage(), e);
}
}
@Override
public Optional deserialize(InputStream inputStream, Class requiredType) throws SerializationException {
try {
return Optional.ofNullable(objectMapper.readValue(inputStream, requiredType));
} catch (IOException e) {
throw new SerializationException("Error deserializing object from JSON: " + e.getMessage(), e);
}
}
@Override
public Optional deserialize(byte[] bytes, Argument requiredType) throws SerializationException {
try {
JavaType javaType = JacksonConfiguration.constructType(requiredType, objectMapper.getTypeFactory());
return Optional.ofNullable(objectMapper.readValue(bytes, javaType));
} catch (IOException e) {
throw new SerializationException("Error deserializing object from JSON: " + e.getMessage(), e);
}
}
@Override
public Optional deserialize(InputStream inputStream, Argument requiredType) throws SerializationException {
try {
JavaType javaType = JacksonConfiguration.constructType(requiredType, objectMapper.getTypeFactory());
return Optional.ofNullable(objectMapper.readValue(inputStream, javaType));
} catch (IOException e) {
throw new SerializationException("Error deserializing object from JSON: " + e.getMessage(), e);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy