Example usage for com.google.gson.internal JsonReaderInternalAccess INSTANCE

List of usage examples for com.google.gson.internal JsonReaderInternalAccess INSTANCE

Introduction

In this page you can find the example usage for com.google.gson.internal JsonReaderInternalAccess INSTANCE.

Prototype

JsonReaderInternalAccess INSTANCE

To view the source code for com.google.gson.internal JsonReaderInternalAccess INSTANCE.

Click Source Link

Usage

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 v  a2 s  . 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;
}

From source file:org.jclouds.json.internal.IgnoreNullMapTypeAdapterFactory.java

License:Apache License

private <K, V> TypeAdapter<Map<K, V>> newMapAdapter(final TypeAdapter<K> keyAdapter,
        final TypeAdapter<V> valueAdapter) {
    return new TypeAdapter<Map<K, V>>() {
        public void write(JsonWriter out, Map<K, V> value) throws IOException {
            out.beginObject();/*from w w w .ja  v a  2  s.c  o  m*/
            for (Map.Entry<K, V> element : value.entrySet()) {
                out.name(String.valueOf(element.getKey()));
                valueAdapter.write(out, element.getValue());
            }
            out.endObject();
        }

        public Map<K, V> read(JsonReader in) throws IOException {
            Map<K, V> result = Maps.newLinkedHashMap();
            in.beginObject();
            while (in.hasNext()) {
                JsonReaderInternalAccess.INSTANCE.promoteNameToValue(in);
                K name = keyAdapter.read(in);
                V value = valueAdapter.read(in);
                if (value != null)
                    result.put(name, value);
            }
            in.endObject();
            return result;
        }
    }.nullSafe();
}

From source file:org.jclouds.json.internal.IgnoreNullMultimapTypeAdapterFactory.java

License:Apache License

private <K, V> TypeAdapter<Multimap<K, V>> newMapAdapter(final TypeAdapter<K> keyAdapter,
        final TypeAdapter<V> valueAdapter) {
    return new TypeAdapter<Multimap<K, V>>() {
        public void write(JsonWriter out, Multimap<K, V> map) throws IOException {
            out.beginObject();//from  www  . j av  a  2 s .co m
            for (K key : map.keySet()) {
                out.name(String.valueOf(key));
                out.beginArray();
                for (V value : map.get(key)) {
                    valueAdapter.write(out, value);
                }
                out.endArray();
            }
            out.endObject();
        }

        public Multimap<K, V> read(JsonReader in) throws IOException {
            ImmutableMultimap.Builder<K, V> result = ImmutableListMultimap.builder();
            in.beginObject();
            while (in.hasNext()) {
                JsonReaderInternalAccess.INSTANCE.promoteNameToValue(in);
                K name = keyAdapter.read(in);
                in.beginArray();
                while (in.hasNext()) {
                    V value = valueAdapter.read(in);
                    if (value != null)
                        result.put(name, value);
                }
                in.endArray();
            }
            in.endObject();
            return result.build();
        }
    }.nullSafe();
}