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

com.mycomm.itool.json.JsonValue Maven / Gradle / Ivy

package com.mycomm.itool.json;

/**
 * Created by jw362j on 7/30/2014.
 */
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;


public abstract class JsonValue {


    protected final static int TYPE_OBJECT = 1;


    protected final static int TYPE_ARRAY = 2;

    protected final static int TYPE_STRING = 3;


    protected final static int TYPE_BOOL = 4;

    protected final static int TYPE_BYTES = 5;


    protected final static int TYPE_NUM = 6;


    protected final static int TYPE_NULL = 7;

    public abstract String toJsonString();

    @Override
    public abstract String toString();

    /**
     * write the object data
     * @param dos
     */
    protected abstract void write(DataOutputStream dos) throws IOException;

    /**
     * reading the object data
     * @param dis
     */
    protected abstract void read(DataInputStream dis) throws IOException;

    /**
     * Parse a String to a JsonValue.
     * @param s a String
     * @return a JsonValue
     */
    protected static JsonValue parseValue(String s) {
        if (s == null || s.equals("null")) {
            return new JsonNull();
        }
        final int len = s.length();
        char curChar = 0;
        int sIdx = -1, eIdx;
        while (++sIdx < len && (curChar = s.charAt(sIdx)) < 33);
        sIdx++;
        if (curChar == '"') {
            eIdx = s.lastIndexOf('"');
            if (eIdx > -1) {
                String str = s.substring(sIdx, eIdx);
                return JsonString.parseString(str);
            } else {
                System.err.println("'\"' is expected to close a string!");
            }
        } else if ((curChar > 47 && curChar < 58) || curChar == '-') {
            return JsonNum.parseNum(s);
        } else if (curChar == '{') {
            eIdx = s.lastIndexOf('}');
            if (eIdx > -1) {
                String str = s.substring(sIdx, eIdx);
                return JsonObject.parseObject(str);
            } else {
                System.err.println("'}' is expected to close a JsonObject!");
            }
        } else if (curChar == '[') {
            eIdx = s.lastIndexOf(']');
            if (eIdx > -1) {
                String str = s.substring(sIdx, eIdx);
                return JsonArray.parseArray(str);
            } else {
                System.err.println("']' is expected to close a JsonArray!");
            }
        } else if (curChar == 't' || curChar == 'T' || curChar == 'f' || curChar == 'F') {
            return JsonBool.parseBool(s);
        }
        return null;
    }
    /**
     * reading the serialized object data from inoutStream
     * @param dis inoutStream
     * @return serialized object
     * @throws IOException IO exception
     */
    protected static JsonValue readObject(DataInputStream dis) throws IOException {
        JsonValue val = null;
        int type = dis.read();
        switch (type) {
            case TYPE_OBJECT:
                val = new JsonObject();
                break;
            case TYPE_ARRAY:
                val = new JsonArray();
                break;
            case TYPE_STRING:
                val = new JsonString();
                break;
            case TYPE_BOOL:
                val = new JsonBool();
                break;
            case TYPE_BYTES:
                val = new JsonBytes();
                break;
            case TYPE_NUM:
                val = new JsonNum();
                break;
            case TYPE_NULL:
                val = new JsonNull();
                break;
            default:
                return null;
        }
        val.read(dis);
        return val;
    }

    /**
     * write the object which need to be serialized into stream
     * @param val the inout object
     * @param dos output stream
     * @throws IOException IO exception
     */
    protected static void writeObject(JsonValue val, DataOutputStream dos) throws IOException {
        val.write(dos);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy