org.usergrid.utils.JsonUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of usergrid-core Show documentation
Show all versions of usergrid-core Show documentation
Core services for Usergrid system.
/*******************************************************************************
* Copyright 2012 Apigee Corporation
*
* 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.usergrid.utils;
import static org.apache.commons.lang.StringUtils.substringAfter;
import static org.usergrid.utils.StringUtils.stringOrSubstringBeforeFirst;
import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.net.URL;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.apache.commons.beanutils.PropertyUtils;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.io.JsonStringEncoder;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig.Feature;
import org.codehaus.jackson.schema.JsonSchema;
import org.codehaus.jackson.smile.SmileFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.usergrid.persistence.Entity;
/**
* @author edanuff
*
*/
public class JsonUtils {
private static final Logger logger = LoggerFactory
.getLogger(JsonUtils.class);
static ObjectMapper mapper = new ObjectMapper();
static SmileFactory smile = new SmileFactory();
static ObjectMapper smileMapper = new ObjectMapper(smile);
private static ObjectMapper indentObjectMapper = new ObjectMapper();
static {
indentObjectMapper.getSerializationConfig().set(Feature.INDENT_OUTPUT, true);
}
/**
* @param obj
* @return
*/
public static String mapToJsonString(Object obj) {
try {
return mapper.writeValueAsString(obj);
} catch (JsonGenerationException e) {
} catch (JsonMappingException e) {
} catch (IOException e) {
}
return "{}";
}
public static String mapToFormattedJsonString(Object obj) {
try {
return indentObjectMapper.writeValueAsString(obj);
} catch (JsonGenerationException e) {
} catch (JsonMappingException e) {
} catch (IOException e) {
}
return "{}";
}
public static String schemaToFormattedJsonString(JsonSchema schema) {
return mapToFormattedJsonString(schema.getSchemaNode());
}
public static JsonSchema getJsonSchema(Class cls) {
JsonSchema jsonSchema = null;
try {
jsonSchema = mapper.generateJsonSchema(cls);
} catch (JsonMappingException e) {
}
return jsonSchema;
}
public static Object parse(String json) {
try {
return mapper.readValue(json, Object.class);
} catch (JsonParseException e) {
} catch (JsonMappingException e) {
} catch (IOException e) {
}
return null;
}
public static JsonNode getJsonSchemaNode(Class cls) {
JsonNode schemaRootNode = null;
JsonSchema jsonSchema = getJsonSchema(cls);
if (jsonSchema != null) {
schemaRootNode = jsonSchema.getSchemaNode();
}
return schemaRootNode;
}
public static String quoteString(String s) {
JsonStringEncoder encoder = new JsonStringEncoder();
return new String(encoder.quoteAsUTF8(s));
}
public static ByteBuffer toByteBuffer(Object obj) {
if (obj == null) {
return null;
}
byte[] bytes = null;
try {
bytes = smileMapper.writeValueAsBytes(obj);
} catch (Exception e) {
logger.error("Error getting SMILE bytes", e);
}
if (bytes != null) {
return ByteBuffer.wrap(bytes);
}
return null;
}
public static Object fromByteBuffer(ByteBuffer byteBuffer) {
return fromByteBuffer(byteBuffer, Object.class);
}
public static Object fromByteBuffer(ByteBuffer byteBuffer, Class clazz) {
if ((byteBuffer == null) || !byteBuffer.hasRemaining()) {
return null;
}
if (clazz == null) {
clazz = Object.class;
}
Object obj = null;
try {
obj = smileMapper.readValue(byteBuffer.array(), byteBuffer.arrayOffset()
+ byteBuffer.position(), byteBuffer.remaining(), clazz);
} catch (Exception e) {
logger.error("Error parsing SMILE bytes", e);
}
return obj;
}
public static JsonNode nodeFromByteBuffer(ByteBuffer byteBuffer) {
if ((byteBuffer == null) || !byteBuffer.hasRemaining()) {
return null;
}
JsonNode obj = null;
try {
obj = smileMapper.readValue(byteBuffer.array(), byteBuffer.arrayOffset()
+ byteBuffer.position(), byteBuffer.remaining(),
JsonNode.class);
} catch (Exception e) {
logger.error("Error parsing SMILE bytes", e);
}
return obj;
}
public static JsonNode toJsonNode(Object obj) {
if (obj == null) {
return null;
}
JsonNode node = mapper.convertValue(obj, JsonNode.class);
return node;
}
public static Map toJsonMap(Object obj) {
if (obj == null) {
return null;
}
@SuppressWarnings("unchecked")
Map map = mapper.convertValue(obj, Map.class);
return map;
}
private static UUID tryConvertToUUID(Object o) {
if (o instanceof String) {
String s = (String) o;
if (s.length() == 36) {
try {
UUID uuid = UUID.fromString(s);
return uuid;
} catch (IllegalArgumentException e) {
}
}
}
return null;
}
public static Object normalizeJsonTree(Object obj) {
if (obj instanceof Map) {
@SuppressWarnings("unchecked")
Map