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:de.undercouch.bson4jackson.deserializers.BsonDateDeserializer.java

@Override
public Date deserialize(BsonParser bsonParser, DeserializationContext ctxt) throws IOException {
    if (bsonParser.getCurrentToken() != JsonToken.VALUE_EMBEDDED_OBJECT
            || bsonParser.getCurrentBsonType() != BsonConstants.TYPE_DATETIME) {
        ctxt.mappingException(Date.class);
    }/*from   w  w w  . j av a 2 s  .c o m*/

    Object obj = bsonParser.getEmbeddedObject();
    if (obj == null) {
        return null;
    }

    return (Date) obj;
}

From source file:de.undercouch.bson4jackson.deserializers.BsonCalendarDeserializer.java

@Override
public Calendar deserialize(BsonParser bsonParser, DeserializationContext ctxt) throws IOException {
    if (bsonParser.getCurrentToken() != JsonToken.VALUE_EMBEDDED_OBJECT
            || bsonParser.getCurrentBsonType() != BsonConstants.TYPE_DATETIME) {
        ctxt.mappingException(Date.class);
    }//from w w  w.  jav  a 2s .com

    Object obj = bsonParser.getEmbeddedObject();
    if (obj == null) {
        return null;
    }

    Calendar cal = Calendar.getInstance();
    cal.setTime((Date) obj);
    return cal;
}

From source file:fr.javatic.mongo.jacksonCodec.javaTime.deserializers.InstantDeserializer.java

@Override
public Instant deserialize(BsonParser bsonParser, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    if (bsonParser.getCurrentToken() != JsonToken.VALUE_EMBEDDED_OBJECT
            || bsonParser.getCurrentBsonType() != BsonConstants.TYPE_DATETIME) {
        ctxt.mappingException(Date.class);
    }/*from   w  ww . j a  v  a2  s  .  c  o  m*/

    Object obj = bsonParser.getEmbeddedObject();
    if (obj == null) {
        return null;
    }

    Date dt = (Date) obj;
    return Instant.ofEpochMilli(dt.getTime());
}

From source file:org.mongojack.internal.DateDeserializer.java

@Override
public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    JsonToken token = jp.getCurrentToken();
    if (token == JsonToken.VALUE_EMBEDDED_OBJECT) {
        // See if it's a date
        Object date = jp.getEmbeddedObject();
        if (date instanceof Date) {
            return (Date) date;
        } else {//from   w w  w . j  a  va2 s  .co m
            throw ctxt.mappingException(Date.class);
        }
    } else {
        return _parseDate(jp, ctxt);
    }
}

From source file:fr.javatic.mongo.jacksonCodec.javaTime.deserializers.ZonedDateTimeDeserializer.java

@Override
public ZonedDateTime deserialize(BsonParser bsonParser, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    if (bsonParser.getCurrentToken() != JsonToken.VALUE_EMBEDDED_OBJECT
            || bsonParser.getCurrentBsonType() != BsonConstants.TYPE_DATETIME) {
        throw ctxt.mappingException(Date.class);
    }//  ww w  .j  av  a2 s . c  om

    Object obj = bsonParser.getEmbeddedObject();
    if (obj == null) {
        return null;
    }

    Date dt = (Date) obj;
    return Instant.ofEpochMilli(dt.getTime()).atZone(ZoneId.of("UTC"));
}

From source file:fr.javatic.mongo.jacksonCodec.javaTime.deserializers.OffsetDateTimeDeserializer.java

@Override
public OffsetDateTime deserialize(BsonParser bsonParser, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    if (bsonParser.getCurrentToken() != JsonToken.VALUE_EMBEDDED_OBJECT
            || bsonParser.getCurrentBsonType() != BsonConstants.TYPE_DATETIME) {
        throw ctxt.mappingException(Date.class);
    }/*from w  w w.  ja v  a  2 s.  c  o m*/

    Object obj = bsonParser.getEmbeddedObject();
    if (obj == null) {
        return null;
    }

    Date dt = (Date) obj;
    return Instant.ofEpochMilli(dt.getTime()).atOffset(ZoneOffset.UTC);
}

From source file:org.mongojack.internal.CalendarDeserializer.java

@Override
public Calendar deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    JsonToken token = jp.getCurrentToken();
    Date date;/*from   ww  w .j  a  v  a2 s.c om*/
    if (token == JsonToken.VALUE_EMBEDDED_OBJECT) {
        // See if it's a date
        Object object = jp.getEmbeddedObject();
        if (object instanceof Date) {
            date = (Date) object;
        } else {
            throw ctxt.mappingException(Calendar.class);
        }
    } else {
        date = _parseDate(jp, ctxt);
    }
    if (date == null) {
        return null;
    }
    return ctxt.constructCalendar(date);
}

From source file:fr.javatic.mongo.jacksonCodec.objectId.IdDeserializer.java

public String deserialize(BsonParser bsonParser, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    if (bsonParser.getCurrentToken() != JsonToken.VALUE_EMBEDDED_OBJECT
            || bsonParser.getCurrentBsonType() != BsonConstants.TYPE_OBJECTID) {
        throw ctxt.mappingException(ObjectId.class);
    }/*from w w  w  .  j  ava 2s  .  co m*/

    ObjectId parsedObjectId = (ObjectId) bsonParser.getEmbeddedObject();
    int timestamp = parsedObjectId.getTime();
    int machineAndProcessIdentifier = parsedObjectId.getMachine();
    int counter = parsedObjectId.getInc();

    byte[] bytes = new byte[12];
    bytes[0] = int3(timestamp);
    bytes[1] = int2(timestamp);
    bytes[2] = int1(timestamp);
    bytes[3] = int0(timestamp);
    bytes[4] = int3(machineAndProcessIdentifier);
    bytes[5] = int2(machineAndProcessIdentifier);
    bytes[6] = int1(machineAndProcessIdentifier);
    bytes[7] = int0(machineAndProcessIdentifier);
    bytes[8] = int3(counter);
    bytes[9] = int2(counter);
    bytes[10] = int1(counter);
    bytes[11] = int0(counter);

    StringBuilder buf = new StringBuilder(24);

    for (final byte b : bytes) {
        buf.append(String.format("%02x", b & 0xff));
    }

    return buf.toString();
}

From source file:org.mongojack.internal.stream.DBDecoderBsonParser.java

@Override
public String getText() throws IOException, JsonParseException {
    if (JsonToken.VALUE_EMBEDDED_OBJECT == getCurrentToken()) {
        return null;
    }//ww  w  .j a v a 2s .  co  m
    return super.getText();
}

From source file:org.mongojack.internal.DBRefDeserializer.java

@Override
public DBRef deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    // First of all, make sure that we can get a copy of the DBCollection
    if (jp instanceof JacksonDBCollectionProvider) {
        K id = null;/*from  w  w w .ja  v a  2s. co m*/
        String collectionName = null;
        JsonToken token = jp.getCurrentToken();
        if (token == JsonToken.VALUE_NULL) {
            return null;
        }
        if (token == JsonToken.VALUE_EMBEDDED_OBJECT) {
            // Someones already kindly decoded it for us
            Object object = jp.getEmbeddedObject();
            if (object instanceof com.mongodb.DBRef) {
                if (keyDeserializer != null) {
                    id = keyDeserializer.deserialize(jp, ctxt);
                } else {
                    id = (K) ((com.mongodb.DBRef) object).getId();
                }
                collectionName = ((com.mongodb.DBRef) object).getRef();
            } else {
                throw ctxt.instantiationException(DBRef.class,
                        "Don't know what to do with embedded object: " + object);
            }
        } else if (token == JsonToken.START_OBJECT) {
            token = jp.nextValue();
            while (token != JsonToken.END_OBJECT) {
                if (jp.getCurrentName().equals("$id")) {
                    if (keyDeserializer != null) {
                        id = keyDeserializer.deserialize(jp, ctxt);
                    } else {
                        id = (K) jp.getEmbeddedObject();
                    }
                } else if (jp.getCurrentName().equals("$ref")) {
                    collectionName = jp.getText();
                } else {
                    // Ignore the rest
                }
                token = jp.nextValue();
            }
        }
        if (id == null) {
            return null;
        }
        if (collectionName == null) {
            throw ctxt.instantiationException(DBRef.class, "DBRef contains no collection name");
        }

        JacksonDBCollection coll = ((JacksonDBCollectionProvider) jp).getDBCollection();
        JacksonDBCollection<T, K> refColl = coll.getReferenceCollection(collectionName, type, keyType);
        return new FetchableDBRef<T, K>(id, refColl);
    } else {
        throw ctxt.instantiationException(DBRef.class,
                "DBRef can only be deserialised by this deserializer if parser implements "
                        + JacksonDBCollectionProvider.class.getName() + " parser is actually "
                        + jp.getClass().getName());
    }
}