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

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

Introduction

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

Prototype

JsonToken START_OBJECT

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

Click Source Link

Document

START_OBJECT is returned when encountering '{' which signals starting of an Object value.

Usage

From source file:org.onosproject.north.aaa.api.parser.impl.ClientParser.java

@Override
public Set<ClientCredential> parseJson(InputStream stream) throws IOException, ParseException {
    ObjectMapper mapper = new ObjectMapper();
    JsonNode jsonNode = mapper.readTree(stream);
    JsonParser jp = jsonNode.traverse();
    Set<ClientCredential> clientSet = new HashSet<>();

    // continue parsing the token till the end of input is reached
    while (!jp.isClosed()) {
        // get the token
        JsonToken token = jp.nextToken();
        // if its the last token then we are done
        if (token == null) {
            break;
        }/*ww w  . j  a  va 2 s. c  o m*/

        if (JsonToken.FIELD_NAME.equals(token) && "clients".equals(jp.getCurrentName())) {
            token = jp.nextToken();
            if (!JsonToken.START_ARRAY.equals(token)) {
                // bail out
                throw new ParseException("expected ARRAY after clients");
            }

            while (true) {
                token = jp.nextToken();
                if (JsonToken.END_ARRAY.equals(token)) {
                    // bail out
                    break;
                }

                if (JsonToken.START_OBJECT.equals(token)) {
                    ClientCredential client = jsonToClientCredential(jp);
                    clientSet.add(client);
                }
            }
        }
    }
    return clientSet;
}

From source file:org.onosproject.north.aaa.api.parser.impl.UserParser.java

@Override
public Set<User> parseJson(InputStream stream) throws IOException, ParseException {
    // begin parsing JSON to Application class
    ObjectMapper mapper = new ObjectMapper();
    JsonNode jsonNode = mapper.readTree(stream);
    JsonParser jp = jsonNode.traverse();
    Set<User> userSet = new HashSet<>();

    // continue parsing the token till the end of input is reached
    while (!jp.isClosed()) {
        // get the token
        JsonToken token = jp.nextToken();
        // if its the last token then we are done
        if (token == null) {
            break;
        }//w w  w  .  j a v  a2  s  .  co m
        if (JsonToken.FIELD_NAME.equals(token) && "users".equals(jp.getCurrentName())) {
            token = jp.nextToken();
            if (!JsonToken.START_ARRAY.equals(token)) {
                // bail out
                throw new ParseException("expected ARRAY after users");
            }

            while (true) {
                token = jp.nextToken();
                if (JsonToken.END_ARRAY.equals(token)) {
                    // bail out
                    break;
                }
                if (JsonToken.START_OBJECT.equals(token)) {
                    User user = jsonToUser(jp);
                    userSet.add(user);
                }
            }
        }
    }
    return userSet;
}

From source file:com.michaelwitbrock.jacksonstream.JsonArrayStreamDataSupplier.java

@Override
public boolean hasNext() {
    if (!maybeHasNext) {
        return false; // didn't get started
    }/*from  w ww  .jav a  2 s .co m*/
    try {
        return (parser.nextToken() == JsonToken.START_OBJECT);
    } catch (Exception e) {
        System.out.println("Ex" + e);
        return false;
    }
}

From source file:com.addthis.codec.jackson.CodecBeanDeserializer.java

@Override
public Object deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    JsonLocation currentLocation = jp.getTokenLocation();
    JsonToken t = jp.getCurrentToken();/*from ww w.j  a v  a2s  .c o m*/
    try {
        if (t == JsonToken.START_OBJECT) {
            ObjectNode objectNode = jp.readValueAsTree();
            handleDefaultsAndRequiredAndNull(ctxt, objectNode);
            jp = jp.getCodec().treeAsTokens(objectNode);
            jp.nextToken();
        } else if (t == JsonToken.END_OBJECT) {
            // for some reason this is how they chose to handle single field objects
            jp.nextToken();
            ObjectNode objectNode = ctxt.getNodeFactory().objectNode();
            handleDefaultsAndRequiredAndNull(ctxt, objectNode);
            jp = jp.getCodec().treeAsTokens(objectNode);
            jp.nextToken();
        }
        Object value = getDelegatee().deserialize(jp, ctxt);
        if (value instanceof SuperCodable) {
            ((SuperCodable) value).postDecode();
        }
        return value;
    } catch (JsonMappingException ex) {
        throw Jackson.maybeImproveLocation(currentLocation, ex);
    }
}

From source file:com.cedarsoft.serialization.jackson.JacksonParserWrapper.java

public void startObject() throws IOException, JsonParseException {
    nextToken(JsonToken.START_OBJECT);
}

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;/* w w w. j av  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());
    }
}

From source file:com.basistech.rosette.dm.jackson.array.ListAttributeArrayDeserializer.java

@Override
@SuppressWarnings("unchecked")
public ListAttribute deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    if (jp.getCurrentToken() == JsonToken.START_ARRAY) { // this is what we expect.
        // we advance to be in the same place the 'else' will be -- the first FIELD_NAME.
        jp.nextToken();//from   w  w w.  j a va  2  s.  co m
    } else {
        throw ctxt.wrongTokenException(jp, JsonToken.START_ARRAY,
                "ListAttributeDeserializer called not array start.");
    }

    if (jp.getCurrentToken() != JsonToken.VALUE_STRING) {
        throw ctxt.mappingException("Expected VALUE_STRING for item type.");
    }
    String itemTypeKeyName = jp.getText();

    KnownAttribute attribute = KnownAttribute.getAttributeForKey(itemTypeKeyName);
    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();

    if (jp.nextToken() != JsonToken.START_ARRAY) {
        throw ctxt.wrongTokenException(jp, JsonToken.START_ARRAY, "No array of values for list.");
    }

    // we just read the elements as we see them,
    while (jp.nextToken() != JsonToken.END_ARRAY) {
        // the START_ARRAY case, which is _normal_. Read the elements.
        items.add(jp.readValueAs(itemClass));
    }
    builder.setItems(items);
    // we are still in the top-level array ...
    if (jp.nextToken() != JsonToken.START_OBJECT) {
        throw ctxt.wrongTokenException(jp, JsonToken.START_OBJECT, "No extended properties for list.");
    }
    Map<String, Object> props = jp.readValueAs(new TypeReference<Map<String, Object>>() {
    });
    for (Map.Entry<String, Object> me : props.entrySet()) {
        builder.extendedProperty(me.getKey(), me.getValue());
    }
    jp.nextToken(); // consume the END_OBJECT of the extended props
    return builder.build();
}

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

public boolean handleUnknownProperty(DeserializationContext ctxt, JsonDeserializer<?> deserializer,
        Object beanOrClass, String propertyName) throws IOException {
    if (propertyName.startsWith("$") || propertyName.equals("code")) {
        // It's a special server response
        JsonToken token = getCurrentToken();
        if (token == JsonToken.FIELD_NAME) {
            token = nextToken();/*from w w w  .j  a  v  a 2  s. co  m*/
        }
        if (token == JsonToken.START_ARRAY || token == JsonToken.START_OBJECT) {
            // The server shouldn't be returning arrays or objects as the
            // response, skip all children
            skipChildren();
        }
        // Store the value, whatever type it happens to be
        dbObject.put(propertyName, getEmbeddedObject());
        return true;
    }
    return false;
}

From source file:com.addthis.codec.config.ConfigNodeCursor.java

static JsonToken forConfigValue(ConfigValue configValue) {
    ConfigValueType valueType = configValue.valueType();
    switch (valueType) {
    case NUMBER://from ww  w  . ja  va2  s  . c  om
        if (configValue.unwrapped() instanceof Double) {
            return JsonToken.VALUE_NUMBER_FLOAT;
        } else {
            return JsonToken.VALUE_NUMBER_INT;
        }
    case BOOLEAN:
        if (configValue.unwrapped().equals(Boolean.TRUE)) {
            return JsonToken.VALUE_TRUE;
        } else {
            return JsonToken.VALUE_FALSE;
        }
    case NULL:
        return JsonToken.VALUE_NULL;
    case STRING:
        return JsonToken.VALUE_STRING;
    case OBJECT:
        return JsonToken.START_OBJECT;
    case LIST:
        return JsonToken.START_ARRAY;
    default:
        // not possible unless the set of enums changes on us later
        throw new IllegalArgumentException(valueType.name() + " is not a supported ConfigValueType");
    }
}

From source file:org.emfjson.jackson.databind.deser.ResourceDeserializer.java

@Override
public Resource deserialize(JsonParser jp, DeserializationContext ctxt, Resource intoValue) throws IOException {
    final Resource resource = getResource(ctxt, intoValue);
    if (resource == null) {
        throw new IllegalArgumentException("Invalid resource");
    }/*  ww  w  . j  a v a2 s .c  om*/

    final ReferenceEntries entries = new ReferenceEntries();
    final EcoreTypeFactory ecoreType = new EcoreTypeFactory();
    final ResourceSet resourceSet = resource.getResourceSet();

    ctxt.setAttribute(RESOURCE, resource);
    ctxt.setAttribute(REFERENCE_ENTRIES, entries);
    ctxt.setAttribute(CACHE, new Cache());
    ctxt.setAttribute(TYPE_FACTORY, ecoreType);
    ctxt.setAttribute(RESOURCE_SET, resourceSet);

    if (!jp.hasCurrentToken()) {
        jp.nextToken();
    }

    final TypeFactory factory = TypeFactory.defaultInstance();
    final JsonDeserializer<Object> deserializer = ctxt
            .findRootValueDeserializer(factory.constructType(EObject.class));

    if (jp.getCurrentToken() == JsonToken.START_ARRAY) {

        while (jp.nextToken() != JsonToken.END_ARRAY) {

            EObject value = (EObject) deserializer.deserialize(jp, ctxt);
            if (value != null) {
                resource.getContents().add(value);
            }
        }

    } else if (jp.getCurrentToken() == JsonToken.START_OBJECT) {
        EObject value = (EObject) deserializer.deserialize(jp, ctxt);
        if (value != null) {
            resource.getContents().add(value);
        }
    }

    entries.resolve(resourceSet, uriHandler);

    return resource;
}