lv.ctco.cukesrest.internal.json.JsonParser Maven / Gradle / Ivy
The newest version!
package lv.ctco.cukesrest.internal.json;
import com.google.gson.stream.*;
import java.util.*;
import static com.google.gson.stream.JsonToken.*;
public class JsonParser {
public Map parsePathToValueMap(String json) {
Map result = new HashMap();
SafeJsonReader reader = new SafeJsonReader(json);
for (JsonToken token : reader) {
if (BEGIN_ARRAY == token) reader.beginArray();
else if (END_ARRAY == token) reader.endArray();
else if (BEGIN_OBJECT == token) reader.beginObject();
else if (END_OBJECT == token) reader.endObject();
else if (NAME == token) reader.nextName();
else if (STRING == token) add(reader.getCurrentPath(), reader.nextString(), result);
else if (NUMBER == token) add(reader.getCurrentPath(), reader.nextString(), result);
else if (BOOLEAN == token) add(reader.getCurrentPath(), Boolean.toString(reader.nextBoolean()), result);
else if (NULL == token) reader.nextNull();
}
reader.close();
return result;
}
static private void add(String path, String value, Map result) {
if (path.startsWith("$.")) {
path = path.substring(2);
} else {
path = path.substring(1);
}
result.put(path, value);
}
}