com.codeheadsystems.oop.mock.converter.JsonConverter Maven / Gradle / Ivy
/*
* Copyright (c) 2022 Ned Wolpert
*
* 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.codeheadsystems.oop.mock.converter;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.InputStream;
import javax.inject.Inject;
import javax.inject.Singleton;
/**
* The type Json converter.
*/
@Singleton
public class JsonConverter {
private final ObjectMapper mapper;
/**
* Instantiates a new Json converter.
*
* @param mapper the mapper
*/
@Inject
public JsonConverter(final ObjectMapper mapper) {
this.mapper = mapper;
}
/**
* To json string.
*
* @param resource the resource
* @return the string
*/
public String toJson(final Object resource) {
try {
return mapper.writeValueAsString(resource);
} catch (JsonProcessingException e) {
throw new IllegalArgumentException("Unable to convert resource type:" + resource.getClass(), e);
}
}
/**
* Convert r.
*
* @param the type parameter
* @param json the json
* @param clazz the clazz
* @return the r
*/
public R convert(final String json, final Class clazz) {
try {
return mapper.readValue(json, clazz);
} catch (JsonProcessingException e) {
throw new IllegalArgumentException("Unable to convert json string to " + clazz, e);
}
}
/**
* Convert r.
*
* @param the type parameter
* @param inputStream the input stream
* @param clazz the clazz
* @return the r
*/
public R convert(final InputStream inputStream, final Class clazz) {
try {
return mapper.readValue(inputStream, clazz); // this will close the stream automatically.
} catch (IOException e) {
throw new IllegalArgumentException("Unable to convert input stream to " + clazz, e);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy