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

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

Introduction

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

Prototype

public void endObject() throws IOException 

Source Link

Document

Consumes the next token from the JSON stream and asserts that it is the end of the current object.

Usage

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

License:Open Source License

@SuppressWarnings("unchecked")
@Override//from w  w w.  j  a v a 2  s  .c o  m
public Throwable read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();
        return null;
    }

    in.beginObject();
    String message = null;
    Throwable cause = null;
    while (in.hasNext()) {
        String name = in.nextName();
        switch (name) {
        case "message": {
            message = in.nextString();
            break;
        }
        case "cause": {
            cause = read(in);
            break;
        }
        default:
            in.skipValue();
        }
    }
    in.endObject();

    try {
        Constructor<Throwable> constructor;
        if (message == null && cause == null) {
            constructor = (Constructor<Throwable>) typeToken.getRawType().getDeclaredConstructor();
            return constructor.newInstance();
        } else if (message == null) {
            constructor = (Constructor<Throwable>) typeToken.getRawType()
                    .getDeclaredConstructor(Throwable.class);
            return constructor.newInstance(cause);
        } else if (cause == null) {
            constructor = (Constructor<Throwable>) typeToken.getRawType().getDeclaredConstructor(String.class);
            return constructor.newInstance(message);
        } else {
            constructor = (Constructor<Throwable>) typeToken.getRawType().getDeclaredConstructor(String.class,
                    Throwable.class);
            return constructor.newInstance(message, cause);
        }
    } catch (NoSuchMethodException e) {
        if (message == null && cause == null)
            return new RuntimeException();
        else if (message == null)
            return new RuntimeException(cause);
        else if (cause == null)
            return new RuntimeException(message);
        else
            return new RuntimeException(message, cause);
    } catch (Exception e) {
        throw new JsonParseException(e);
    }
}

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

License:Open Source License

public ModifyContextImpl read() throws IOException {
    this.numberOfBytes = 0;

    final Reader reader = new InputStreamReader(this.stream, StandardCharsets.UTF_8);

    final ChannelState.Builder state = new ChannelState.Builder();

    Boolean locked = null;/*from  w  ww.jav a2  s.  c  o  m*/

    Map<MetaKey, CacheEntryInformation> cacheEntries = Collections.emptyMap();
    Map<String, ArtifactInformation> artifacts = Collections.emptyMap();
    Map<MetaKey, String> extractedMetaData = Collections.emptyMap();
    Map<MetaKey, String> providedMetaData = Collections.emptyMap();
    Map<String, String> aspects = new HashMap<>();

    final JsonReader jr = new JsonReader(reader);

    jr.beginObject();
    while (jr.hasNext()) {
        final String name = jr.nextName();
        switch (name) {
        case "locked":
            state.setLocked(locked = jr.nextBoolean());
            break;
        case "creationTimestamp":
            state.setCreationTimestamp(readTime(jr));
            break;
        case "modificationTimestamp":
            state.setModificationTimestamp(readTime(jr));
            break;
        case "cacheEntries":
            cacheEntries = readCacheEntries(jr);
            break;
        case "artifacts":
            artifacts = readArtifacts(jr);
            break;
        case "extractedMetaData":
            extractedMetaData = readMetadata(jr);
            break;
        case "providedMetaData":
            providedMetaData = readMetadata(jr);
            break;
        case "validationMessages":
            state.setValidationMessages(readValidationMessages(jr));
            break;
        case "aspects":
            aspects = readAspects(jr);
            break;
        default:
            jr.skipValue();
            break;
        }
    }
    jr.endObject();

    if (locked == null) {
        throw new IOException("Missing values for channel");
    }

    // transient information

    state.setNumberOfArtifacts(artifacts.size());
    state.setNumberOfBytes(this.numberOfBytes);

    // create result

    return new ModifyContextImpl(this.channelId, this.store, this.cacheStore, state.build(), aspects, artifacts,
            cacheEntries, extractedMetaData, providedMetaData);
}

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

License:Open Source License

private Map<String, String> readAspects(final JsonReader jr) throws IOException {
    final Map<String, String> result = new HashMap<>();

    jr.beginObject();/*from   www . j  ava  2  s . c om*/
    while (jr.hasNext()) {
        switch (jr.nextName()) {
        case "map":
            jr.beginObject();
            while (jr.hasNext()) {
                final String id = jr.nextName();
                String value = null;
                if (jr.peek() == JsonToken.STRING) {
                    value = jr.nextString();
                } else {
                    jr.skipValue();
                }
                result.put(id, value);
            }
            jr.endObject();
            break;
        }
    }
    jr.endObject();

    return result;
}

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();//  w  w  w.  ja  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, String> readMetadata(final JsonReader jr) throws IOException {
    final Map<MetaKey, String> result = new HashMap<>();

    jr.beginObject();// w  w  w  .j  a v a 2  s .  c  o  m

    while (jr.hasNext()) {
        final String name = jr.nextName();
        if (jr.peek() == JsonToken.NULL) {
            jr.skipValue();
        } else {
            final String value = jr.nextString();
            result.put(MetaKey.fromString(name), value);
        }
    }

    jr.endObject();

    return result;
}

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

License:Open Source License

private ValidationMessage readValidationMessage(final JsonReader jr) throws IOException {
    String aspectId = null;//from   www.  j  av a 2  s. co  m
    Severity severity = null;
    String message = null;
    Set<String> artifactIds = Collections.emptySet();

    jr.beginObject();
    while (jr.hasNext()) {
        final String name = jr.nextName();
        switch (name) {
        case "aspectId":
            aspectId = jr.nextString();
            break;
        case "severity":
            severity = Severity.valueOf(jr.nextString());
            break;
        case "message":
            message = jr.nextString();
            break;
        case "artifactIds":
            artifactIds = readSet(jr);
            break;
        }
    }
    jr.endObject();

    if (aspectId == null || severity == null || message == null) {
        throw new IOException("Missing values in validation message");
    }

    return new ValidationMessage(aspectId, severity, message, artifactIds);
}

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();/*from w w  w .  ja v a 2  s  .  c  o  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;
}

From source file:org.eclipse.packagedrone.repo.MetaKeys.java

License:Open Source License

public static Map<MetaKey, String> readJson(final Reader input) throws IOException {
    @SuppressWarnings("resource")
    final JsonReader reader = new JsonReader(input);

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

    reader.beginObject();/*from  www  . ja v  a 2  s  .  c o m*/

    while (reader.hasNext()) {
        final String name = reader.nextName();
        if (reader.peek() == JsonToken.NULL) {
            reader.nextNull();
        } else {
            result.put(MetaKey.fromString(name), reader.nextString());
        }
    }

    reader.endObject();

    return result;
}

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  www.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.smarthome.storage.json.PropertiesTypeAdapter.java

License:Open Source License

@Override
public Map<String, Object> read(JsonReader in) throws IOException {
    // gson implementation code is modified when deserializing numbers
    JsonToken peek = in.peek();//w w  w.  j  a va2s .c o  m
    if (peek == JsonToken.NULL) {
        in.nextNull();
        return null;
    }

    Map<String, Object> map = constructor.get(TOKEN).construct();

    if (peek == JsonToken.BEGIN_ARRAY) {
        in.beginArray();
        while (in.hasNext()) {
            in.beginArray(); // entry array
            String key = keyAdapter.read(in);

            // modification
            Object value = getValue(in);

            Object replaced = map.put(key, value);
            if (replaced != null) {
                throw new JsonSyntaxException("duplicate key: " + key);
            }
            in.endArray();
        }
        in.endArray();
    } else {
        in.beginObject();
        while (in.hasNext()) {
            JsonReaderInternalAccess.INSTANCE.promoteNameToValue(in);
            String key = keyAdapter.read(in);

            // modification
            Object value = getValue(in);

            Object replaced = map.put(key, value);
            if (replaced != null) {
                throw new JsonSyntaxException("duplicate key: " + key);
            }
        }
        in.endObject();
    }
    return map;
}