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

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

Introduction

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

Prototype

public long nextLong() throws IOException 

Source Link

Document

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

Usage

From source file:de.innovationgate.utils.GsonUtils.java

License:Open Source License

/**
 * Reads the next long value from the reader or null if it is a null value
 * @param reader The reader//  w w  w.  j a va2s . co  m
 * @return Long value or null
 * @throws IOException
 */
public static Long nextLongOrNull(JsonReader reader) throws IOException {
    if (reader.peek() != JsonToken.NULL) {
        return reader.nextLong();
    } else {
        reader.nextNull();
        return null;
    }
}

From source file:io.datakernel.datagraph.server.GsonStreamIdAdapter.java

License:Apache License

@Override
public StreamId read(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();//from w ww .j  av a  2s.  c  om
        return null;
    }
    return new StreamId(reader.nextLong());
}

From source file:org.apache.hadoop.fs.http.client.FileStatus.java

License:Apache License

public static TypeAdapter adapter() {
    return new TypeAdapter<FileStatus>() {
        @Override//from  w  ww  .  jav  a  2  s. co m
        public void write(JsonWriter out, FileStatus value) throws IOException {
            /* not implemented */
        }

        @Override
        public FileStatus read(JsonReader in) throws IOException {
            FileStatus instance = null;
            in.setLenient(true);

            if (in.peek() == JsonToken.BEGIN_OBJECT) {
                in.beginObject();

                if (in.nextName().equalsIgnoreCase("FileStatus")) {
                    String name;
                    in.beginObject();
                    instance = new FileStatus();

                    while (in.hasNext()) {
                        name = in.nextName();

                        if (name.equalsIgnoreCase("accessTime")) {
                            instance.accessTime = in.nextLong();
                        } else if (name.equalsIgnoreCase("blockSize")) {
                            instance.blockSize = in.nextInt();
                        } else if (name.equalsIgnoreCase("length")) {
                            instance.length = in.nextLong();
                        } else if (name.equalsIgnoreCase("modificationTime")) {
                            instance.modTime = in.nextLong();
                        } else if (name.equalsIgnoreCase("replication")) {
                            instance.replication = in.nextInt();
                        } else if (name.equalsIgnoreCase("group")) {
                            instance.group = in.nextString();
                        } else if (name.equalsIgnoreCase("owner")) {
                            instance.owner = in.nextString();
                        } else if (name.equalsIgnoreCase("pathSuffix")) {
                            instance.suffix = in.nextString();
                        } else if (name.equalsIgnoreCase("permission")) {
                            instance.permission = in.nextString();
                        } else if (name.equalsIgnoreCase("type")) {
                            instance.type = FileType.valueOf(in.nextString());
                        }
                    }

                    in.endObject();
                }

                in.endObject();
            }
            return instance;
        }
    };
}

From source file:org.bimserver.client.ClientIfcModel.java

License:Open Source License

@SuppressWarnings({ "unchecked", "rawtypes" })
private void processDownload(Long download)
        throws BimServerClientException, UserException, ServerException, PublicInterfaceNotFoundException {
    WaitingList<Long> waitingList = new WaitingList<Long>();
    try {//  ww  w  . j a  v  a2 s .  c  o  m
        InputStream downloadData = bimServerClient.getDownloadData(download, getIfcSerializerOid());
        boolean log = false;
        // TODO Make this streaming again, make sure the EmfSerializer getInputStream method is working properly
        if (log) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            if (downloadData instanceof SerializerInputstream) {
                SerializerInputstream serializerInputStream = (SerializerInputstream) downloadData;
                serializerInputStream.getEmfSerializer().writeToOutputStream(baos);
            } else {
                IOUtils.copy((InputStream) downloadData, baos);
            }
            FileOutputStream fos = new FileOutputStream(new File(download + ".json"));
            IOUtils.write(baos.toByteArray(), fos);
            fos.close();
            downloadData = new ByteArrayInputStream(baos.toByteArray());
        } else {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            if (downloadData instanceof SerializerInputstream) {
                SerializerInputstream serializerInputStream = (SerializerInputstream) downloadData;
                serializerInputStream.getEmfSerializer().writeToOutputStream(baos);
            } else {
                IOUtils.copy((InputStream) downloadData, baos);
            }
            downloadData = new ByteArrayInputStream(baos.toByteArray());
        }
        JsonReader jsonReader = new JsonReader(new InputStreamReader(downloadData, Charsets.UTF_8));
        try {
            jsonReader.beginObject();
            if (jsonReader.nextName().equals("objects")) {
                jsonReader.beginArray();
                while (jsonReader.hasNext()) {
                    jsonReader.beginObject();
                    if (jsonReader.nextName().equals("__oid")) {
                        long oid = jsonReader.nextLong();
                        if (jsonReader.nextName().equals("__type")) {
                            String type = jsonReader.nextString();
                            EClass eClass = (EClass) Ifc2x3tc1Package.eINSTANCE.getEClassifier(type);
                            if (eClass == null) {
                                throw new BimServerClientException("No class found with name " + type);
                            }
                            if (jsonReader.nextName().equals("__state")) {
                                String state = jsonReader.nextString();
                                IdEObject object = null;
                                if (containsNoFetch(oid)) {
                                    object = getNoFetch(oid);
                                } else {
                                    object = (IdEObject) Ifc2x3tc1Factory.eINSTANCE.create(eClass);
                                    ((IdEObjectImpl) object).eSetStore(eStore);
                                    ((IdEObjectImpl) object).setOid(oid);
                                    add(oid, object);
                                }
                                if (state.equals("NOT_LOADED")) {
                                    ((IdEObjectImpl) object).setLoadingState(State.TO_BE_LOADED);
                                } else {
                                    while (jsonReader.hasNext()) {
                                        String featureName = jsonReader.nextName();
                                        boolean embedded = false;
                                        if (featureName.startsWith("__ref")) {
                                            featureName = featureName.substring(5);
                                        } else if (featureName.startsWith("__emb")) {
                                            embedded = true;
                                            featureName = featureName.substring(5);
                                        }
                                        EStructuralFeature eStructuralFeature = eClass
                                                .getEStructuralFeature(featureName);
                                        if (eStructuralFeature == null) {
                                            throw new BimServerClientException("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;
                                                while (jsonReader.hasNext()) {
                                                    if (embedded) {
                                                        List list = (List) object.eGet(eStructuralFeature);
                                                        jsonReader.beginObject();
                                                        String n = jsonReader.nextName();
                                                        if (n.equals("__type")) {
                                                            String t = jsonReader.nextString();
                                                            IdEObject wrappedObject = (IdEObject) Ifc2x3tc1Factory.eINSTANCE
                                                                    .create((EClass) Ifc2x3tc1Package.eINSTANCE
                                                                            .getEClassifier(t));
                                                            if (jsonReader.nextName().equals("value")) {
                                                                EStructuralFeature wv = wrappedObject.eClass()
                                                                        .getEStructuralFeature("wrappedValue");
                                                                wrappedObject.eSet(wv,
                                                                        readPrimitive(jsonReader, wv));
                                                                list.add(wrappedObject);
                                                            } else {
                                                                // error
                                                            }
                                                        } else if (n.equals("oid")) {
                                                            // Sometimes embedded is true, bot also referenced are included, those are always embdedded in an object
                                                            long refOid = jsonReader.nextLong();
                                                            if (containsNoFetch(refOid)) {
                                                                IdEObject refObj = getNoFetch(refOid);
                                                                AbstractEList l = (AbstractEList) object
                                                                        .eGet(eStructuralFeature);
                                                                while (l.size() <= index) {
                                                                    l.addUnique(refObj.eClass().getEPackage()
                                                                            .getEFactoryInstance()
                                                                            .create(refObj.eClass()));
                                                                }
                                                                l.setUnique(index, refObj);
                                                            } else {
                                                                waitingList.add(refOid, new ListWaitingObject(
                                                                        object, eStructuralFeature, index));
                                                            }
                                                        }
                                                        jsonReader.endObject();
                                                    } else {
                                                        long refOid = jsonReader.nextLong();
                                                        if (containsNoFetch(refOid)) {
                                                            IdEObject refObj = getNoFetch(refOid);
                                                            AbstractEList l = (AbstractEList) object
                                                                    .eGet(eStructuralFeature);
                                                            while (l.size() <= index) {
                                                                l.addUnique(refObj.eClass().getEPackage()
                                                                        .getEFactoryInstance()
                                                                        .create(refObj.eClass()));
                                                            }
                                                            l.setUnique(index, refObj);
                                                        } else {
                                                            waitingList.add(refOid, new ListWaitingObject(
                                                                    object, eStructuralFeature, index));
                                                        }
                                                        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");
                                                    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 (embedded) {
                                                    jsonReader.beginObject();
                                                    if (jsonReader.nextName().equals("__type")) {
                                                        String t = jsonReader.nextString();
                                                        IdEObject wrappedObject = (IdEObject) Ifc2x3tc1Factory.eINSTANCE
                                                                .create((EClass) Ifc2x3tc1Package.eINSTANCE
                                                                        .getEClassifier(t));
                                                        if (jsonReader.nextName().equals("value")) {
                                                            EStructuralFeature wv = wrappedObject.eClass()
                                                                    .getEStructuralFeature("wrappedValue");
                                                            wrappedObject.eSet(wv,
                                                                    readPrimitive(jsonReader, wv));
                                                            object.eSet(eStructuralFeature, wrappedObject);
                                                        } else {
                                                            // error
                                                        }
                                                    }
                                                    jsonReader.endObject();
                                                } else {
                                                    long refOid = jsonReader.nextLong();
                                                    if (containsNoFetch(refOid)) {
                                                        IdEObject refObj = getNoFetch(refOid);
                                                        object.eSet(eStructuralFeature, refObj);
                                                    } else {
                                                        waitingList.add(refOid, new SingleWaitingObject(object,
                                                                eStructuralFeature));
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                if (waitingList.containsKey(oid)) {
                                    try {
                                        waitingList.updateNode(oid, eClass, object);
                                    } catch (DeserializeException e) {
                                        LOGGER.error("", e);
                                    }
                                }
                            }
                        }
                    }
                    jsonReader.endObject();
                }
                jsonReader.endArray();
            }
            jsonReader.endObject();
        } catch (IfcModelInterfaceException e1) {
            LOGGER.error("", e1);
        } finally {
            jsonReader.close();
        }
    } catch (IOException e) {
        LOGGER.error("", e);
    } catch (SerializerException e) {
        LOGGER.error("", e);
    } finally {
        waitingList.dumpIfNotEmpty();
        bimServerClient.getServiceInterface().cleanupLongAction(download);
    }
}

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

License:Open Source License

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override//  ww  w .  ja  va2s  . c o m
public IfcModelInterface read(InputStream in, String filename, long fileSize) throws DeserializeException {
    IfcModelInterface model = new IfcModel();
    WaitingList<Long> waitingList = new WaitingList<Long>();
    JsonReader jsonReader = new JsonReader(new InputStreamReader(in));
    try {
        jsonReader.beginObject();
        if (jsonReader.nextName().equals("objects")) {
            jsonReader.beginArray();
            while (jsonReader.hasNext()) {
                jsonReader.beginObject();
                if (jsonReader.nextName().equals("__oid")) {
                    long oid = jsonReader.nextLong();
                    if (jsonReader.nextName().equals("__type")) {
                        String type = jsonReader.nextString();
                        EClass eClass = (EClass) Ifc2x3tc1Package.eINSTANCE.getEClassifier(type);
                        if (eClass == null) {
                            throw new DeserializeException("No class found with name " + type);
                        }
                        IdEObject object = (IdEObject) Ifc2x3tc1Factory.eINSTANCE.create(eClass);
                        // ((IdEObjectImpl) object).setDelegate(new
                        // ClientDelegate(this, object, null));
                        ((IdEObjectImpl) object).setOid(oid);
                        model.add(object.getOid(), object);
                        while (jsonReader.hasNext()) {
                            String featureName = jsonReader.nextName();
                            boolean embedded = false;
                            if (featureName.startsWith("__ref")) {
                                featureName = featureName.substring(5);
                            } else if (featureName.startsWith("__emb")) {
                                embedded = true;
                                featureName = featureName.substring(5);
                            }
                            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;
                                    while (jsonReader.hasNext()) {
                                        if (embedded) {
                                            List list = (List) object.eGet(eStructuralFeature);
                                            jsonReader.beginObject();
                                            if (jsonReader.nextName().equals("__type")) {
                                                String t = jsonReader.nextString();
                                                IdEObject wrappedObject = (IdEObject) Ifc2x3tc1Factory.eINSTANCE
                                                        .create((EClass) Ifc2x3tc1Package.eINSTANCE
                                                                .getEClassifier(t));
                                                if (jsonReader.nextName().equals("value")) {
                                                    EStructuralFeature wv = wrappedObject.eClass()
                                                            .getEStructuralFeature("wrappedValue");
                                                    wrappedObject.eSet(wv, readPrimitive(jsonReader, wv));
                                                    list.add(wrappedObject);
                                                } else {
                                                    // error
                                                }
                                            }
                                            jsonReader.endObject();
                                        } else {
                                            long refOid = jsonReader.nextLong();
                                            waitingList.add(refOid,
                                                    new ListWaitingObject(object, eStructuralFeature, index));
                                            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");
                                        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("__type")) {
                                            String t = jsonReader.nextString();
                                            IdEObject wrappedObject = (IdEObject) Ifc2x3tc1Factory.eINSTANCE
                                                    .create((EClass) Ifc2x3tc1Package.eINSTANCE
                                                            .getEClassifier(t));
                                            if (jsonReader.nextName().equals("value")) {
                                                EStructuralFeature wv = wrappedObject.eClass()
                                                        .getEStructuralFeature("wrappedValue");
                                                wrappedObject.eSet(wv, readPrimitive(jsonReader, wv));
                                                object.eSet(eStructuralFeature, wrappedObject);
                                            } else {
                                                // error
                                            }
                                        }
                                        jsonReader.endObject();
                                    } else {
                                        waitingList.add(jsonReader.nextLong(),
                                                new SingleWaitingObject(object, eStructuralFeature));
                                    }
                                }
                            }
                        }
                        if (waitingList.containsKey(oid)) {
                            waitingList.updateNode(oid, eClass, object);
                        }
                    }
                }
                jsonReader.endObject();
            }
            jsonReader.endArray();
        }
        jsonReader.endObject();
    } catch (IOException e1) {
        e1.printStackTrace();
    } catch (IfcModelInterfaceException e1) {
        e1.printStackTrace();
    } finally {
        try {
            jsonReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return model;
}

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;//www. java2 s. co  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   w ww. j av a 2  s  .co m*/
            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.DateTimeAdapter.java

License:Apache License

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

From source file:org.eclipse.packagedrone.repo.channel.apm.ChannelReader.java

License:Open Source License

private Map<String, ArtifactInformation> readArtifacts(final JsonReader jr) throws IOException {
    jr.beginObject();/*from w w w. j  a  v  a  2 s . c o  m*/

    final Map<String, ArtifactInformation> result = new HashMap<>();

    while (jr.hasNext()) {
        final String id = jr.nextName();
        jr.beginObject();

        String name = null;
        Long size = null;
        Instant creationTimestamp = null;
        String parentId = null;
        Set<String> childIds = Collections.emptySet();
        Set<String> facets = Collections.emptySet();
        String virtualizerAspectId = null;
        List<ValidationMessage> validationMessages = Collections.emptyList();
        Map<MetaKey, String> extractedMetaData = Collections.emptyMap();
        Map<MetaKey, String> providedMetaData = Collections.emptyMap();

        while (jr.hasNext()) {
            final String ele = jr.nextName();
            switch (ele) {
            case "name":
                name = jr.nextString();
                break;
            case "size":
                size = jr.nextLong();
                break;
            case "date":
                creationTimestamp = readTime(jr);
                break;
            case "parentId":
                parentId = jr.nextString();
                break;
            case "childIds":
                childIds = readSet(jr);
                break;
            case "facets":
                facets = readSet(jr);
                break;
            case "virtualizerAspectId":
                virtualizerAspectId = jr.nextString();
                break;
            case "extractedMetaData":
                extractedMetaData = readMetadata(jr);
                break;
            case "providedMetaData":
                providedMetaData = readMetadata(jr);
                break;
            case "validationMessages":
                validationMessages = readValidationMessages(jr);
                break;
            default:
                jr.skipValue();
                break;
            }
        }
        jr.endObject();

        if (id == null || name == null || size == null || creationTimestamp == null) {
            throw new IOException("Missing values for artifact");
        }

        this.numberOfBytes += size;

        result.put(id, new ArtifactInformation(id, parentId, childIds, name, size, creationTimestamp, facets,
                validationMessages, providedMetaData, extractedMetaData, virtualizerAspectId));
    }

    jr.endObject();

    return result;
}

From source file:org.eclipse.packagedrone.repo.channel.apm.ChannelReader.java

License:Open Source License

private Map<MetaKey, CacheEntryInformation> readCacheEntries(final JsonReader jr) throws IOException {
    final Map<MetaKey, CacheEntryInformation> result = new HashMap<>();

    jr.beginObject();//w  w w . j  ava 2s .  co m
    while (jr.hasNext()) {
        final String entryName = jr.nextName();
        jr.beginObject();

        String name = null;
        Long size = null;
        String mimeType = null;
        Instant timestamp = null;

        while (jr.hasNext()) {
            final String ele = jr.nextName();
            switch (ele) {
            case "name":
                name = jr.nextString();
                break;
            case "size":
                size = jr.nextLong();
                break;
            case "mimeType":
                mimeType = jr.nextString();
                break;
            case "timestamp":
                timestamp = readTime(jr);
                break;
            default:
                jr.skipValue();
                break;
            }
        }

        if (name == null || size == null || mimeType == null || timestamp == null) {
            throw new IOException("Invalid format");
        }

        jr.endObject();

        final MetaKey key = MetaKey.fromString(entryName);

        result.put(key, new CacheEntryInformation(key, name, size, mimeType, timestamp));
    }
    jr.endObject();

    return result;
}