Example usage for com.google.gson.stream JsonToken BEGIN_OBJECT

List of usage examples for com.google.gson.stream JsonToken BEGIN_OBJECT

Introduction

In this page you can find the example usage for com.google.gson.stream JsonToken BEGIN_OBJECT.

Prototype

JsonToken BEGIN_OBJECT

To view the source code for com.google.gson.stream JsonToken BEGIN_OBJECT.

Click Source Link

Document

The opening of a JSON object.

Usage

From source file:com.gilecode.yagson.adapters.TypeAdviceReadingSimpleAdapterWrapper.java

License:Apache License

@Override
public T read(JsonReader in, ReadContext ctx) throws IOException {

    if (in.peek() == JsonToken.BEGIN_OBJECT) {
        // if '{' is found for the simple type, we expect and parse type advice here, and fail otherwise
        return TypeUtils.readTypeAdvisedValue(gson, in, null, ctx);

    } else {//  ww w  . j  av a  2 s  .  com
        // no type advice, use delegate
        return delegateTypeAdapter.read(in, ctx);
    }
}

From source file:com.gilecode.yagson.ReadContext.java

License:Apache License

public <T> T doRead(JsonReader reader, TypeAdapter<T> typeAdapter, String pathElement) throws IOException {

    // PROBLEM: as JsonReader does not support lookaheads for more than one token, we cannot
    //    distinguish regular objects like "{field:value}" from type-advised primitives like
    //    "{@type:Long, @val:1}" here.
    //// w w  w. j av a2  s .co  m
    // SOLUTION: all non-Simple TypeAdapters shall handle type advices themselves, so we delegate to them.
    //     For simple delegates, if '{' found, we expect and parse type advice here, and fail otherwise

    if (AdapterUtils.isSimpleTypeAdapter(typeAdapter) && reader.peek() == JsonToken.BEGIN_OBJECT
            && gson.getTypeInfoPolicy().isEnabled()) {
        // simple type adapters do not use '{', so this is definitely a type advice. Use a wrapper adapter
        // to correctly process it
        typeAdapter = new TypeAdviceReadingSimpleAdapterWrapper<T>(gson, typeAdapter);
    }

    return rctx.doRead(reader, typeAdapter, pathElement, this);
}

From source file:com.github.gv2011.hprops.Converter.java

License:Open Source License

private void dispatchTop(final JsonReader json, final Consumer<Property> target, final JsonToken t,
        final String key) {
    if (t.equals(JsonToken.BEGIN_OBJECT))
        doObject(json, target, key);//from   w  w w. ja va2  s .co m
    else if (t.equals(JsonToken.BEGIN_ARRAY))
        doList(json, target, key);
    else
        throw new IllegalArgumentException(t.toString());
}

From source file:com.github.gv2011.hprops.Converter.java

License:Open Source License

private void dispatch(final JsonReader json, final Consumer<Property> target, final JsonToken t,
        final String key) {
    if (t.equals(JsonToken.BOOLEAN))
        write(target, key, getBoolean(json));
    else if (t.equals(JsonToken.STRING))
        write(target, key, getString(json));
    else if (t.equals(JsonToken.NUMBER))
        write(target, key, getNumber(json));
    else if (t.equals(JsonToken.BEGIN_OBJECT))
        doObject(json, target, key);/*from  w ww  .  j av  a  2 s  . co m*/
    else if (t.equals(JsonToken.BEGIN_ARRAY))
        doList(json, target, key);
    else if (t.equals(JsonToken.NULL))
        getNull(json);
    else
        throw new IllegalArgumentException(t.toString());
}

From source file:com.goide.dlv.JsonReaderEx.java

License:Apache License

/**
 * Returns the type of the next token without consuming it.
 */// w  w w.ja v  a  2s. c  o  m
@NotNull
public JsonToken peek() {
    int p = peeked;
    if (p == PEEKED_NONE) {
        p = doPeek();
    }

    switch (p) {
    case PEEKED_BEGIN_OBJECT:
        return JsonToken.BEGIN_OBJECT;
    case PEEKED_END_OBJECT:
        return JsonToken.END_OBJECT;
    case PEEKED_BEGIN_ARRAY:
        return JsonToken.BEGIN_ARRAY;
    case PEEKED_END_ARRAY:
        return JsonToken.END_ARRAY;
    case PEEKED_SINGLE_QUOTED_NAME:
    case PEEKED_DOUBLE_QUOTED_NAME:
    case PEEKED_UNQUOTED_NAME:
        return JsonToken.NAME;
    case PEEKED_TRUE:
    case PEEKED_FALSE:
        return JsonToken.BOOLEAN;
    case PEEKED_NULL:
        return JsonToken.NULL;
    case PEEKED_SINGLE_QUOTED:
    case PEEKED_DOUBLE_QUOTED:
    case PEEKED_UNQUOTED:
    case PEEKED_BUFFERED:
        return JsonToken.STRING;
    case PEEKED_LONG:
    case PEEKED_NUMBER:
        return JsonToken.NUMBER;
    case PEEKED_EOF:
        return JsonToken.END_DOCUMENT;
    default:
        throw new AssertionError();
    }
}

From source file:com.greensopinion.finance.services.persistence.CategoriesTypeAdapter.java

License:Apache License

@Override
public Categories read(JsonReader reader) throws IOException {
    reader.beginObject();/*  ww w.j a  va  2s .c om*/
    checkState(NAME_CATEGORIES.equals(reader.nextName()));
    reader.beginArray();

    ImmutableList.Builder<Category> elements = ImmutableList.<Category>builder();
    while (reader.hasNext()) {
        if (reader.peek() == JsonToken.BEGIN_OBJECT) {
            elements.add(gson.getAdapter(Category.class).read(reader));
        } else {
            elements.add(readCategory(reader.nextString()));
        }
    }
    reader.endArray();
    reader.endObject();

    return new Categories(elements.build());
}

From source file:com.gs.reladomo.serial.gson.GsonWrappedListTypedAdapter.java

License:Apache License

@Override
public SerializedList<U, T> read(JsonReader jsonReader) throws IOException {
    ReladomoDeserializer deserializer;/*from   w w  w. ja va  2s .  c o  m*/
    if (this.typeClass == null) {
        deserializer = new ReladomoDeserializer();
    } else {
        deserializer = new ReladomoDeserializer(typeClass);
    }
    deserializer.setIgnoreUnknown();
    JsonDeserializerState state = JsonDeserializerState.ListStartState.INSTANCE;
    while (true) {
        JsonToken nextToken = jsonReader.peek();
        //BEGIN_ARRAY, END_ARRAY, BEGIN_OBJECT, END_OBJECT, NAME, STRING, NUMBER, BOOLEAN, NULL, END_DOCUMENT;
        if (JsonToken.BEGIN_OBJECT == nextToken) {
            jsonReader.beginObject();
            state = state.startObject(deserializer);
        } else if (JsonToken.END_OBJECT == nextToken) {
            jsonReader.endObject();
            state = state.endObject(deserializer);
        } else if (JsonToken.BEGIN_ARRAY == nextToken) {
            jsonReader.beginArray();
            state = state.startArray(deserializer);
        } else if (JsonToken.END_ARRAY == nextToken) {
            jsonReader.endArray();
            state = state.endArray(deserializer);
        } else if (JsonToken.BOOLEAN == nextToken) {
            if (jsonReader.nextBoolean()) {
                state = state.valueTrue(deserializer);
            } else {
                state = state.valueFalse(deserializer);
            }
        } else if (JsonToken.NAME == nextToken) {
            String name = jsonReader.nextName();
            state = state.fieldName(name, deserializer);
        } else if (JsonToken.NUMBER == nextToken) {
            String value = jsonReader.nextString();
            state = state.valueString(value, deserializer); // we do the parsing to avoid precision loss
        } else if (JsonToken.STRING == nextToken) {
            String value = jsonReader.nextString();
            Attribute attribute = deserializer.getCurrentAttribute();
            if (attribute instanceof TimestampAttribute) {
                Timestamp timestamp = GsonReladomoTypeAdapterSerialWriter.jsonToTimestamp(value);
                state = state.valueTimestamp(timestamp, deserializer);
            } else {
                state = state.valueString(value, deserializer);
            }
        } else if (JsonToken.NULL == nextToken) {
            jsonReader.nextNull();
            state = state.valueNull(deserializer);
        } else if (JsonToken.END_DOCUMENT == nextToken) {
            break;
        }
    }
    return deserializer.getDeserializedResultAsList();
}

From source file:com.gs.reladomo.serial.gson.GsonWrappedTypedAdapter.java

License:Apache License

@Override
public Serialized<T> read(JsonReader jsonReader) throws IOException {
    ReladomoDeserializer deserializer;//from w w  w  .  ja v  a2s.  co  m
    if (this.typeClass == null) {
        deserializer = new ReladomoDeserializer();
    } else {
        deserializer = new ReladomoDeserializer(typeClass);
    }
    deserializer.setIgnoreUnknown();
    JsonDeserializerState state = JsonDeserializerState.NormalParserState.INSTANCE;
    while (true) {
        JsonToken nextToken = jsonReader.peek();
        //BEGIN_ARRAY, END_ARRAY, BEGIN_OBJECT, END_OBJECT, NAME, STRING, NUMBER, BOOLEAN, NULL, END_DOCUMENT;
        if (JsonToken.BEGIN_OBJECT == nextToken) {
            jsonReader.beginObject();
            state = state.startObject(deserializer);
        } else if (JsonToken.END_OBJECT == nextToken) {
            jsonReader.endObject();
            state = state.endObject(deserializer);
        } else if (JsonToken.BEGIN_ARRAY == nextToken) {
            jsonReader.beginArray();
            state = state.startArray(deserializer);
        } else if (JsonToken.END_ARRAY == nextToken) {
            jsonReader.endArray();
            state = state.endArray(deserializer);
        } else if (JsonToken.BOOLEAN == nextToken) {
            if (jsonReader.nextBoolean()) {
                state = state.valueTrue(deserializer);
            } else {
                state = state.valueFalse(deserializer);
            }
        } else if (JsonToken.NAME == nextToken) {
            String name = jsonReader.nextName();
            state = state.fieldName(name, deserializer);
        } else if (JsonToken.NUMBER == nextToken) {
            String value = jsonReader.nextString();
            state = state.valueString(value, deserializer); // we do the parsing to avoid precision loss
        } else if (JsonToken.STRING == nextToken) {
            String value = jsonReader.nextString();
            Attribute attribute = deserializer.getCurrentAttribute();
            if (attribute instanceof TimestampAttribute) {
                Timestamp timestamp = GsonReladomoTypeAdapterSerialWriter.jsonToTimestamp(value);
                state = state.valueTimestamp(timestamp, deserializer);
            } else {
                state = state.valueString(value, deserializer);
            }
        } else if (JsonToken.NULL == nextToken) {
            jsonReader.nextNull();
            state = state.valueNull(deserializer);
        } else if (JsonToken.END_DOCUMENT == nextToken) {
            break;
        }
    }
    return deserializer.getDeserializedResult();
}

From source file:com.ibm.g11n.pipeline.resfilter.impl.JsonResource.java

License:Open Source License

static List<KeyPiece> splitKeyPieces(String key) {
    if (USE_JSONPATH_PATTERN.matcher(key).matches()) {
        List<KeyPiece> result = new ArrayList<KeyPiece>();
        boolean inQuotes = false;
        StringBuilder currentToken = new StringBuilder();
        // Disregard $ at the beginning - it's not really part of the key...
        StringCharacterIterator i = new StringCharacterIterator(key.substring(JSONPATH_ROOT.length()));
        boolean inSubscript = false;
        while (i.current() != StringCharacterIterator.DONE) {
            char c = i.current();
            if (c == '\'') {
                inQuotes = !inQuotes;//from w ww.  j  a  v  a 2 s.  co  m
            }
            if (!inQuotes && (c == '.' || c == '[' || c == ']')) {
                if (currentToken.length() > 0) {
                    addToken(result, currentToken.toString(), inSubscript);
                    currentToken.setLength(0);
                    if (inSubscript) {
                        inSubscript = false;
                    }
                }
                if (c == '[') {
                    inSubscript = true; // Record that the next token had an
                    // array subscript on it.
                }
            } else {
                currentToken.append(c);
            }
            i.next();
        }
        addToken(result, currentToken.toString(), inSubscript);

        return Collections.unmodifiableList(result);
    }
    // Otherwise, this is a plain JSON object label
    return Collections.singletonList(new KeyPiece(key, JsonToken.BEGIN_OBJECT));
}

From source file:com.ibm.g11n.pipeline.resfilter.impl.JsonResource.java

License:Open Source License

static void addToken(List<KeyPiece> result, String s, boolean inSubscript) {
    if (s.startsWith("'")) {
        // Turn any "\u0027" in the key back into '
        String modifiedKeyPiece = s.substring(1, s.length() - 1).replaceAll("\\\\u0027", "'");
        result.add(new KeyPiece(modifiedKeyPiece, JsonToken.BEGIN_OBJECT));
    } else if (inSubscript) {
        // [0] produces an array
        result.add(new KeyPiece(s, JsonToken.BEGIN_ARRAY));
    } else {//from  ww w  .  j a  v  a2  s . co m
        for (String s2 : s.split("\\.")) {
            if (!s2.isEmpty()) {
                result.add(new KeyPiece(s2, JsonToken.BEGIN_OBJECT));
            }
        }
    }
}