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

net_io.utils.JSONUtils Maven / Gradle / Ivy

package net_io.utils;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import net_io.utils.Mixed.ENTITY_TYPE;


public class JSONUtils {
	public final static String ANONYMOUS_NODE = "__ANONYMOUS_NODE__"; 
	public final static String EMPTY_NAME_NODE = "__EMPTY_NAME_NODE__";
    protected static final char[][] CHARS_ESCAPE = {
        {'\b', 'b'},
        {'\n', 'n'},
        {'\t', 't'},
        {'\f', 'f'},
        {'\r', 'r'},
        {'\"', '\"'},
        {'\\', '\\'},
    };
    
    private final static int SPACE_CHAR = 32;
    private final static String INDENT = "  ";
    
    public static String escape(String str) {
    	int len = str.length();
    	StringBuffer sb = new StringBuffer();
    	for(int i=0; i= CHARS_ESCAPE.length) {
    			sb.append(ch);
    		}
    	}
    	return sb.toString();
    }

    public static String unescape(String str) {
    	int len = str.length();
    	StringBuffer sb = new StringBuffer();
    	for(int i=0; i= len) {
        				//TODO: Exception
        				break; //忽略最后一个 \\u
    				}
    				sb.append((char)Integer.parseInt(str.substring(i+1, i+5), 16));
    				i += 4;
    			} else { //转义字符
    	    		int j = 0;
    	    		for(; j= CHARS_ESCAPE.length) {
    	    			sb.append(ch); //转义字符表查不到,直接保存原文
    	    		}
    				
    			}
    		} else {
    			sb.append(ch);
    		}
    	}
    	return sb.toString();
    }


	private byte[] data = null;
	private int offset = 0;
	private JSONUtils(byte[] data) {
		this.data = data;
		this.offset = 0;
	}
	public static Document toDOM(Mixed result) throws ParserConfigurationException {
		DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		Document doc = builder.newDocument();
		
		Element rootElement = doc.createElement("ROOT");
		doc.appendChild(rootElement);
		processOneNode(doc, result, rootElement);
		return doc;
	}
	
	private static void processOneNode(Document doc, Mixed result, Element element) {
		Mixed.ENTITY_TYPE type = result.type();
		
		if(type == Mixed.ENTITY_TYPE.LIST) {
			for(int i=0; i SPACE_CHAR) {
				break;
			}
		}
	}

	private Mixed parseStart() throws JSONException {
		Mixed ret = null;
		filterEmptyChar(); //忽略全部空白字符
		if(offset >= data.length) {
			return null;
		}
		switch(data[offset]) {
		case '{':
			ret = parseObject();
			break;
		case '[':
			ret = parseArray();
			break;
		case '"':
			ret = parseString();
			break;
		default:
			ret = parsePrimitive();
		}
		return ret;
	}
	
	private Mixed parseObject() throws JSONException {
		@SuppressWarnings("rawtypes")
		Mixed map = new Mixed(new HashMap());
		String key = null;
		offset++; //跳过({)
		while(offset < data.length) {
			filterEmptyChar(); //过滤空白字符
			//对象结束符“}”检查
			if(offset >= data.length || data[offset] == '}') {
				offset++;
				return map;
			}
			//解析KEY
			key = parseKey().toString();
			filterEmptyChar(); //过滤空白字符
			//解析Value
			if(offset >= data.length || data[offset] != ':') {
				throw new JSONException("Miss the (:).");
			}
			offset++; //跳过(:)
			Object ret = parseStart();
			//保存结果
			map.set(key, ret);
			filterEmptyChar(); //忽略全部空白字符
			//下一元素处理
			if(data[offset] == ',') {
				offset++;
				continue;
			}
			break;
		}
		offset++; //跳过(})
		
		return map;
	}
	private Mixed parseArray() throws JSONException {
		@SuppressWarnings("rawtypes")
		Mixed list = new Mixed(new ArrayList());
		offset++; //跳过([)
		while(offset < data.length) {
			filterEmptyChar(); //忽略全部空白字符
			//数组结束符“]”检查
			if(offset >= data.length || data[offset] == ']') {
				offset++; //跳过(])
				return list;
			}
			Object ret = parseStart();
			//保存结果
			list.add(ret);
			filterEmptyChar(); //忽略全部空白字符
			//下一元素处理
			if(data[offset] == ',') {
				offset++;
				continue;
			}
			break;
		}
		offset++; //跳过(])
		return list;
	}
	private Mixed parseKey() throws JSONException {
		//存在单引号或双引号作为定界符,则按正常的字符串解析
		if(data[offset] == '"' || data[offset] == '\'') {
			return parseString();
		}
		int start = offset;
		try {
			while(offset < data.length) {
				if(data[offset] <= SPACE_CHAR || data[offset] == ':') {
					String str = new String(data, start, offset-start, "UTF-8");
					str = unescape(str);
					//要求:offset最后停留(:)
					while(offset < data.length && data[offset] != ':') {
						offset++;
					}
					return new Mixed(str);
				}
				if(data[offset] == '\\') {
					offset += 2;
				} else {
					offset++;
				}
			}
		} catch (UnsupportedEncodingException e) {
			throw new JSONException("UnsupportedEncoding - "+e.getMessage());
		}
		throw new JSONException("Format error. not found (\").");
	}

	private Mixed parseString() throws JSONException {
		if(data[offset] != '"' && data[offset] != '\'') {
			throw new JSONException("String data type must start with (\" or ').");
		}
		byte limitChar = data[offset];
		offset++; //跳过第1个(")
		int start = offset;
		try {
			while(offset < data.length) {
				if(data[offset] == limitChar) {
					String str = new String(data, start, offset-start, "UTF-8");
					str = unescape(str);
					offset++; //跳过第2个(")
					return new Mixed(str);
				}
				if(data[offset] == '\\') {
					offset += 2;
				} else {
					offset++;
				}
			}
		} catch (UnsupportedEncodingException e) {
			throw new JSONException("UnsupportedEncoding - "+e.getMessage());
		}
		throw new JSONException("Format error. not found ("+(limitChar=='"'?'"':'\'')+").");
	}
	private Mixed parsePrimitive() throws JSONException {
		filterEmptyChar(); //忽略全部空白字符
		int start = offset;
		for(; offset




© 2015 - 2024 Weber Informatics LLC | Privacy Policy