All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.json.simple.JSONValue Maven / Gradle / Ivy

The newest version!
/*
 * $Id: JSONValue.java,v 1.1 2006/04/15 14:37:04 platform Exp $
 * Created on 2006-4-15
 */
package org.json.simple;

import com.jn.easyjson.core.JSONBuilderProvider;
import com.jn.langx.util.io.IOs;
import org.json.simple.parser.ParseException;

import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.List;
import java.util.Map;

public class JSONValue {
    /**
     * Parse JSON text into java object from the input source.
     * Please use parseWithException() if you don't want to ignore the exception.
     *
     * @param in
     * @return Instance of the following:
     * org.json.simple.JSONObject,
     * org.json.simple.JSONArray,
     * java.lang.String,
     * java.lang.Number,
     * java.lang.Boolean,
     * null
     * @see org.json.simple.parser.JSONParser#parse(Reader)
     * @see #parseWithException(Reader)
     */
    public static Object parse(Reader in) {
        String string = null;
        try {
            string = IOs.readAsString(in);
        } catch (IOException ex) {

        }
        return parse(string);
    }

    public static Object parse(String s) {
        return JsonMapper.fromJsonTreeNode(JSONBuilderProvider.simplest().fromJson(s));
    }

    /**
     * Parse JSON text into java object from the input source.
     *
     * @param in
     * @return Instance of the following:
     * org.json.simple.JSONObject,
     * org.json.simple.JSONArray,
     * java.lang.String,
     * java.lang.Number,
     * java.lang.Boolean,
     * null
     * @throws IOException
     * @throws ParseException
     * @see org.json.simple.parser.JSONParser
     */
    public static Object parseWithException(Reader in) throws IOException, ParseException {
        return parseWithException(IOs.readAsString(in));
    }

    public static Object parseWithException(String s) throws ParseException {
        return JsonMapper.fromJsonTreeNode(JSONBuilderProvider.simplest().fromJson(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. * * @param value * @see org.json.simple.JSONObject#writeJSONString(Map, Writer) * @see org.json.simple.JSONArray#writeJSONString(List, Writer) */ public static void writeJSONString(Object value, Writer out) throws IOException { out.write(toJSONString(value)); } /** * Convert an object to JSON text. *

* If this object is a Map or a List, and it's also a JSONAware, JSONAware will be considered firstly. *

* DO NOT call this method from toJSONString() of a class that implements both JSONAware and Map or List with * "this" as the parameter, use JSONObject.toJSONString(Map) or JSONArray.toJSONString(List) instead. * * @param value * @return JSON text, or "null" if value is null or it's an NaN or an INF number. * @see org.json.simple.JSONObject#toJSONString(Map) * @see org.json.simple.JSONArray#toJSONString(List) */ public static String toJSONString(Object value) { return JSONBuilderProvider.simplest().toJson(JsonMapper.toJsonTreeNode(value)); } /** * Escape quotes, \, /, \r, \n, \b, \f, \t and other control characters (U+0000 through U+001F). * * @param s * @return */ public static String escape(String s) { if (s == null) { return null; } StringBuffer sb = new StringBuffer(); escape(s, sb); return sb.toString(); } /** * @param s - Must not be null. * @param sb */ static void escape(String s, StringBuffer sb) { for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); switch (ch) { case '"': sb.append("\\\""); break; case '\\': sb.append("\\\\"); break; case '\b': sb.append("\\b"); break; case '\f': sb.append("\\f"); break; case '\n': sb.append("\\n"); break; case '\r': sb.append("\\r"); break; case '\t': sb.append("\\t"); break; case '/': sb.append("\\/"); break; default: //Reference: http://www.unicode.org/versions/Unicode5.1.0/ if ((ch >= '\u0000' && ch <= '\u001F') || (ch >= '\u007F' && ch <= '\u009F') || (ch >= '\u2000' && ch <= '\u20FF')) { String ss = Integer.toHexString(ch); sb.append("\\u"); for (int k = 0; k < 4 - ss.length(); k++) { sb.append('0'); } sb.append(ss.toUpperCase()); } else { sb.append(ch); } } }//for } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy