Skip to content

Instantly share code, notes, and snippets.

@slashnhax
Created July 14, 2015 05:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slashnhax/407716e17937a0e9e49c to your computer and use it in GitHub Desktop.
Save slashnhax/407716e17937a0e9e49c to your computer and use it in GitHub Desktop.
public class JSON {
public static <T extends Object> String mapToJson(Map<String, T> map){
String res = "{";
for(int i = 0; i < map.size(); i++){
Map.Entry<String, T> e = (Map.Entry<String, T>)map.entrySet().toArray()[i];
if(i > 0)
res += ",";
res += "\"" + e.getKey() + "\":" + objectToValue(e.getValue());
}
res += "}";
return res;
}
public static String objectToValue(Object o){
String res = "";
if(o instanceof Boolean || o instanceof Integer){
res += String.valueOf(o);
} else if (o instanceof Object[]){
Object[] array = (Object[])o;
res += "[";
for(int i = 0; i < array.length; i++){
if(i > 0)
res += ",";
res += objectToValue(array[i]);
}
res += "]";
} else if (o instanceof Map){
Map map = (Map)o;
res += "{";
for(int i = 0; i < map.size(); i++){
if(i > 0)
res += ",";
res += objectToValue(map.keySet().toArray()[i]) + ":" + objectToValue(map.values().toArray()[i]);
}
res += "}";
} else {
res += "\"" + String.valueOf(o) + "\"";
}
return res;
}
public static Map<String, Object> jsonToMap(String jsonString){
LinkedHashMap<String, Object> info = new LinkedHashMap<>();
if(jsonString != null) {
jsonString = jsonString.substring(1, jsonString.length()-1);
int block = 0;
boolean qToggle = false;
boolean bValue = false;
String key = "";
String value = "";
for (char c : jsonString.toCharArray()) {
switch (c) {
case '"':
if(qToggle)
block--;
else
block++;
qToggle^=true;
continue;
case '{':
block++;
break;
case '}':
block--;
break;
case '[':
block++;
break;
case ']':
block--;
break;
case ',':
if(block == 0){
info.put(key, toObject(value));
bValue = false;
key = "";
value = "";
continue;
}
case ':':
if(block == 0){
bValue = true;
continue;
}
}
if (bValue)
value += c;
else
key += c;
}
info.put(key, toObject(value));
}
return info;
}
public static Object toObject(String string){
if(string.matches("-?\\d+")){
return Integer.parseInt(string);
} else if(string.matches("-?\\d+\\.\\d+")){
return Double.parseDouble(string);
} else if (string.matches("(true|false)")){
return Boolean.valueOf(string);
} else if (string.matches("\\{.*\\}")){
LinkedHashMap<Object, Object> map = new LinkedHashMap<>();
string = string.substring(1, string.length() - 1);
int block = 0;
boolean bValue = false;
String key = "";
String value = "";
for (char c : string.toCharArray()) {
switch (c) {
case '"':
continue;
case '{':
block++;
continue;
case '}':
block--;
break;
case '[':
block++;
break;
case ']':
block--;
break;
case ',':
if(block == 0){
//TODO: Parse to Object?
map.put(key, toObject(value));
bValue = false;
key = "";
value = "";
continue;
}
case ':':
if(block == 0){
bValue = true;
continue;
}
default:
}
if (bValue)
value += c;
else
key += c;
}
map.put(key, toObject(value));
return map;
} else if (string.matches("\\[.*\\]")) {
ArrayList<Object> list = new ArrayList<>();
string = string.substring(1, string.length()-1);
int block = 0;
String value = "";
for (char c : string.toCharArray()) {
switch (c) {
case '"':
//block ^= true;
continue;
case '{':
block++;
continue;
case '}':
block--;
break;
case '[':
block++;
break;
case ']':
block--;
break;
case ',':
if (block == 0) {
list.add(toObject(value));
value = "";
continue;
}
case ':':
if (block == 0)
continue;
default:
value += c;
}
}
list.add(toObject(value));
return list.toArray();
}
return string;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment