org.apache.chemistry.opencmis.commons.impl.json.JSONValue Maven / Gradle / Ivy
/*
* 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.chemistry.opencmis.commons.impl.json;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.Writer;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import org.apache.chemistry.opencmis.commons.impl.json.parser.JSONParseException;
import org.apache.chemistry.opencmis.commons.impl.json.parser.JSONParser;
/**
* (Taken from JSON.simple <http://code.google.com/p/json-simple/> and
* modified for OpenCMIS.)
*
* @author FangYidong<[email protected]>
*/
public final class JSONValue {
private JSONValue() {
}
/**
* Parse JSON text into java object from the input source. Please use
* parseWithException() if you don't want to ignore the exception.
*
* @see org.apache.chemistry.opencmis.commons.impl.json.parser.JSONParser#parse(Reader)
* @see #parseWithException(Reader)
*
* @param in
* @return Instance of the following: JSONObject, JSONArray,
* java.lang.String, java.lang.Number, java.lang.Boolean, null
*
*/
public static Object parse(Reader in) {
try {
JSONParser parser = new JSONParser();
return parser.parse(in);
} catch (Exception e) {
return null;
}
}
public static Object parse(String s) {
StringReader in = new StringReader(s);
return parse(in);
}
/**
* Parse JSON text into java object from the input source.
*
* @see org.apache.chemistry.opencmis.commons.impl.json.parser.JSONParser
*
* @param in
* @return Instance of the following: JSONObject, JSONArray,
* java.lang.String, java.lang.Number, java.lang.Boolean, null
*/
public static Object parseWithException(Reader in) throws IOException, JSONParseException {
JSONParser parser = new JSONParser();
return parser.parse(in);
}
public static Object parseWithException(String s) throws JSONParseException {
JSONParser parser = new JSONParser();
return parser.parse(s);
}
/**
* Encode an object into JSON text and write it to out.
*
* If this object is a Map or a List, and it's also a JSONStreamAware or a
* JSONAware, JSONStreamAware or JSONAware will be considered firstly.
*
* DO NOT call this method from writeJSONString(Writer) of a class that
* implements both JSONStreamAware and (Map or List) with "this" as the
* first parameter, use JSONObject.writeJSONString(Map, Writer) or
* JSONArray.writeJSONString(List, Writer) instead.
*
* @see org.apache.chemistry.opencmis.commons.impl.json.JSONObject#writeJSONString(Map,
* Writer)
* @see org.apache.chemistry.opencmis.commons.impl.json.JSONArray#writeJSONString(List,
* Writer)
*
* @param value
* @param out
*/
@SuppressWarnings("unchecked")
public static void writeJSONString(Object value, Writer out) throws IOException {
if (value == null) {
out.write("null");
return;
}
if (value instanceof String) {
out.write('\"');
out.write(escape((String) value));
out.write('\"');
return;
}
if (value instanceof Double) {
if (((Double) value).isInfinite() || ((Double) value).isNaN()) {
out.write("null");
} else {
out.write(value.toString());
}
return;
}
if (value instanceof Float) {
if (((Float) value).isInfinite() || ((Float) value).isNaN()) {
out.write("null");
} else {
out.write(value.toString());
}
return;
}
if (value instanceof BigDecimal) {
out.write(((BigDecimal) value).toPlainString());
return;
}
if (value instanceof Number) {
out.write(value.toString());
return;
}
if (value instanceof Boolean) {
out.write(value.toString());
return;
}
if (value instanceof JSONStreamAware) {
((JSONStreamAware) value).writeJSONString(out);
return;
}
if (value instanceof JSONAware) {
out.write(((JSONAware) value).toJSONString());
return;
}
if (value instanceof Map) {
JSONObject.writeJSONString((Map) value, out);
return;
}
if (value instanceof List) {
JSONArray.writeJSONString((List