org.apache.shindig.common.JsonSerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of shindig-common Show documentation
Show all versions of shindig-common Show documentation
Common java code for Shindig
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.shindig.common;
import org.apache.shindig.common.util.DateUtil;
import org.apache.shindig.common.uri.Uri;
import org.joda.time.DateTime;
import org.json.JSONArray;
import org.json.JSONObject;
import com.google.common.collect.Multimap;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
/**
* Serializes a JSONObject.
*
* The methods here are designed to be substantially more CPU and memory efficient than those found
* in org.json or net.sf.json. In profiling, the performance of both of these libraries has been
* found to be woefully inadequate for large scale deployments.
*
* The append*() methods can be used to serialize directly into an Appendable, such as an output
* stream. This avoids unnecessary copies to intermediate objects.
*
* To reduce output size, null values in json arrays and objects will always be removed.
*/
public final class JsonSerializer {
// Multiplier to use for allocating the buffer.
private static final int BASE_MULTIPLIER = 256;
private static final char[] HEX_DIGITS = {
'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
};
private JsonSerializer() {}
public static String serialize(Object object) {
StringBuilder buf = new StringBuilder(1024);
try {
append(buf, object);
} catch (IOException e) {
throw new RuntimeException(e);
}
return buf.toString();
}
/**
* Serialize a JSONObject. Does not guard against cyclical references.
*/
public static String serialize(JSONObject object) {
StringBuilder buf = new StringBuilder(object.length() * BASE_MULTIPLIER);
try {
appendJsonObject(buf, object);
} catch (IOException e) {
// Shouldn't ever happen unless someone adds something to append*.
throw new RuntimeException(e);
}
return buf.toString();
}
/**
* Serializes a Map as a JSON object. Does not guard against cyclical references.
*/
public static String serialize(Map map) {
StringBuilder buf = new StringBuilder(map.size() * BASE_MULTIPLIER);
try {
appendMap(buf, map);
} catch (IOException e) {
// Shouldn't ever happen unless someone adds something to append*.
throw new RuntimeException(e);
}
return buf.toString();
}
/**
* Serializes a Collection as a JSON array. Does not guard against cyclical references.
*/
public static String serialize(Collection> collection) {
StringBuilder buf = new StringBuilder(collection.size() * BASE_MULTIPLIER);
try {
appendCollection(buf, collection);
} catch (IOException e) {
// Shouldn't ever happen unless someone adds something to append*.
throw new RuntimeException(e);
}
return buf.toString();
}
/**
* Serializes an array as a JSON array. Does not guard against cyclical references
*/
public static String serialize(Object[] array) {
StringBuilder buf = new StringBuilder(array.length * BASE_MULTIPLIER);
try {
appendArray(buf, array);
} catch (IOException e) {
// Shouldn't ever happen unless someone adds something to append*.
throw new RuntimeException(e);
}
return buf.toString();
}
/**
* Serializes a JSON array. Does not guard against cyclical references
*/
public static String serialize(JSONArray array) {
StringBuilder buf = new StringBuilder(array.length() * BASE_MULTIPLIER);
try {
appendJsonArray(buf, array);
} catch (IOException e) {
// Shouldn't ever happen unless someone adds something to append*.
throw new RuntimeException(e);
}
return buf.toString();
}
/**
* Appends a value to the buffer.
*
* @throws IOException If {@link Appendable#append(char)} throws an exception.
*/
@SuppressWarnings("unchecked")
public static void append(Appendable buf, Object value) throws IOException {
if (value == null || value == JSONObject.NULL) {
buf.append("null");
} else if (value instanceof Number ||
value instanceof Boolean) {
// Primitives
buf.append(value.toString());
} else if (value instanceof CharSequence ||
value instanceof DateTime ||
value instanceof Locale ||
value instanceof Uri ||
value.getClass().isEnum()) {
// String-like Primitives
appendString(buf, value.toString());
} else if (value instanceof Date) {
appendString(buf, DateUtil.formatIso8601Date((Date)value));
} else if (value instanceof JSONObject) {
appendJsonObject(buf, (JSONObject) value);
} else if (value instanceof JSONArray) {
appendJsonArray(buf, (JSONArray) value);
} else if (value instanceof Map) {
appendMap(buf, (Map) value);
} else if (value instanceof Multimap) {
appendMultimap(buf, (Multimap) value);
} else if (value instanceof Collection) {
appendCollection(buf, (Collection
© 2015 - 2024 Weber Informatics LLC | Privacy Policy