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

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

Introduction

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

Prototype

public void nextNull() throws IOException 

Source Link

Document

Consumes the next token from the JSON stream and asserts that it is a literal null.

Usage

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 ww w.  j a v  a2 s . c om

    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.FieldNameTypeAdapter.java

License:Open Source License

@Override
public IFieldName read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();
        return null;
    } else {//from w  ww.  jav a2  s .  c o m
        String identifier = in.nextString();
        return VmFieldName.get(identifier);
    }
}

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

License:Open Source License

@Override
public File read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();
        return null;
    } else {//from  ww w.j a v a  2  s .  c o  m
        return new File(in.nextString());
    }
}

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();
        return null;
    } else {//from w  w w  .ja  v  a  2 s. co  m
        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();/*www.j  a v a 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();
        return null;
    } else {/* www  .  j  a v a 2  s.  com*/
        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();
        return null;
    } else {//from w w w.  j  av  a2s.c o m
        String identifier = in.nextString();
        return VmTypeName.get(identifier);
    }
}

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();
        return null;
    }/*from w  ww.  j a v  a  2 s .c  om*/
    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();//from  w w w  .j  a  va  2  s . c om
    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;
}

From source file:org.eclipse.tm4e.core.internal.parser.json.JSONPListParser.java

License:Open Source License

public T parse(InputStream contents) throws Exception {
    PList<T> pList = new PList<T>(theme);
    JsonReader reader = new JsonReader(new InputStreamReader(contents, StandardCharsets.UTF_8));
    // reader.setLenient(true);
    boolean parsing = true;
    while (parsing) {
        JsonToken nextToken = reader.peek();
        switch (nextToken) {
        case BEGIN_ARRAY:
            pList.startElement(null, "array", null, null);
            reader.beginArray();/*from  w  w w . jav a  2s.c om*/
            break;
        case END_ARRAY:
            pList.endElement(null, "array", null);
            reader.endArray();
            break;
        case BEGIN_OBJECT:
            pList.startElement(null, "dict", null, null);
            reader.beginObject();
            break;
        case END_OBJECT:
            pList.endElement(null, "dict", null);
            reader.endObject();
            break;
        case NAME:
            String lastName = reader.nextName();
            pList.startElement(null, "key", null, null);
            pList.characters(lastName.toCharArray(), 0, lastName.length());
            pList.endElement(null, "key", null);
            break;
        case NULL:
            reader.nextNull();
            break;
        case BOOLEAN:
            reader.nextBoolean();
            break;
        case NUMBER:
            reader.nextLong();
            break;
        case STRING:
            String value = reader.nextString();
            pList.startElement(null, "string", null, null);
            pList.characters(value.toCharArray(), 0, value.length());
            pList.endElement(null, "string", null);
            break;
        case END_DOCUMENT:
            parsing = false;
            break;
        default:
            break;
        }
    }
    reader.close();
    return pList.getResult();
}