Example usage for com.google.gson.stream JsonReader nextInt

List of usage examples for com.google.gson.stream JsonReader nextInt

Introduction

In this page you can find the example usage for com.google.gson.stream JsonReader nextInt.

Prototype

public int nextInt() throws IOException 

Source Link

Document

Returns the com.google.gson.stream.JsonToken#NUMBER int value of the next token, consuming it.

Usage

From source file:org.bimserver.deserializers.JsonDeserializer.java

License:Open Source License

private Object readPrimitive(JsonReader jsonReader, EStructuralFeature eStructuralFeature) throws IOException {
    EClassifier eClassifier = eStructuralFeature.getEType();
    if (eClassifier == EcorePackage.eINSTANCE.getEString()) {
        return jsonReader.nextString();
    } else if (eClassifier == EcorePackage.eINSTANCE.getEDouble()) {
        return jsonReader.nextDouble();
    } else if (eClassifier == EcorePackage.eINSTANCE.getEBoolean()) {
        return jsonReader.nextBoolean();
    } else if (eClassifier == EcorePackage.eINSTANCE.getEInt()) {
        return jsonReader.nextInt();
    } else if (eClassifier == EcorePackage.eINSTANCE.getEEnum()) {
        EEnum eEnum = (EEnum) eStructuralFeature.getEType();
        return eEnum.getEEnumLiteral(jsonReader.nextString()).getInstance();
    } else if (eClassifier instanceof EClass) {
        if (eStructuralFeature.getEType().getName().equals("IfcGloballyUniqueId")) {
            IfcGloballyUniqueId ifcGloballyUniqueId = Ifc2x3tc1Factory.eINSTANCE.createIfcGloballyUniqueId();
            ifcGloballyUniqueId.setWrappedValue(jsonReader.nextString());
            return ifcGloballyUniqueId;
        } else {//from  w w  w . j  av  a  2  s.c o m
            throw new RuntimeException();
        }
    } else if (eClassifier instanceof EEnum) {
        EEnum eEnum = (EEnum) eStructuralFeature.getEType();
        return eEnum.getEEnumLiteral(jsonReader.nextString()).getInstance();
    } else {
        throw new RuntimeException("Unimplemented type " + eStructuralFeature.getEType().getName());
    }
}

From source file:org.bimserver.emf.SharedJsonDeserializer.java

License:Open Source License

@SuppressWarnings({ "rawtypes", "unchecked" })
private IdEObject processObject(IfcModelInterface model, WaitingList<Long> waitingList, JsonReader jsonReader,
        EClass eClass) throws IOException, DeserializeException, IfcModelInterfaceException {
    IdEObjectImpl object = null;/*from w w  w .  j a va2 s .c  o m*/
    jsonReader.beginObject();
    if (jsonReader.nextName().equals("_i")) {
        long oid = jsonReader.nextLong();
        if (jsonReader.nextName().equals("_t")) {
            String type = jsonReader.nextString();
            if (eClass == null) {
                eClass = model.getPackageMetaData().getEClassIncludingDependencies(type);
            }
            if (eClass == null) {
                throw new DeserializeException("No class found with name " + type);
            }
            if (model.containsNoFetch(oid)) {
                object = (IdEObjectImpl) model.getNoFetch(oid);
            } else {
                if (eClass == StorePackage.eINSTANCE.getIfcHeader()) {
                    object = (IdEObjectImpl) StoreFactory.eINSTANCE.createIfcHeader();
                } else {
                    object = (IdEObjectImpl) model.create(eClass, oid);
                }
            }

            if (jsonReader.nextName().equals("_s")) {
                int state = jsonReader.nextInt();
                if (state == 1) {
                    object.setLoadingState(State.LOADED);
                    while (jsonReader.hasNext()) {
                        String featureName = jsonReader.nextName();
                        boolean embedded = false;
                        if (featureName.startsWith("_r")) {
                            featureName = featureName.substring(2);
                        } else if (featureName.startsWith("_e")) {
                            embedded = true;
                            featureName = featureName.substring(2);
                        }
                        EStructuralFeature eStructuralFeature = eClass.getEStructuralFeature(featureName);
                        if (eStructuralFeature == null) {
                            throw new DeserializeException(
                                    "Unknown field (" + featureName + ") on class " + eClass.getName());
                        }
                        if (eStructuralFeature.isMany()) {
                            jsonReader.beginArray();
                            if (eStructuralFeature instanceof EAttribute) {
                                List list = (List) object.eGet(eStructuralFeature);
                                List<String> stringList = null;

                                if (eStructuralFeature.getEType() == EcorePackage.eINSTANCE.getEDoubleObject()
                                        || eStructuralFeature.getEType() == EcorePackage.eINSTANCE
                                                .getEDouble()) {
                                    EStructuralFeature asStringFeature = eClass
                                            .getEStructuralFeature(eStructuralFeature.getName() + "AsString");
                                    stringList = (List<String>) object.eGet(asStringFeature);
                                }

                                while (jsonReader.hasNext()) {
                                    Object e = readPrimitive(jsonReader, eStructuralFeature);
                                    list.add(e);
                                    if (eStructuralFeature.getEType() == EcorePackage.eINSTANCE.getEDouble()) {
                                        double val = (Double) e;
                                        stringList.add("" + val); // TODO
                                        // this
                                        // is
                                        // losing
                                        // precision,
                                        // maybe
                                        // also
                                        // send
                                        // the
                                        // string
                                        // value?
                                    }
                                }
                            } else if (eStructuralFeature instanceof EReference) {
                                int index = 0;
                                AbstractEList list = (AbstractEList) object.eGet(eStructuralFeature);
                                while (jsonReader.hasNext()) {
                                    if (embedded) {
                                        JsonToken peek = jsonReader.peek();
                                        if (peek == JsonToken.NUMBER) {
                                            long refOid = jsonReader.nextLong();
                                            processRef(model, waitingList, object, eStructuralFeature, index,
                                                    list, refOid);
                                        } else {
                                            jsonReader.beginObject();
                                            String nextName = jsonReader.nextName();
                                            if (nextName.equals("_t")) {
                                                String t = jsonReader.nextString();
                                                IdEObject wrappedObject = (IdEObject) model
                                                        .create(model.getPackageMetaData().getEClass(t), -1);
                                                if (jsonReader.nextName().equals("_v")) {
                                                    EStructuralFeature wv = wrappedObject.eClass()
                                                            .getEStructuralFeature("wrappedValue");
                                                    wrappedObject.eSet(wv, readPrimitive(jsonReader, wv));
                                                    list.add(wrappedObject);
                                                } else {
                                                    // error
                                                }
                                            } else if (nextName.equals("_i")) {
                                                // Not all are embedded...
                                                long refOid = jsonReader.nextLong();
                                                processRef(model, waitingList, object, eStructuralFeature,
                                                        index, list, refOid);
                                            }
                                            jsonReader.endObject();
                                        }
                                    } else {
                                        long refOid = jsonReader.nextLong();
                                        processRef(model, waitingList, object, eStructuralFeature, index, list,
                                                refOid);
                                    }
                                    index++;
                                }
                            }
                            jsonReader.endArray();
                        } else {
                            if (eStructuralFeature instanceof EAttribute) {
                                Object x = readPrimitive(jsonReader, eStructuralFeature);
                                if (eStructuralFeature.getEType() == EcorePackage.eINSTANCE.getEDouble()) {
                                    EStructuralFeature asStringFeature = object.eClass()
                                            .getEStructuralFeature(eStructuralFeature.getName() + "AsString");
                                    if (asStringFeature != null) {
                                        object.eSet(asStringFeature, "" + x); // TODO
                                    }
                                    // this
                                    // is
                                    // losing
                                    // precision,
                                    // maybe
                                    // also
                                    // send
                                    // the
                                    // string
                                    // value?
                                }
                                object.eSet(eStructuralFeature, x);
                            } else if (eStructuralFeature instanceof EReference) {
                                if (eStructuralFeature.getName().equals("GlobalId")) {
                                    IfcGloballyUniqueId globallyUniqueId = Ifc2x3tc1Factory.eINSTANCE
                                            .createIfcGloballyUniqueId();
                                    globallyUniqueId.setWrappedValue(jsonReader.nextString());
                                    object.eSet(eStructuralFeature, globallyUniqueId);
                                } else if (embedded) {
                                    jsonReader.beginObject();
                                    if (jsonReader.nextName().equals("_t")) {
                                        String t = jsonReader.nextString();
                                        IdEObject wrappedObject = (IdEObject) model.create(
                                                model.getPackageMetaData().getEClassIncludingDependencies(t),
                                                -1);
                                        if (eStructuralFeature.getEAnnotation("dbembed") != null) {
                                            for (EStructuralFeature eStructuralFeature2 : wrappedObject.eClass()
                                                    .getEAllStructuralFeatures()) {
                                                String fn = jsonReader.nextName();
                                                if (fn.equals(eStructuralFeature2.getName())) {
                                                    wrappedObject.eSet(eStructuralFeature2,
                                                            readPrimitive(jsonReader, eStructuralFeature2));
                                                } else {
                                                    throw new DeserializeException(
                                                            fn + " / " + eStructuralFeature2.getName());
                                                }
                                            }
                                            object.eSet(eStructuralFeature, wrappedObject);
                                        } else {
                                            if (jsonReader.nextName().equals("_v")) {
                                                EStructuralFeature wv = wrappedObject.eClass()
                                                        .getEStructuralFeature("wrappedValue");
                                                wrappedObject.eSet(wv, readPrimitive(jsonReader, wv));
                                                object.eSet(eStructuralFeature, wrappedObject);
                                            }
                                        }
                                    }
                                    jsonReader.endObject();
                                } else {
                                    long refOid = jsonReader.nextLong();
                                    boolean isInverse = false;
                                    EntityDefinition entityBN = model.getPackageMetaData().getSchemaDefinition()
                                            .getEntityBN(object.eClass().getName());
                                    if (entityBN != null) {
                                        // Some entities like GeometryInfo/Data are not in IFC schema, but valid
                                        Attribute attributeBN = entityBN
                                                .getAttributeBNWithSuper(eStructuralFeature.getName());
                                        if (attributeBN != null) {
                                            if (attributeBN instanceof InverseAttribute) {
                                                isInverse = true;
                                            }
                                        }
                                    }
                                    if (!isInverse) {
                                        if (model.contains(refOid)) {
                                            object.eSet(eStructuralFeature, model.get(refOid));
                                        } else {
                                            waitingList.add(refOid, new SingleWaitingObject(-1, object,
                                                    (EReference) eStructuralFeature));
                                        }
                                    }
                                }
                            }
                        }
                    }
                } else {
                    // state not_loaded
                }
                if (waitingList.containsKey(oid)) {
                    waitingList.updateNode(oid, eClass, object);
                }
                model.add(object.getOid(), object);
            } else {
                LOGGER.info("_s expected");
            }
        } else {
            LOGGER.info("_t expected");
        }
    } else {
        LOGGER.info("_i expected");
    }
    jsonReader.endObject();
    return object;
}

From source file:org.bimserver.emf.SharedJsonDeserializer.java

License:Open Source License

private Object readPrimitive(JsonReader jsonReader, EStructuralFeature eStructuralFeature) throws IOException {
    EClassifier eClassifier = eStructuralFeature.getEType();
    if (eClassifier == EcorePackage.eINSTANCE.getEString()) {
        return jsonReader.nextString();
    } else if (eClassifier == EcorePackage.eINSTANCE.getEDouble()) {
        return jsonReader.nextDouble();
    } else if (eClassifier == EcorePackage.eINSTANCE.getEBoolean()) {
        return jsonReader.nextBoolean();
    } else if (eClassifier == EcorePackage.eINSTANCE.getEInt()) {
        return jsonReader.nextInt();
    } else if (eClassifier == EcorePackage.eINSTANCE.getELong()) {
        return jsonReader.nextLong();
    } else if (eClassifier == EcorePackage.eINSTANCE.getEIntegerObject()) {
        return jsonReader.nextInt();
    } else if (eClassifier == EcorePackage.eINSTANCE.getEByteArray()) {
        return Base64.decodeBase64(jsonReader.nextString());
    } else if (eClassifier == EcorePackage.eINSTANCE.getEDate()) {
        long timestamp = jsonReader.nextLong();
        return new Date(timestamp);
    } else if (eClassifier == EcorePackage.eINSTANCE.getEFloat()) {
        return (float) jsonReader.nextDouble();
    } else if (eClassifier == EcorePackage.eINSTANCE.getEEnum()) {
        EEnum eEnum = (EEnum) eStructuralFeature.getEType();
        return eEnum.getEEnumLiteral(jsonReader.nextString()).getInstance();
    } else if (eClassifier instanceof EClass) {
        if (eStructuralFeature.getEType().getName().equals("IfcGloballyUniqueId")) {
            IfcGloballyUniqueId ifcGloballyUniqueId = Ifc2x3tc1Factory.eINSTANCE.createIfcGloballyUniqueId();
            ifcGloballyUniqueId.setWrappedValue(jsonReader.nextString());
            return ifcGloballyUniqueId;
        } else {/*from   www  .  j  av  a 2 s.  com*/
            throw new RuntimeException();
        }
    } else if (eClassifier instanceof EEnum) {
        EEnum eEnum = (EEnum) eStructuralFeature.getEType();
        if (jsonReader.peek() == JsonToken.BOOLEAN) {
            return eEnum.getEEnumLiteral(jsonReader.nextBoolean() ? "TRUE" : "FALSE").getInstance();
        } else {
            return eEnum.getEEnumLiteral(jsonReader.nextString()).getInstance();
        }
    } else {
        throw new RuntimeException("Unimplemented type " + eStructuralFeature.getEType().getName());
    }
}

From source file:org.coursera.courier.android.adapters.CustomIntAdapter.java

License:Apache License

@Override
public CustomInt read(JsonReader in) throws IOException {
    return new CustomInt(in.nextInt());
}

From source file:org.cyanogenmod.changelog.ChangelogParser.java

License:Open Source License

/**
 * Read ChangeInfo JSON entity/* w w w. j a va  2s .  c  om*/
 * See http://review.cyanogenmod.org/Documentation/rest-api.html
 * @param reader the JsonReader to use
 * @return the parsed Change.
 * @throws IOException
 */
private Change parseChangeInfo(JsonReader reader) throws IOException {
    Change change = new Change();
    reader.beginObject();
    while (reader.hasNext()) {
        switch (reader.nextName()) {
        case "_number":
            change.setChangeId(reader.nextString());
            break;
        case "project":
            change.setProject(reader.nextString());
            break;
        case "subject":
            change.setSubject(reader.nextString());
            break;
        case "updated":
            change.setLastUpdate(parseTimestamp(reader.nextString()));
            break;
        case "insertions":
            change.setInsertions(reader.nextInt());
            break;
        case "deletions":
            change.setDeletions(reader.nextInt());
            break;
        default:
            reader.skipValue();
        }
    }
    reader.endObject();
    return change;
}

From source file:org.eclipse.lsp4j.jsonrpc.debug.adapters.DebugMessageTypeAdapter.java

License:Open Source License

@Override
public Message read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();/* ww  w. java 2s.  c om*/
        return null;
    }

    in.beginObject();
    String messageType = null, method = null, message = null;
    int seq = 0, request_seq = 0;
    Boolean rawSuccess = null;
    Object rawParams = null;
    Object rawBody = null;

    try {

        while (in.hasNext()) {
            String name = in.nextName();
            switch (name) {
            case "seq": {
                seq = in.nextInt();
                break;
            }
            case "request_seq": {
                // on responses we treat the request_seq as the id
                request_seq = in.nextInt();
                break;
            }
            case "type": {
                messageType = in.nextString();
                break;
            }
            case "success": {
                rawSuccess = in.nextBoolean();
                break;
            }
            case "command": {
                method = in.nextString();
                break;
            }
            case "event": {
                method = in.nextString();
                break;
            }
            case "message": {
                if (in.peek() == JsonToken.NULL) {
                    in.nextNull();
                } else {
                    message = in.nextString();
                }
                break;
            }
            case "arguments": {
                rawParams = parseParams(in, method);
                break;
            }
            case "body": {
                rawBody = parseBody(in, messageType, request_seq, method, rawSuccess);
                break;
            }
            default:
                in.skipValue();
            }
        }
        boolean success = rawSuccess != null ? rawSuccess : false;
        Object params = parseParams(rawParams, method);
        Object body = parseBody(rawBody, messageType, request_seq, method, success);

        in.endObject();
        return createMessage(messageType, seq, request_seq, method, success, message, params, body);

    } catch (JsonSyntaxException | MalformedJsonException | EOFException exception) {
        if ("request".equals(messageType) || "event".equals(messageType) || "response".equals(messageType)) {
            // Create a message and bundle it to an exception with an issue that wraps the original exception
            boolean success = rawSuccess != null ? rawSuccess : false;
            Message resultMessage = createMessage(messageType, seq, request_seq, method, success, message,
                    rawParams, rawBody);
            MessageIssue issue = new MessageIssue("Message could not be parsed.",
                    ResponseErrorCode.ParseError.getValue(), exception);
            throw new MessageIssueException(resultMessage, issue);
        } else {
            throw exception;
        }
    }
}

From source file:org.eclipse.lsp4j.jsonrpc.json.adapters.EnumTypeAdapter.java

License:Open Source License

@Override
public T read(JsonReader in) throws IOException {
    JsonToken peek = in.peek();/*from w  ww  .  j  a v a  2 s  .c  om*/
    if (peek == JsonToken.NULL) {
        in.nextNull();
        return null;
    } else if (peek == JsonToken.NUMBER) {
        return valueToConstant.get(in.nextInt());
    } else {
        String string = in.nextString();
        try {
            return valueToConstant.get(Integer.parseInt(string));
        } catch (NumberFormatException e) {
            return nameToConstant.get(string);
        }
    }
}

From source file:org.eclipse.lsp4j.jsonrpc.json.adapters.MessageTypeAdapter.java

License:Open Source License

@Override
public Message read(JsonReader in) throws IOException, JsonIOException, JsonSyntaxException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();// ww w.j  av  a2s . c  o  m
        return null;
    }

    in.beginObject();
    String jsonrpc = null, method = null;
    Either<String, Number> id = null;
    Object rawParams = null;
    Object rawResult = null;
    ResponseError responseError = null;
    try {

        while (in.hasNext()) {
            String name = in.nextName();
            switch (name) {
            case "jsonrpc": {
                jsonrpc = in.nextString();
                break;
            }
            case "id": {
                if (in.peek() == JsonToken.NUMBER)
                    id = Either.forRight(in.nextInt());
                else
                    id = Either.forLeft(in.nextString());
                break;
            }
            case "method": {
                method = in.nextString();
                break;
            }
            case "params": {
                rawParams = parseParams(in, method);
                break;
            }
            case "result": {
                rawResult = parseResult(in, id != null ? id.get().toString() : null);
                break;
            }
            case "error": {
                responseError = gson.fromJson(in, ResponseError.class);
                break;
            }
            default:
                in.skipValue();
            }
        }
        Object params = parseParams(rawParams, method);
        Object result = parseResult(rawResult, id != null ? id.get().toString() : null);

        in.endObject();
        return createMessage(jsonrpc, id, method, params, result, responseError);

    } catch (JsonSyntaxException | MalformedJsonException | EOFException exception) {
        if (id != null || method != null) {
            // Create a message and bundle it to an exception with an issue that wraps the original exception
            Message message = createMessage(jsonrpc, id, method, rawParams, rawResult, responseError);
            MessageIssue issue = new MessageIssue("Message could not be parsed.",
                    ResponseErrorCode.ParseError.getValue(), exception);
            throw new MessageIssueException(message, issue);
        } else {
            throw exception;
        }
    }
}

From source file:org.eclipse.recommenders.utils.gson.MultisetTypeAdapterFactory.java

License:Open Source License

private <E> TypeAdapter<Multiset<E>> newMultisetAdapter(final TypeAdapter<E> elementAdapter) {
    return new TypeAdapter<Multiset<E>>() {
        public void write(JsonWriter out, Multiset<E> multiset) throws IOException {
            if (multiset == null) {
                out.nullValue();//from  w  w  w.j  a  v a2 s  .  co m
                return;
            }

            out.beginArray();
            for (Entry<E> entry : multiset.entrySet()) {
                out.beginObject();
                out.name("id");
                elementAdapter.write(out, entry.getElement());
                out.name("count");
                out.value(entry.getCount());
                out.endObject();
            }
            out.endArray();
        }

        public Multiset<E> read(JsonReader in) throws IOException {
            if (in.peek() == JsonToken.NULL) {
                in.nextNull();
                return null;
            }

            Multiset<E> result = LinkedHashMultiset.create();
            in.beginArray();
            while (in.hasNext()) {
                in.beginObject();
                in.nextName(); // "id"
                E element = elementAdapter.read(in);
                in.nextName(); // "count"
                int count = in.nextInt();
                result.add(element, count);
                in.endObject();
            }
            in.endArray();
            return result;
        }
    };
}

From source file:org.eclipse.thym.core.plugin.registry.CordovaPluginRegistryManager.java

License:Open Source License

private CordovaRegistryPluginInfo parseCordovaRegistryPluginInfo(JsonReader reader) throws IOException {
    CordovaRegistryPluginInfo info = new CordovaRegistryPluginInfo();
    reader.beginObject();//from   w  ww  . j  a v  a  2  s. co m
    reader.skipValue(); // name
    reader.beginArray();
    reader.nextString(); //ecosystem:cordova
    info.setName(safeReadStringValue(reader));
    info.setDescription(safeReadStringValue(reader));
    reader.endArray();
    reader.nextName();
    reader.nextInt();
    reader.endObject();
    return info;
}