Search in sources :

Example 6 with JsonToken

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;
}
Also used : JsonToken(com.google.gson.stream.JsonToken) Users(com.github.fabienrenaud.jjb.model.Users)

Example 7 with JsonToken

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;
                }
        }
    }
}
Also used : User(com.github.fabienrenaud.jjb.model.Users.User) Friend(com.github.fabienrenaud.jjb.model.Users.Friend) JsonToken(com.google.gson.stream.JsonToken)

Example 8 with JsonToken

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);
}
Also used : JsonToken(com.google.gson.stream.JsonToken) IOException(java.io.IOException)

Example 9 with JsonToken

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;
}
Also used : OutPort(org.apache.airavata.workflow.core.dag.port.OutPort) PortModel(org.apache.airavata.model.PortModel) ArrayList(java.util.ArrayList) JsonToken(com.google.gson.stream.JsonToken) OutPortImpl(org.apache.airavata.workflow.core.dag.port.OutPortImpl)

Example 10 with JsonToken

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;
}
Also used : InPort(org.apache.airavata.workflow.core.dag.port.InPort) PortModel(org.apache.airavata.model.PortModel) ArrayList(java.util.ArrayList) JsonToken(com.google.gson.stream.JsonToken) InputPortIml(org.apache.airavata.workflow.core.dag.port.InputPortIml)

Aggregations

JsonToken (com.google.gson.stream.JsonToken)26 JsonReader (com.google.gson.stream.JsonReader)9 IOException (java.io.IOException)6 List (java.util.List)4 JsonPrimitive (com.google.gson.JsonPrimitive)3 InputStreamReader (java.io.InputStreamReader)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 BufferedReader (java.io.BufferedReader)2 PortModel (org.apache.airavata.model.PortModel)2 DeserializeException (org.bimserver.plugins.deserializers.DeserializeException)2 ListWaitingObject (org.bimserver.shared.ListWaitingObject)2 WaitingList (org.bimserver.shared.WaitingList)2 AbstractEList (org.eclipse.emf.common.util.AbstractEList)2 EList (org.eclipse.emf.common.util.EList)2 EObject (org.eclipse.emf.ecore.EObject)2 EReference (org.eclipse.emf.ecore.EReference)2 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)2 ParsingException (org.eclipse.smarthome.automation.parser.ParsingException)2 ParsingNestedException (org.eclipse.smarthome.automation.parser.ParsingNestedException)2