org.cloudfoundry.identity.uaa.util.JsonUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cloudfoundry-identity-metrics-data Show documentation
Show all versions of cloudfoundry-identity-metrics-data Show documentation
Cloud Foundry User Account and Authentication (UAA) Server
The newest version!
/*
* ****************************************************************************
* Cloud Foundry
* Copyright (c) [2009-2017] Pivotal Software, Inc. All Rights Reserved.
*
* This product is licensed to you under the Apache License, Version 2.0 (the "License").
* You may not use this product except in compliance with the License.
*
* This product includes a number of subcomponents with
* separate copyright notices and license terms. Your use of these
* subcomponents is subject to the terms and conditions of the
* subcomponent's license, as noted in the LICENSE file.
* ****************************************************************************
*/
package org.cloudfoundry.identity.uaa.util;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.Date;
import java.util.Map;
public class JsonUtils {
private static ObjectMapper objectMapper = new ObjectMapper();
public static String writeValueAsString(Object object) throws JsonUtilException {
try {
return objectMapper.writeValueAsString(object);
} catch (IOException e) {
throw new JsonUtilException(e);
}
}
public static byte[] writeValueAsBytes(Object object) throws JsonUtilException {
try {
return objectMapper.writeValueAsBytes(object);
} catch (IOException e) {
throw new JsonUtilException(e);
}
}
public static T readValue(String s, Class clazz) throws JsonUtilException {
try {
if (hasText(s)) {
return objectMapper.readValue(s, clazz);
} else {
return null;
}
} catch (IOException e) {
throw new JsonUtilException(e);
}
}
public static T readValue(byte[] data, Class clazz) throws JsonUtilException {
try {
if (data!=null && data.length>0) {
return objectMapper.readValue(data, clazz);
} else {
return null;
}
} catch (IOException e) {
throw new JsonUtilException(e);
}
}
public static T readValue(String s, TypeReference typeReference) {
try {
if (hasText(s)) {
return objectMapper.readValue(s, typeReference);
} else {
return null;
}
} catch (IOException e) {
throw new JsonUtilException(e);
}
}
public static T readValue(byte[] data, TypeReference typeReference) {
try {
if (data!=null && data.length>0) {
return objectMapper.readValue(data, typeReference);
} else {
return null;
}
} catch (IOException e) {
throw new JsonUtilException(e);
}
}
public static T convertValue(Object object, Class toClazz) throws JsonUtilException {
try {
if (object == null) {
return null;
} else {
return objectMapper.convertValue(object, toClazz);
}
} catch (IllegalArgumentException e) {
throw new JsonUtilException(e);
}
}
public static JsonNode readTree(JsonParser p) {
try {
return objectMapper.readTree(p);
} catch (IOException e) {
throw new JsonUtilException(e);
}
}
public static JsonNode readTree(String s) {
try {
if (hasText(s)) {
return objectMapper.readTree(s);
} else {
return null;
}
} catch (IOException e) {
throw new JsonUtilException(e);
}
}
public static class JsonUtilException extends RuntimeException {
private static final long serialVersionUID = -4804245225960963421L;
public JsonUtilException(Throwable cause) {
super(cause);
}
}
public static String serializeExcludingProperties(Object object, String... propertiesToExclude) {
String serialized = JsonUtils.writeValueAsString(object);
Map properties = JsonUtils.readValue(serialized, new TypeReference