javaでjson解析、出力をするツール

1.解析定義のソースコード:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ToObject {

private static final char cl_s = '[';
private static final char cl_e = ']’;
private static final char cd_s = '{';
private static final char cd_e = '}’;
private static final String Null = “Null";

private int e = 0;
private String jsonString = null;

public Object toObject(String jsonString) {
this.jsonString = jsonString;
return this.toObject(0);
}
// 路由
private Object toObject(int s) {
char c = this.jsonString.charAt(s);
if (c == cl_s) {
return this.toObjectFromCLS(s + 1);
} else if (c == cd_s) {
return this.toObjectFromCDS(s + 1);
} else {
if (s == 0) {
return ToObject.Null;
}
if (c == '"') {
return this.toObjectFromString(s);
} else {
return this.toObjectFromSimpleType(s);
}
}
}

// コレクションを分析
private Object toObjectFromCLS(int s) {
List<Object> ret = new ArrayList<Object>();
char c;
int l = jsonString.length();
Object obj;
for (int i = s; i < l; i++) {
c = jsonString.charAt(i);
if (c == ' ' || c == ',’) {
continue;
}
if (c == cl_e) {
e = i;
return ret;
}
obj = this.toObject(i);
if (ToObject.Null.equals(obj)) {
break;
}
ret.add(obj);
i = e;
}
return null;
}

// キーとペアの値を解析
private Object toObjectFromCDS(int s) {
Map<Object, Object> ret = new HashMap<Object, Object>();
char c;
int l = jsonString.length(), step = 0;
Object key = null, value = null, obj;
for (int i = s; i < l; i++) {
c = jsonString.charAt(i);
if (c == ' ') {
continue;
}
if (c == cd_e) {
e = i;
return ret;
}
if (c == ',’) {
step = 0;
key = null;
continue;
}
if (c == ':’) {
step = 1;
value = null;
continue;
}
obj = this.toObject(i);
if (ToObject.Null.equals(obj)) {
break;
}
if (step == 0) {
key = obj;
} else if (step == 1) {
value = obj;
ret.put(key, value);
}
i = e;
}
return null;
}

// 文字列の解析
private Object toObjectFromString(int s) {
char c = jsonString.charAt(s);
s++;
StringBuilder ret = new StringBuilder();
for (int i = s; i < jsonString.length(); i++) {
c = jsonString.charAt(i);
if (c == '"') {
e = i;
return ret.toString();
}
if (c == '\"') {
ret.append('"');
continue;
}
if (c == '\\’ && jsonString.charAt(i + 1) == '\"') {
i++;
ret.append('\"');
continue;
}
ret.append(c);
}
return null;
}

// 基本タイプ
private Object toObjectFromSimpleType(int s) {
final String t = “true";
final String f = “false";
final String n = “null";
final String u = “undefined";
if (this.isType(s, t)) {
e = s + t.length() – 1;
return true;
}
if (this.isType(s, f)) {
e = s + f.length() – 1;
return false;
}
if (this.isType(s, n)) {
e = s + n.length() – 1;
return null;
}
if (this.isType(s, u)) {
e = s + u.length() – 1;
return null;
}
// 数字
char c = jsonString.charAt(s);
if ((c >= '0’ && c <= '9’) || c == '.’ || c == '-') {
boolean hasPoint = false;
for (int i = s; i < jsonString.length(); i++) {
c = jsonString.charAt(i);
switch (c) {
case '.’:
if (hasPoint)
return null;
hasPoint = true;
break;
case '0’:
case '1’:
case '2’:
case '3’:
case '4’:
case '5’:
case '6’:
case '7’:
case '8’:
case '9’:
case '-':
break;
case 'F’:
case 'f’:
e = i;
return Float.parseFloat(jsonString.substring(s, i – s));
case 'L’:
case 'l’:
e = i;
if (hasPoint)
return null;
return Long.parseLong(jsonString.substring(s, i – s));
default:
e = i – 1;
if (hasPoint) {
return Double.parseDouble(jsonString
.substring(s, i – s));
} else {
return Integer.parseInt(jsonString.substring(s, i – s));
}
}
}
}
return null;
}

private boolean isType(int s, String type) {
for (int i = 0; i < type.length(); i++) {
if (jsonString.charAt(s + i) != type.charAt(i)) {
return false;
}
}
return true;
}

2.テストコード下記:

public static void main(String[] args) {
String json = “{\"key1\":\"value1\",\"key2\":[\"array1\",\"array2\"]}";
Object obj = new ToObject().toObject(json);
Map<Object, Object> map = (Map<Object, Object>)obj;
Set<Object> keys = map.keySet();
Object value = null;
for(Object key : keys) {
value = map.get(key);
System.out.print(“key=" + key);
if (value instanceof List) {
List<Object> array = (List<Object>)value;
System.out.print(“\tvalue=");
for(Object item : array) {
System.out.print(item);
}
System.out.println();
} else {
System.out.println(“\tvalue=" + value);
}
}
}

 

Development

Posted by arkgame