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

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

Introduction

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

Prototype

public JsonToken peek() throws IOException 

Source Link

Document

Returns the type of the next token without consuming it.

Usage

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

License:Open Source License

@Override
public IMethodName read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();/*w w  w  . ja  v a2 s. c  o  m*/
        return null;
    } else {
        String identifier = in.nextString();
        return VmMethodName.get(identifier);
    }
}

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();//  w  ww.  j  a va  2 s .  c o  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.recommenders.utils.gson.PackageNameTypeAdapter.java

License:Open Source License

@Override
public IPackageName read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();//from   ww w  . j  a v  a  2s . com
        return null;
    } else {
        String identifier = in.nextString();
        return VmPackageName.get(identifier);
    }
}

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

License:Open Source License

@Override
public ITypeName read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();// w  ww.  ja  v  a2s. c o m
        return null;
    } else {
        String identifier = in.nextString();
        return VmTypeName.get(identifier);
    }
}

From source file:org.eclipse.smarthome.automation.parser.gson.internal.RuleGSONParser.java

License:Open Source License

@Override
public Set<Rule> parse(InputStreamReader reader) throws ParsingException {
    JsonReader jr = new JsonReader(reader);
    try {/*from w  w  w  . ja  v  a2 s  . c o m*/
        if (jr.hasNext()) {
            JsonToken token = jr.peek();
            if (JsonToken.BEGIN_ARRAY.equals(token)) {
                Rule[] rules = gson.fromJson(jr, Rule[].class);
                return new HashSet<Rule>(Arrays.asList(rules));
            } else {
                Rule rule = gson.fromJson(jr, Rule.class);
                Set<Rule> rules = new HashSet<Rule>();
                rules.add(rule);
                return rules;
            }
        }
    } catch (Exception e) {
        throw new ParsingException(new ParsingNestedException(ParsingNestedException.RULE, null, e));
    } finally {
        try {
            jr.close();
        } catch (IOException e) {
        }
    }
    return Collections.emptySet();
}

From source file:org.eclipse.smarthome.automation.parser.gson.internal.TemplateGSONParser.java

License:Open Source License

@Override
public Set<Template> parse(InputStreamReader reader) throws ParsingException {
    JsonReader jr = new JsonReader(reader);
    try {/*from   w w  w .j a  v  a2s. c om*/
        if (jr.hasNext()) {
            JsonToken token = jr.peek();
            if (JsonToken.BEGIN_ARRAY.equals(token)) {
                Template[] templates = gson.fromJson(jr, RuleTemplate[].class);
                return new HashSet<Template>(Arrays.asList(templates));
            } else {
                Template template = gson.fromJson(jr, RuleTemplate.class);
                Set<Template> templates = new HashSet<Template>();
                templates.add(template);
                return templates;
            }
        }
    } catch (Exception e1) {
        throw new ParsingException(new ParsingNestedException(ParsingNestedException.TEMPLATE, null, e1));
    } finally {
        try {
            jr.close();
        } catch (IOException e) {
        }
    }
    return Collections.emptySet();
}

From source file:org.eclipse.smarthome.persistence.mapdb.internal.StateTypeAdapter.java

License:Open Source License

@Override
public State read(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();//ww w.  j  a  v  a2  s .c  o  m
        return null;
    }
    String value = reader.nextString();
    String[] parts = value.split(TYPE_SEPARATOR);
    String valueTypeName = parts[0];
    String valueAsString = parts[1];

    try {
        @SuppressWarnings("unchecked")
        Class<? extends State> valueType = (Class<? extends State>) Class.forName(valueTypeName);
        List<Class<? extends State>> types = Collections.singletonList(valueType);
        return TypeParser.parseState(types, valueAsString);
    } catch (Exception e) {
        logger.warn("Couldn't deserialize state '{}': {}", value, e.getMessage());
    }
    return null;
}

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();
    if (peek == JsonToken.NULL) {
        in.nextNull();// www  .j  a v a 2 s .c o m
        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;
}

From source file:org.eclipse.smarthome.storage.json.PropertiesTypeAdapter.java

License:Open Source License

private Object getValue(JsonReader in) throws IOException {
    Object value = null;/*from  ww w.  j a v  a  2  s.  c  om*/

    // if the next json token is a number we read it as a BigDecimal,
    // otherwise use the default adapter to read it
    if (JsonToken.NUMBER.equals(in.peek())) {
        String inString = in.nextString();
        if (inString.endsWith(".0")) {
            value = new BigDecimal(inString).setScale(0);
        } else {
            value = new BigDecimal(inString);
        }
    } else {
        value = valueAdapter.read(in);
    }

    return value;

}

From source file:org.eclipse.smarthome.storage.mapdb.internal.PropertiesTypeAdapter.java

License:Open Source License

private Object getValue(JsonReader in) throws IOException {
    Object value = null;/*from  w  ww . j a v a2  s . co m*/

    // if the next json token is a number we read it as a BigDecimal,
    // otherwise use the default adapter to read it
    if (JsonToken.NUMBER.equals(in.peek())) {
        value = new BigDecimal(in.nextString());
    } else {
        value = valueAdapter.read(in);
    }

    return value;
}