2011-07-13 17:53:17 +00:00
|
|
|
/*******************************************************************************
|
|
|
|
|
* Copyright (c) 2011 Ziver Koc
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
* THE SOFTWARE.
|
|
|
|
|
******************************************************************************/
|
2010-05-04 12:59:57 +00:00
|
|
|
package zutil.parser.json;
|
|
|
|
|
|
2010-10-27 18:02:44 +00:00
|
|
|
import zutil.io.MultiPrintStream;
|
2011-06-24 23:20:59 +00:00
|
|
|
import zutil.parser.DataNode;
|
|
|
|
|
import zutil.parser.DataNode.DataType;
|
2010-05-04 12:59:57 +00:00
|
|
|
|
2010-10-23 19:46:52 +00:00
|
|
|
/**
|
|
|
|
|
* This is a JSON parser class
|
|
|
|
|
*
|
|
|
|
|
* @author Ziver
|
|
|
|
|
*/
|
2010-07-01 16:22:02 +00:00
|
|
|
public class JSONParser{
|
2010-05-04 12:59:57 +00:00
|
|
|
private String json;
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args){
|
|
|
|
|
JSONParser parser = new JSONParser("" +
|
|
|
|
|
"{"+
|
|
|
|
|
" \"firstName\": \"John\","+
|
|
|
|
|
" \"lastName\": \"Smith\","+
|
|
|
|
|
" \"age\": 25,"+
|
|
|
|
|
" \"address\": {"+
|
|
|
|
|
" \"streetAddress\": \"21 2nd Street\","+
|
|
|
|
|
" \"city\": \"New York\","+
|
|
|
|
|
" \"state\": \"NY\","+
|
|
|
|
|
" \"postalCode\": \"10021\""+
|
|
|
|
|
" },"+
|
|
|
|
|
" \"phoneNumber\": ["+
|
|
|
|
|
" { \"type\": \"home\", \"number\": \"212 555-1234\" },"+
|
|
|
|
|
" { \"type\": \"fax\", \"number\": \"646 555-4567\" }"+
|
|
|
|
|
" ]"+
|
|
|
|
|
"}");
|
2011-06-24 23:20:59 +00:00
|
|
|
DataNode node = parser.read();
|
2010-05-04 12:59:57 +00:00
|
|
|
MultiPrintStream.out.dump( node );
|
2010-10-23 19:46:52 +00:00
|
|
|
JSONWriter writer = new JSONWriter( System.out );
|
2010-05-04 12:59:57 +00:00
|
|
|
writer.write(node);
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-23 19:46:52 +00:00
|
|
|
/**
|
|
|
|
|
* Creates a new instance of the parser
|
|
|
|
|
*
|
|
|
|
|
* @param json is the JSON String to parse
|
|
|
|
|
*/
|
2010-05-04 12:59:57 +00:00
|
|
|
public JSONParser(String json){
|
|
|
|
|
this.json = json;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-23 19:46:52 +00:00
|
|
|
/**
|
|
|
|
|
* Starts parsing
|
|
|
|
|
*
|
|
|
|
|
* @return a JSONNode object that is the root of the JSON
|
|
|
|
|
*/
|
2011-06-24 23:20:59 +00:00
|
|
|
public DataNode read(){
|
2010-05-04 12:59:57 +00:00
|
|
|
return parse(new StringBuffer(json));
|
|
|
|
|
}
|
2010-10-23 19:46:52 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Starts parsing
|
|
|
|
|
*
|
|
|
|
|
* @return a JSONNode object that is the root of the JSON
|
|
|
|
|
*/
|
|
|
|
|
/*public static JSONNode read( String json ){
|
|
|
|
|
return parse(new StringBuffer(json));
|
|
|
|
|
}*/
|
2010-05-04 12:59:57 +00:00
|
|
|
|
2010-10-23 19:46:52 +00:00
|
|
|
/**
|
|
|
|
|
* This is the real recursive parsing method
|
|
|
|
|
*
|
|
|
|
|
* @param json
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
2011-06-24 23:20:59 +00:00
|
|
|
protected static DataNode parse(StringBuffer json){
|
|
|
|
|
DataNode root = null;
|
|
|
|
|
DataNode key = null;
|
|
|
|
|
DataNode node = null;
|
2010-05-04 12:59:57 +00:00
|
|
|
int next_index;
|
|
|
|
|
|
|
|
|
|
while(Character.isWhitespace( json.charAt(0) ) ||
|
|
|
|
|
json.charAt(0) == ',' || json.charAt(0) == ':')
|
|
|
|
|
json.deleteCharAt(0);
|
|
|
|
|
char c = json.charAt(0);
|
|
|
|
|
json.deleteCharAt(0);
|
|
|
|
|
|
|
|
|
|
switch( c ){
|
|
|
|
|
case ']':
|
|
|
|
|
case '}':
|
|
|
|
|
return null;
|
|
|
|
|
case '{':
|
2011-06-24 23:20:59 +00:00
|
|
|
root = new DataNode(DataType.Map);
|
2010-05-04 12:59:57 +00:00
|
|
|
while((key = parse( json )) != null && (node = parse( json )) != null){
|
2011-06-24 23:20:59 +00:00
|
|
|
root.set( key.toString(), node );
|
2010-05-04 12:59:57 +00:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case '[':
|
2011-06-24 23:20:59 +00:00
|
|
|
root = new DataNode(DataType.List);
|
2010-05-04 12:59:57 +00:00
|
|
|
while((node = parse( json )) != null){
|
|
|
|
|
root.add( node );
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
// Parse String
|
|
|
|
|
case '\"':
|
2011-06-24 23:20:59 +00:00
|
|
|
root = new DataNode(DataType.String);
|
2010-05-04 12:59:57 +00:00
|
|
|
next_index = json.indexOf("\"");
|
|
|
|
|
root.set( json.substring(0, next_index) );
|
|
|
|
|
json.delete(0, next_index+1);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2011-06-24 23:20:59 +00:00
|
|
|
root = new DataNode(DataType.Number);
|
2010-05-04 12:59:57 +00:00
|
|
|
for(next_index=0; next_index<json.length() ;++next_index)
|
|
|
|
|
if( json.charAt(next_index)==',' ||
|
|
|
|
|
json.charAt(next_index)==']' ||
|
|
|
|
|
json.charAt(next_index)=='}'){
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
root.set( c+json.substring(0, next_index) );
|
|
|
|
|
json.delete(0, next_index);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return root;
|
|
|
|
|
}
|
|
|
|
|
}
|