org.cloudfoundry.client.lib.util.JsonUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cloudfoundry-client-lib Show documentation
Show all versions of cloudfoundry-client-lib Show documentation
A Cloud Foundry client library for Java
The newest version!
/*
* Copyright 2009-2012 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.cloudfoundry.client.lib.util;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.MediaType;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
/**
* Some JSON helper utilities used by the Cloud Foundry Java client.
*
* @author Thomas Risberg
*/
public class JsonUtil {
public static final MediaType JSON_MEDIA_TYPE = new MediaType(MediaType.APPLICATION_JSON.getType(),
MediaType.APPLICATION_JSON.getSubtype(),
StandardCharsets.UTF_8);
protected static final Log logger = LogFactory.getLog(JsonUtil.class);
private static final ObjectMapper mapper = new ObjectMapper();
public static List convertJsonToList(String json) {
List retList = new ArrayList<>();
if (json != null) {
try {
retList = mapper.readValue(json, new TypeReference>() {
});
} catch (IOException e) {
logger.warn("Error while reading Java List from JSON response: " + json, e);
}
}
return retList;
}
public static Map convertJsonToMap(String json) {
Map retMap = new HashMap<>();
if (json != null) {
try {
retMap = mapper.readValue(json, new TypeReference
© 2015 - 2024 Weber Informatics LLC | Privacy Policy