Example usage for com.fasterxml.jackson.core JsonToken VALUE_EMBEDDED_OBJECT

List of usage examples for com.fasterxml.jackson.core JsonToken VALUE_EMBEDDED_OBJECT

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonToken VALUE_EMBEDDED_OBJECT.

Prototype

JsonToken VALUE_EMBEDDED_OBJECT

To view the source code for com.fasterxml.jackson.core JsonToken VALUE_EMBEDDED_OBJECT.

Click Source Link

Document

Placeholder token returned when the input source has a concept of embedded Object that are not accessible as usual structure (of starting with #START_OBJECT , having values, ending with #END_OBJECT ), but as "raw" objects.

Usage

From source file:org.mongojack.internal.util.CalendarDeserializerTest.java

@Test
public void testWithDateObject() throws IOException {
    when(jsonParser.getCurrentToken()).thenReturn(JsonToken.VALUE_EMBEDDED_OBJECT);
    when(jsonParser.getEmbeddedObject()).thenReturn(date);

    deserializedDate = deserializer.deserialize(jsonParser, deserializationContext);

    assertEquals(calendar, deserializedDate);
}

From source file:org.mongojack.internal.util.CalendarDeserializerTest.java

@Test(expected = JsonMappingException.class)
public void testWithoutDateObject() throws IOException {
    when(jsonParser.getCurrentToken()).thenReturn(JsonToken.VALUE_EMBEDDED_OBJECT);
    when(jsonParser.getEmbeddedObject()).thenReturn(new Object());

    deserializedDate = deserializer.deserialize(jsonParser, deserializationContext);
}

From source file:org.ojai.beans.jackson.DocumentParser.java

@Override
public JsonToken nextToken() throws IOException, JsonParseException {
    if (tokens.isEmpty() && (_currEventType = r.next()) != null) {
        if (r.inMap() && r.getFieldName() != null && _currEventType != EventType.END_MAP
                && _currEventType != EventType.END_ARRAY) {
            tokens.add(JsonToken.FIELD_NAME);
        }/*w  w  w  . ja va2s . c o m*/

        switch (_currEventType) {
        case START_ARRAY:
            tokens.add(JsonToken.START_ARRAY);
            break;
        case END_ARRAY:
            tokens.add(JsonToken.END_ARRAY);
            break;
        case START_MAP:
            tokens.add(JsonToken.START_OBJECT);
            break;
        case END_MAP:
            tokens.add(JsonToken.END_OBJECT);
            break;
        case NULL:
            tokens.add(JsonToken.VALUE_NULL);
            break;
        case STRING:
            tokens.add(JsonToken.VALUE_STRING);
            break;
        case BYTE:
        case SHORT:
        case INT:
        case LONG:
            tokens.add(JsonToken.VALUE_NUMBER_INT);
            break;
        case DECIMAL:
        case DOUBLE:
        case FLOAT:
            tokens.add(JsonToken.VALUE_NUMBER_FLOAT);
            break;
        case BOOLEAN:
            tokens.add(r.getBoolean() ? JsonToken.VALUE_TRUE : JsonToken.VALUE_FALSE);
            break;
        case DATE:
        case TIME:
        case TIMESTAMP:
        case INTERVAL:
        case BINARY:
            tokens.add(JsonToken.VALUE_EMBEDDED_OBJECT);
            break;
        }
    }
    _currToken = tokens.isEmpty() ? JsonToken.NOT_AVAILABLE : tokens.remove();
    return _currToken;
}

From source file:com.basistech.rosette.dm.jackson.ListAttributeDeserializer.java

@SuppressWarnings("unchecked")
private ListAttribute deserialize(JsonParser jp, DeserializationContext ctxt, TokenBuffer tb)
        throws IOException {
    jp.nextToken();//from   w  w w  .  j  a v a 2 s  .c om
    String keyName = jp.getText();

    if (tb != null) { // need to put back skipped properties?
        jp = JsonParserSequence.createFlattened(tb.asParser(jp), jp);
    }
    // Must point to the next value; tb had no current, jp pointed to VALUE_STRING:

    KnownAttribute attribute = KnownAttribute.getAttributeForKey(keyName);
    if (attribute == null) {
        attribute = KnownAttribute.UNKNOWN;
    }
    Class<? extends BaseAttribute> itemClass = attribute.attributeClass();

    ListAttribute.Builder<BaseAttribute> builder = new ListAttribute.Builder<>(attribute.attributeClass());
    List<BaseAttribute> items = Lists.newArrayList();

    JsonToken nextToken;
    while ((nextToken = jp.nextToken()) != JsonToken.END_OBJECT) {
        if (nextToken != JsonToken.FIELD_NAME) {
            throw ctxt.wrongTokenException(jp, JsonToken.END_OBJECT, "Expected field name.");
        } else {
            String name = jp.getCurrentName();
            if ("items".equals(name)) {
                // the actual list items.
                nextToken = jp.nextToken();
                if (nextToken == JsonToken.VALUE_EMBEDDED_OBJECT) {
                    Object o = jp.getEmbeddedObject();
                    if (o instanceof List) { // could it be an array, also?!?
                        // when using JsonTree, sometimes Jackson just sticks the entire Java object in here.
                        items.addAll((List) o);
                    } else {
                        throw ctxt.mappingException(
                                "List contains VALUE_EMBEDDED_OBJECT for items, but it wasn't a list.");
                    }
                } else if (nextToken != JsonToken.START_ARRAY) { // what about nothing?
                    throw ctxt.wrongTokenException(jp, JsonToken.START_ARRAY, "Expected array of items");
                } else {
                    // the START_ARRAY case, which is _normal_. Read the elements.
                    while (jp.nextToken() != JsonToken.END_ARRAY) {
                        items.add(jp.readValueAs(itemClass));
                    }
                }
            } else {
                nextToken = jp.nextToken();
                Object value;
                if (nextToken == JsonToken.VALUE_EMBEDDED_OBJECT) {
                    value = jp.getEmbeddedObject();
                } else {
                    value = jp.readValueAs(Object.class);
                }
                builder.extendedProperty(name, value);
            }
        }
    }
    builder.setItems(items);
    return builder.build();
}

From source file:com.netflix.spectator.tdigest.Json.java

private TDigestMeasurement decode(JsonParser parser) throws IOException {
    expect(parser, JsonToken.START_OBJECT);
    require("name".equals(parser.nextFieldName()), "expected name");
    Id id = registry.createId(parser.nextTextValue());
    while (parser.nextToken() == JsonToken.FIELD_NAME) {
        id = id.withTag(parser.getText(), parser.nextTextValue());
    }//  w w w. j  av  a  2  s .c  o m
    long t = parser.nextLongValue(-1L);
    expect(parser, JsonToken.VALUE_EMBEDDED_OBJECT);
    TDigest v = AVLTreeDigest.fromBytes(ByteBuffer.wrap(parser.getBinaryValue()));
    expect(parser, JsonToken.END_ARRAY);
    return new TDigestMeasurement(id, t, v);
}

From source file:org.mongojack.internal.object.BsonObjectCursor.java

private static JsonToken getToken(Object o) {
    if (o == null) {
        return JsonToken.VALUE_NULL;
    } else if (o instanceof Iterable) {
        return JsonToken.START_ARRAY;
    } else if (o instanceof BSONObject) {
        return JsonToken.START_OBJECT;
    } else if (o instanceof Number) {
        if (o instanceof Double || o instanceof Float || o instanceof BigDecimal) {
            return JsonToken.VALUE_NUMBER_FLOAT;
        } else {/* w  ww . j  av a 2s . co  m*/
            return JsonToken.VALUE_NUMBER_INT;
        }
    } else if (o instanceof Boolean) {
        if ((Boolean) o) {
            return JsonToken.VALUE_TRUE;
        } else {
            return JsonToken.VALUE_FALSE;
        }
    } else if (o instanceof CharSequence) {
        return JsonToken.VALUE_STRING;
    } else if (o instanceof ObjectId) {
        return JsonToken.VALUE_EMBEDDED_OBJECT;
    } else if (o instanceof DBRef) {
        return JsonToken.VALUE_EMBEDDED_OBJECT;
    } else if (o instanceof Date) {
        return JsonToken.VALUE_EMBEDDED_OBJECT;
    } else if (o instanceof byte[]) {
        return JsonToken.VALUE_EMBEDDED_OBJECT;
    } else {
        throw new IllegalStateException("Don't know how to parse type: " + o.getClass());
    }
}

From source file:de.undercouch.bson4jackson.BsonParser.java

@Override
public JsonToken nextToken() throws IOException, JsonParseException {
    Context ctx = _currentContext;
    if (_currToken == null && ctx == null) {
        try {//from   www  .  j  a  va 2s  .c  o  m
            _currToken = handleNewDocument(false);
        } catch (EOFException e) {
            //there is nothing more to read. indicate EOF
            return null;
        }
    } else {
        _tokenPos = _counter.getPosition();
        if (ctx == null) {
            if (_currToken == JsonToken.END_OBJECT) {
                //end of input
                return null;
            }
            throw new JsonParseException("Found element outside the document", getTokenLocation());
        }

        if (ctx.state == State.DONE) {
            //next field
            ctx.reset();
        }

        boolean readValue = true;
        if (ctx.state == State.FIELDNAME) {
            readValue = false;
            while (true) {
                //read field name or end of document
                ctx.type = _in.readByte();
                if (ctx.type == BsonConstants.TYPE_END) {
                    //end of document
                    _currToken = (ctx.array ? JsonToken.END_ARRAY : JsonToken.END_OBJECT);
                    _currentContext = _currentContext.parent;
                } else if (ctx.type == BsonConstants.TYPE_UNDEFINED) {
                    //skip field name and then ignore this token
                    skipCString();
                    continue;
                } else {
                    ctx.state = State.VALUE;
                    _currToken = JsonToken.FIELD_NAME;

                    if (ctx.array) {
                        //immediately read value of array element (discard field name)
                        readValue = true;
                        skipCString();
                        ctx.fieldName = null;
                    } else {
                        //read field name
                        ctx.fieldName = readCString();
                    }
                }
                break;
            }
        }

        if (readValue) {
            //parse element's value
            switch (ctx.type) {
            case BsonConstants.TYPE_DOUBLE:
                ctx.value = _in.readDouble();
                _currToken = JsonToken.VALUE_NUMBER_FLOAT;
                break;

            case BsonConstants.TYPE_STRING:
                ctx.value = readString();
                _currToken = JsonToken.VALUE_STRING;
                break;

            case BsonConstants.TYPE_DOCUMENT:
                _currToken = handleNewDocument(false);
                break;

            case BsonConstants.TYPE_ARRAY:
                _currToken = handleNewDocument(true);
                break;

            case BsonConstants.TYPE_BINARY:
                _currToken = handleBinary();
                break;

            case BsonConstants.TYPE_OBJECTID:
                ctx.value = readObjectId();
                _currToken = JsonToken.VALUE_EMBEDDED_OBJECT;
                break;

            case BsonConstants.TYPE_BOOLEAN:
                boolean b = _in.readBoolean();
                ctx.value = b;
                _currToken = (b ? JsonToken.VALUE_TRUE : JsonToken.VALUE_FALSE);
                break;

            case BsonConstants.TYPE_DATETIME:
                ctx.value = new Date(_in.readLong());
                _currToken = JsonToken.VALUE_EMBEDDED_OBJECT;
                break;

            case BsonConstants.TYPE_NULL:
                _currToken = JsonToken.VALUE_NULL;
                break;

            case BsonConstants.TYPE_REGEX:
                _currToken = handleRegEx();
                break;

            case BsonConstants.TYPE_DBPOINTER:
                _currToken = handleDBPointer();
                break;

            case BsonConstants.TYPE_JAVASCRIPT:
                ctx.value = new JavaScript(readString());
                _currToken = JsonToken.VALUE_EMBEDDED_OBJECT;
                break;

            case BsonConstants.TYPE_SYMBOL:
                ctx.value = readSymbol();
                _currToken = JsonToken.VALUE_EMBEDDED_OBJECT;
                break;

            case BsonConstants.TYPE_JAVASCRIPT_WITH_SCOPE:
                _currToken = handleJavascriptWithScope();
                break;

            case BsonConstants.TYPE_INT32:
                ctx.value = _in.readInt();
                _currToken = JsonToken.VALUE_NUMBER_INT;
                break;

            case BsonConstants.TYPE_TIMESTAMP:
                ctx.value = readTimestamp();
                _currToken = JsonToken.VALUE_EMBEDDED_OBJECT;
                break;

            case BsonConstants.TYPE_INT64:
                ctx.value = _in.readLong();
                _currToken = JsonToken.VALUE_NUMBER_INT;
                break;

            case BsonConstants.TYPE_MINKEY:
                ctx.value = "MinKey";
                _currToken = JsonToken.VALUE_STRING;
                break;

            case BsonConstants.TYPE_MAXKEY:
                ctx.value = "MaxKey";
                _currToken = JsonToken.VALUE_STRING;
                break;

            default:
                throw new JsonParseException("Unknown element type " + ctx.type, getTokenLocation());
            }
            ctx.state = State.DONE;
        }
    }
    return _currToken;
}

From source file:com.bazaarvoice.jackson.rison.RisonParser.java

@Override
public byte[] getBinaryValue(Base64Variant b64variant) throws IOException, JsonParseException {
    if (_currToken != JsonToken.VALUE_STRING
            && (_currToken != JsonToken.VALUE_EMBEDDED_OBJECT || _binaryValue == null)) {
        _reportError("Current token (" + _currToken
                + ") not VALUE_STRING or VALUE_EMBEDDED_OBJECT, can not access as binary");
    }// w  w  w  .java 2s  .  co m
    /* To ensure that we won't see inconsistent data, better clear up
     * state...
     */
    if (_tokenIncomplete) {
        try {
            _binaryValue = _decodeBase64(b64variant);
        } catch (IllegalArgumentException iae) {
            throw _constructError(
                    "Failed to decode VALUE_STRING as base64 (" + b64variant + "): " + iae.getMessage());
        }
        /* let's clear incomplete only now; allows for accessing other
         * textual content in error cases
         */
        _tokenIncomplete = false;
    } else { // may actually require conversion...
        if (_binaryValue == null) {
            ByteArrayBuilder builder = _getByteArrayBuilder();
            _decodeBase64(getText(), builder, b64variant);
            _binaryValue = builder.toByteArray();
        }
    }
    return _binaryValue;
}

From source file:de.undercouch.bson4jackson.BsonParser.java

/**
 * Reads binary data from the input stream
 * @return the json token read/*  w  ww . j  a v  a  2s.c  o m*/
 * @throws IOException if an I/O error occurs
 */
protected JsonToken handleBinary() throws IOException {
    int size = _in.readInt();
    byte subtype = _in.readByte();
    Context ctx = getContext();
    switch (subtype) {
    case BsonConstants.SUBTYPE_BINARY_OLD:
        int size2 = _in.readInt();
        byte[] buf2 = new byte[size2];
        _in.readFully(buf2);
        ctx.value = buf2;
        break;

    case BsonConstants.SUBTYPE_UUID:
        long l1 = _in.readLong();
        long l2 = _in.readLong();
        ctx.value = new UUID(l1, l2);
        break;

    default:
        byte[] buf = new byte[size];
        _in.readFully(buf);
        ctx.value = buf;
        break;
    }

    return JsonToken.VALUE_EMBEDDED_OBJECT;
}

From source file:de.undercouch.bson4jackson.BsonParser.java

/**
 * Reads and compiles a regular expression
 * @return the json token read//from w  w w .ja va2 s  .  c  o m
 * @throws IOException if an I/O error occurs
 */
protected JsonToken handleRegEx() throws IOException {
    String regex = readCString();
    String pattern = readCString();
    getContext().value = Pattern.compile(regex, regexStrToFlags(pattern));
    return JsonToken.VALUE_EMBEDDED_OBJECT;
}