use of com.google.gson.stream.JsonToken in project java-json-benchmark by fabienrenaud.
the class UsersStreamDeserializer method gson.
/*
* GSON
*/
@Override
public Users gson(JsonReader reader) throws IOException {
Users uc = new Users();
reader.beginObject();
JsonToken token;
while ((token = reader.peek()) != JsonToken.END_OBJECT) {
if (token == JsonToken.NAME) {
String fieldname = reader.nextName();
if ("users".equals(fieldname)) {
uc.users = new ArrayList<>();
reader.beginArray();
while (reader.peek() != JsonToken.END_ARRAY) {
uc.users.add(gsonUser(reader));
}
reader.endArray();
}
}
}
reader.endObject();
return uc;
}
use of com.google.gson.stream.JsonToken in project java-json-benchmark by fabienrenaud.
the class UsersStreamDeserializer method gsonUser.
private User gsonUser(JsonReader reader) throws IOException {
User r = new User();
while (true) {
JsonToken token = reader.peek();
switch(token) {
case BEGIN_OBJECT:
reader.beginObject();
break;
case END_OBJECT:
reader.endObject();
return r;
case NAME:
String fieldname = reader.nextName();
switch(fieldname) {
case "_id":
r._id = reader.nextString();
break;
case "index":
r.index = reader.nextInt();
break;
case "guid":
r.guid = reader.nextString();
break;
case "isActive":
r.isActive = reader.nextBoolean();
break;
case "balance":
r.balance = reader.nextString();
break;
case "picture":
r.picture = reader.nextString();
break;
case "age":
r.age = reader.nextInt();
break;
case "eyeColor":
r.eyeColor = reader.nextString();
break;
case "name":
r.name = reader.nextString();
break;
case "gender":
r.gender = reader.nextString();
break;
case "company":
r.company = reader.nextString();
break;
case "email":
r.email = reader.nextString();
break;
case "phone":
r.phone = reader.nextString();
break;
case "address":
r.address = reader.nextString();
break;
case "about":
r.about = reader.nextString();
break;
case "registered":
r.registered = reader.nextString();
break;
case "latitude":
r.latitude = reader.nextDouble();
break;
case "longitude":
r.longitude = reader.nextDouble();
break;
case "greeting":
r.greeting = reader.nextString();
break;
case "favoriteFruit":
r.favoriteFruit = reader.nextString();
break;
case "tags":
r.tags = new ArrayList<>();
boolean carryOn = true;
while (carryOn) {
token = reader.peek();
switch(token) {
case BEGIN_ARRAY:
reader.beginArray();
break;
case END_ARRAY:
reader.endArray();
carryOn = false;
break;
case STRING:
r.tags.add(reader.nextString());
break;
}
}
break;
case "friends":
r.friends = new ArrayList<>();
Friend f = null;
carryOn = true;
while (carryOn) {
token = reader.peek();
switch(token) {
case BEGIN_ARRAY:
reader.beginArray();
break;
case END_ARRAY:
reader.endArray();
carryOn = false;
break;
case BEGIN_OBJECT:
reader.beginObject();
f = new Friend();
break;
case END_OBJECT:
reader.endObject();
r.friends.add(f);
break;
case NAME:
String fn = reader.nextName();
switch(fn) {
case "id":
f.id = reader.nextString();
break;
case "name":
f.name = reader.nextString();
break;
}
break;
}
}
break;
}
}
}
}
use of com.google.gson.stream.JsonToken in project java-sdk by watson-developer-cloud.
the class AggregationDeserializer method parseNext.
/**
* Checks the next {@link JsonToken} to decide the next appropriate parsing method.
*
* @param in {@link JsonReader} object used for parsing
* @param objMap Map used to build the structure for the resulting {@link QueryAggregation} object
* @throws IOException signals that there has been an IO exception
*/
private void parseNext(JsonReader in, HashMap<String, Object> objMap) throws IOException {
JsonToken token = in.peek();
String lastName = "";
if (token == JsonToken.NAME) {
lastName = in.nextName();
token = in.peek();
}
switch(token) {
case BEGIN_ARRAY:
parseArray(in, objMap, lastName);
break;
case BEGIN_OBJECT:
parseObject(in, objMap, lastName);
break;
case STRING:
objMap.put(lastName, in.nextString());
break;
case NUMBER:
objMap.put(lastName, in.nextDouble());
break;
case BOOLEAN:
objMap.put(lastName, in.nextBoolean());
break;
default:
throw new IOException("Unexpected JSON token encountered");
}
collapseMap(objMap);
}
use of com.google.gson.stream.JsonToken in project airavata by apache.
the class JsonWorkflowParser method readApplicationOutputs.
private List<OutPort> readApplicationOutputs(JsonReader jsonReader) throws IOException, ParserException {
List<OutPort> outPorts = new ArrayList<>();
PortModel portModel;
OutPort outPort;
String name;
JsonToken peek = jsonReader.peek();
if (peek == JsonToken.NULL) {
jsonReader.nextNull();
} else if (peek == JsonToken.BEGIN_ARRAY) {
jsonReader.beginArray();
while (jsonReader.hasNext()) {
portModel = new PortModel();
outPort = new OutPortImpl(portModel);
jsonReader.beginObject();
while (jsonReader.hasNext()) {
name = jsonReader.nextName();
if (name.equals(NAME)) {
portModel.setName(jsonReader.nextString());
} else if (name.equals(ID)) {
portModel.setPortId(jsonReader.nextString());
} else if (name.equals(DATATYPE)) {
jsonReader.skipValue();
} else if (name.equals(DEFAULT_VALUE)) {
// can output has default values?
jsonReader.skipValue();
} else if (name.equals(DESCRIPTION)) {
portModel.setDescription(jsonReader.nextString());
} else {
jsonReader.skipValue();
}
}
jsonReader.endObject();
outPorts.add(outPort);
}
jsonReader.endArray();
} else {
throw new ParserException("Error! reading application outputs, expected " + getTokenString(JsonToken.NULL) + " or " + getTokenString(JsonToken.BEGIN_ARRAY) + " but found " + getTokenString(peek));
}
return outPorts;
}
use of com.google.gson.stream.JsonToken in project airavata by apache.
the class JsonWorkflowParser method readApplicationInputs.
private List<InPort> readApplicationInputs(JsonReader jsonReader) throws IOException, ParserException {
List<InPort> inPorts = new ArrayList<>();
JsonToken peek = jsonReader.peek();
PortModel portModel;
InPort inPort;
String name;
if (peek == JsonToken.NULL) {
jsonReader.nextNull();
} else if (peek == JsonToken.BEGIN_ARRAY) {
jsonReader.beginArray();
while (jsonReader.hasNext()) {
portModel = new PortModel();
inPort = new InputPortIml(portModel);
jsonReader.beginObject();
while (jsonReader.hasNext()) {
name = jsonReader.nextName();
if (name.equals(NAME)) {
portModel.setName(jsonReader.nextString());
} else if (name.equals(ID)) {
portModel.setPortId(jsonReader.nextString());
} else if (name.equals(DATATYPE)) {
jsonReader.skipValue();
} else if (name.equals(DEFAULT_VALUE)) {
inPort.setDefaultValue(jsonReader.nextString());
} else if (name.equals(DESCRIPTION)) {
portModel.setDescription(jsonReader.nextString());
} else {
jsonReader.skipValue();
}
}
jsonReader.endObject();
inPorts.add(inPort);
}
jsonReader.endArray();
} else {
throw new ParserException("Error! reading application inputs, expected " + getTokenString(JsonToken.NULL) + " or " + getTokenString(JsonToken.BEGIN_ARRAY) + " but found " + getTokenString(peek));
}
return inPorts;
}
Aggregations