org.fisco.bcos.sdk.transaction.tools.JsonUtils Maven / Gradle / Ivy
/**
* Copyright 2014-2019 the original author or 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
*
*
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 org.fisco.bcos.sdk.transaction.tools;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.fisco.bcos.sdk.transaction.model.exception.JsonException;
import org.fisco.bcos.sdk.utils.ObjectMapperFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* a useful toolkit of json based on Jackson.
*
* @author maojiayu
*/
public class JsonUtils {
protected static Logger log = LoggerFactory.getLogger(JsonUtils.class);
private static ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();
public static T fromJson(String json, Class clazz) {
try {
return fromJsonWithException(json, clazz);
} catch (Exception e) {
log.error("json is: " + json, e);
return null;
}
}
@SuppressWarnings("rawtypes")
public static T fromJson(String json, Class c, Class... t) {
try {
return fromJsonWithException(json, c, t);
} catch (IOException e) {
throw new JsonException(e);
}
}
public static T fromJson(String json, JavaType type) {
try {
return fromJsonWithException(json, type);
} catch (IOException e) {
throw new JsonException(e);
}
}
public static T fromJson(String json, TypeReference typeReference) {
try {
return objectMapper.readValue(json, typeReference);
} catch (Exception e) {
log.error("json is: " + json, e);
return null;
}
}
public static T fromJsonWithException(String json, Class clazz)
throws JsonParseException, JsonMappingException, IOException {
return objectMapper.readValue(json, clazz);
}
@SuppressWarnings("rawtypes")
public static T fromJsonWithException(String json, Class c, Class... t)
throws JsonParseException, JsonMappingException, IOException {
JavaType javaType = objectMapper.getTypeFactory().constructParametricType(c, t);
return objectMapper.readValue(json, javaType);
}
@SuppressWarnings("unchecked")
public static T fromJsonWithException(String json, JavaType type)
throws JsonParseException, JsonMappingException, IOException {
T ret = (T) objectMapper.readValue(json, type);
return ret;
}
public static List fromJsonList(String json, Class c) {
try {
return fromJsonListWithException(json, c);
} catch (IOException e) {
throw new JsonException(e);
}
}
@SuppressWarnings("unchecked")
public static List fromJsonListWithException(String json, Class c)
throws IOException {
JavaType type = getCollectionType(ArrayList.class, c);
return (List) objectMapper.readValue(json, type);
}
public static JavaType getCollectionType(Class> collectionClass, Class>... elementClasses) {
return objectMapper
.getTypeFactory()
.constructParametricType(collectionClass, elementClasses);
}
public static String toJsonWithException(Object o) throws JsonProcessingException {
return objectMapper.writeValueAsString(o);
}
public static String toJson(Object o) {
try {
return toJsonWithException(o);
} catch (Exception e) {
throw new JsonException(e);
}
}
public static Map convertValue(
Object req, Class keyClazz, Class valueClazz) {
Map ret =
objectMapper.convertValue(
req,
objectMapper
.getTypeFactory()
.constructMapType(Map.class, keyClazz, valueClazz));
return ret;
}
@SuppressWarnings("rawtypes")
public static T convertMap(Map map, Class retClazz) {
return objectMapper.convertValue(map, retClazz);
}
}