2011-07-13 17:53:17 +00:00
|
|
|
/*******************************************************************************
|
2013-05-28 19:29:24 +00:00
|
|
|
* Copyright (c) 2013 Ziver
|
|
|
|
|
*
|
2011-07-13 17:53:17 +00:00
|
|
|
* 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:
|
2013-05-28 19:29:24 +00:00
|
|
|
*
|
2011-07-13 17:53:17 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
|
* all copies or substantial portions of the Software.
|
2013-05-28 19:29:24 +00:00
|
|
|
*
|
2011-07-13 17:53:17 +00:00
|
|
|
* 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.
|
|
|
|
|
******************************************************************************/
|
2013-05-28 19:29:24 +00:00
|
|
|
|
2010-05-04 12:59:57 +00:00
|
|
|
package zutil.parser.json;
|
|
|
|
|
|
2011-06-24 23:20:59 +00:00
|
|
|
import zutil.parser.DataNode;
|
|
|
|
|
import zutil.parser.DataNode.DataType;
|
2011-09-20 17:18:25 +00:00
|
|
|
import zutil.struct.MutableInt;
|
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
|
|
|
|
2010-10-23 19:46:52 +00:00
|
|
|
/**
|
|
|
|
|
* Starts parsing
|
|
|
|
|
*
|
2011-09-19 19:04:21 +00:00
|
|
|
* @param json is the JSON String to parse
|
2010-10-23 19:46:52 +00:00
|
|
|
* @return a JSONNode object that is the root of the JSON
|
|
|
|
|
*/
|
2011-09-19 19:04:21 +00:00
|
|
|
public DataNode read(String json){
|
2011-09-20 17:18:25 +00:00
|
|
|
return parse(new MutableInt(), new StringBuilder(json));
|
2010-05-04 12:59:57 +00:00
|
|
|
}
|
|
|
|
|
|
2011-09-19 19:04:21 +00:00
|
|
|
|
2010-10-23 19:46:52 +00:00
|
|
|
/**
|
|
|
|
|
* This is the real recursive parsing method
|
|
|
|
|
*
|
|
|
|
|
* @param json
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
2011-09-20 17:18:25 +00:00
|
|
|
protected static DataNode parse(MutableInt index, StringBuilder json){
|
2011-06-24 23:20:59 +00:00
|
|
|
DataNode root = null;
|
|
|
|
|
DataNode key = null;
|
|
|
|
|
DataNode node = null;
|
2010-05-04 12:59:57 +00:00
|
|
|
int next_index;
|
|
|
|
|
|
2011-09-19 19:04:21 +00:00
|
|
|
while(Character.isWhitespace( json.charAt(index.i) ) ||
|
|
|
|
|
json.charAt(index.i) == ',' || json.charAt(index.i) == ':')
|
|
|
|
|
index.i++;
|
|
|
|
|
char c = json.charAt(index.i++);
|
2010-05-04 12:59:57 +00:00
|
|
|
|
|
|
|
|
switch( c ){
|
|
|
|
|
case ']':
|
|
|
|
|
case '}':
|
|
|
|
|
return null;
|
|
|
|
|
case '{':
|
2011-06-24 23:20:59 +00:00
|
|
|
root = new DataNode(DataType.Map);
|
2011-09-19 19:04:21 +00:00
|
|
|
while((key = parse( index, json )) != null && (node = parse( index, 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);
|
2011-09-19 19:04:21 +00:00
|
|
|
while((node = parse( index, json )) != null){
|
2010-05-04 12:59:57 +00:00
|
|
|
root.add( node );
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
// Parse String
|
|
|
|
|
case '\"':
|
2011-06-24 23:20:59 +00:00
|
|
|
root = new DataNode(DataType.String);
|
2011-09-19 19:04:21 +00:00
|
|
|
next_index = json.indexOf( "\"", index.i);
|
|
|
|
|
root.set( json.substring(index.i, next_index) );
|
|
|
|
|
index.i = next_index+1;
|
2010-05-04 12:59:57 +00:00
|
|
|
break;
|
|
|
|
|
default:
|
2011-06-24 23:20:59 +00:00
|
|
|
root = new DataNode(DataType.Number);
|
2011-09-19 19:04:21 +00:00
|
|
|
for(next_index=index.i; next_index<json.length() ;++next_index)
|
2010-05-04 12:59:57 +00:00
|
|
|
if( json.charAt(next_index)==',' ||
|
|
|
|
|
json.charAt(next_index)==']' ||
|
|
|
|
|
json.charAt(next_index)=='}'){
|
|
|
|
|
break;
|
|
|
|
|
}
|
2011-09-19 19:04:21 +00:00
|
|
|
root.set( c+json.substring(index.i, next_index) );
|
|
|
|
|
index.i = next_index;
|
2010-05-04 12:59:57 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return root;
|
|
|
|
|
}
|
|
|
|
|
}
|