com.adobe.platform.operation.internal.util.JsonUtil Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2019 Adobe
* All Rights Reserved.
*
* NOTICE: Adobe permits you to use, modify, and distribute this file in
* accordance with the terms of the Adobe license agreement accompanying
* it. If you have received this file from a source other than Adobe,
* then your use, modification, or distribution of it requires the prior
* written permission of Adobe.
*/
package com.adobe.platform.operation.internal.util;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.adobe.platform.operation.exception.SdkException;
public class JsonUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(JsonUtil.class);
private static ObjectMapper objectMapper = new ObjectMapper();
public static T deserializeJsonStream(InputStream inputStream, Class jsonType) {
try {
return objectMapper.readValue(inputStream, jsonType);
} catch (IOException e) {
LOGGER.error("Error while parsing json stream ", e);
throw new SdkException("Error while reading json stream ", e);
}
}
public static T deserializeJsonString(String jsonString, Class jsonType) {
try {
return objectMapper.readValue(jsonString, jsonType);
} catch (IOException e) {
LOGGER.error("Error while parsing json string ", e);
throw new SdkException(String.format("Error while reading json string %s", jsonString), e);
}
}
public static JsonNode readJsonTreeFromFile(File jsonFile) throws IOException {
return objectMapper.readTree(jsonFile);
}
public static JsonNode readJsonTreeFromString(String json) {
try {
return objectMapper.readTree(json);
} catch (IOException e) {
throw new SdkException("Error while reading json from string ", e);
}
}
}