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

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

Introduction

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

Prototype

JsonToken END_OBJECT

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

Click Source Link

Document

END_OBJECT is returned when encountering '}' which signals ending of an Object value

Usage

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

@Override
public ReferenceEntry deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    EObject parent = EMFContext.getParent(ctxt);
    EReference reference = EMFContext.getReference(ctxt);

    String id = null;//  ww w  . j  a v  a  2  s .  c om
    String type = null;

    while (jp.nextToken() != JsonToken.END_OBJECT) {
        final String field = jp.getCurrentName();

        if (field.equalsIgnoreCase(info.getProperty())) {
            id = jp.nextTextValue();
        } else if (field.equalsIgnoreCase(info.getTypeProperty())) {
            type = jp.nextTextValue();
        }
    }

    return id != null ? new ReferenceEntry.Base(parent, reference, id, type) : null;
}

From source file:com.msopentech.odatajclient.engine.data.metadata.edm.AssociationDeserializer.java

@Override
public Association deserialize(final JsonParser jp, final DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

    final Association association = new Association();

    for (; jp.getCurrentToken() != JsonToken.END_OBJECT; jp.nextToken()) {
        final JsonToken token = jp.getCurrentToken();
        if (token == JsonToken.FIELD_NAME) {
            if ("Name".equals(jp.getCurrentName())) {
                association.setName(jp.nextTextValue());
            } else if ("ReferentialConstraint".equals(jp.getCurrentName())) {
                jp.nextToken();// w w  w  .j a  va  2  s  . c  o  m
                association.setReferentialConstraint(jp.getCodec().readValue(jp, ReferentialConstraint.class));
            } else if ("End".equals(jp.getCurrentName())) {
                jp.nextToken();
                association.getEnds().add(jp.getCodec().readValue(jp, AssociationEnd.class));
            }
        }
    }

    return association;
}

From source file:com.msopentech.odatajclient.engine.data.metadata.edm.DataServicesDeserializer.java

@Override
public DataServices deserialize(final JsonParser jp, final DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

    final DataServices dataServices = new DataServices();

    for (; jp.getCurrentToken() != JsonToken.END_OBJECT; jp.nextToken()) {
        final JsonToken token = jp.getCurrentToken();
        if (token == JsonToken.FIELD_NAME) {
            if ("DataServiceVersion".equals(jp.getCurrentName())) {
                dataServices.setDataServiceVersion(jp.nextTextValue());
            } else if ("MaxDataServiceVersion".equals(jp.getCurrentName())) {
                dataServices.setMaxDataServiceVersion(jp.nextTextValue());
            } else if ("Schema".equals(jp.getCurrentName())) {
                jp.nextToken();/*from   www  .  j  av  a 2  s .co m*/
                dataServices.getSchemas().add(jp.getCodec().readValue(jp, Schema.class));
            }
        }
    }

    return dataServices;
}

From source file:org.mstc.zmq.json.Decoder.java

@SuppressWarnings("unchecked")
public static void decode(String input, Field[] fields, Object b) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);
    if (logger.isDebugEnabled())
        logger.debug(input);//from w ww .  j ava2s.  c  o  m
    JsonFactory factory = mapper.getFactory();
    try (JsonParser jp = factory.createParser(input)) {
        /* Sanity check: verify that we got "Json Object" */
        if (jp.nextToken() != JsonToken.START_OBJECT) {
            throw new IOException("Expected data to start with an Object");
        }

        /* Iterate over object fields */
        while (jp.nextToken() != JsonToken.END_OBJECT) {
            String fieldName = jp.getCurrentName();
            jp.nextToken();
            Field field = getField(fieldName, fields);
            if (field == null) {
                throw new IOException(
                        "Could not find field [" + fieldName + "] on class " + b.getClass().getName());
            }
            try {
                if (field.getType().isAssignableFrom(List.class)) {
                    String adder = getAdder(fieldName);
                    TypeFactory t = TypeFactory.defaultInstance();
                    ParameterizedType listType = (ParameterizedType) field.getGenericType();
                    Class<?> listClass = (Class<?>) listType.getActualTypeArguments()[0];
                    List list = mapper.readValue(jp.getValueAsString(),
                            t.constructCollectionType(List.class, listClass));
                    Method m = b.getClass().getDeclaredMethod(adder, Collection.class);
                    m.invoke(b, list);
                } else if (field.getType().isArray()) {
                    Class<?> type = field.getType();
                    String setter = getSetter(fieldName);
                    Method m = b.getClass().getDeclaredMethod(setter, field.getType());
                    logger.info("Field {} is array of {}[], {}, using method {}", field.getName(),
                            field.getType().getComponentType(), jp.getCurrentToken().name(), m);
                    if (jp.getCurrentToken().id() == JsonToken.START_ARRAY.id()) {
                        List list = new ArrayList();
                        while (jp.nextToken() != JsonToken.END_ARRAY) {
                            String value = jp.getText();
                            switch (jp.getCurrentToken()) {
                            case VALUE_STRING:
                                list.add(value);
                                break;
                            case VALUE_NUMBER_INT:
                                if (type.getComponentType().isAssignableFrom(double.class)) {
                                    list.add(Double.parseDouble(value));
                                } else if (type.getComponentType().isAssignableFrom(float.class)) {
                                    list.add(Float.parseFloat(value));
                                } else {
                                    list.add(Integer.parseInt(value));
                                }
                                break;
                            case VALUE_NUMBER_FLOAT:
                                logger.info("Add float");
                                list.add(jp.getFloatValue());
                                break;
                            case VALUE_NULL:
                                break;
                            default:
                                logger.warn("[3] Not sure how to handle {} yet", jp.getCurrentToken().name());
                            }
                        }
                        Object array = Array.newInstance(field.getType().getComponentType(), list.size());
                        for (int i = 0; i < list.size(); i++) {
                            Object val = list.get(i);
                            Array.set(array, i, val);
                        }
                        m.invoke(b, array);
                    } else {
                        if (type.getComponentType().isAssignableFrom(byte.class)) {
                            m.invoke(b, jp.getBinaryValue());
                        }
                    }
                } else {
                    String setter = getSetter(fieldName);
                    logger.debug("{}: {}", setter, field.getType().getName());
                    Method m = b.getClass().getDeclaredMethod(setter, field.getType());

                    switch (jp.getCurrentToken()) {
                    case VALUE_STRING:
                        m.invoke(b, jp.getText());
                        break;
                    case VALUE_NUMBER_INT:
                        m.invoke(b, jp.getIntValue());
                        break;
                    case VALUE_NUMBER_FLOAT:
                        m.invoke(b, jp.getFloatValue());
                        break;
                    case VALUE_NULL:
                        logger.debug("Skip invoking {}.{}, property is null", b.getClass().getName(),
                                m.getName());
                        break;
                    case START_OBJECT:
                        StringBuilder sb = new StringBuilder();
                        while (jp.nextToken() != JsonToken.END_OBJECT) {
                            switch (jp.getCurrentToken()) {
                            case VALUE_STRING:
                                sb.append("\"").append(jp.getText()).append("\"");
                                break;
                            case FIELD_NAME:
                                if (sb.length() > 0)
                                    sb.append(",");
                                sb.append("\"").append(jp.getText()).append("\"").append(":");
                                break;
                            case VALUE_NUMBER_INT:
                                sb.append(jp.getIntValue());
                                break;
                            case VALUE_NUMBER_FLOAT:
                                sb.append(jp.getFloatValue());
                                break;
                            case VALUE_NULL:
                                sb.append("null");
                                break;
                            default:
                                logger.warn("[2] Not sure how to handle {} yet", jp.getCurrentToken().name());
                            }
                        }
                        String s = String.format("%s%s%s", JsonToken.START_OBJECT.asString(), sb.toString(),
                                JsonToken.END_OBJECT.asString());
                        Object parsed = getNested(field.getType(), s.getBytes());
                        m.invoke(b, parsed);
                        break;
                    default:
                        logger.warn("[1] Not sure how to handle {} yet", jp.getCurrentToken().name());
                    }
                }
            } catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException
                    | IllegalArgumentException e) {
                logger.error("Failed setting field [{}], builder: {}", fieldName, b.getClass().getName(), e);
            }
        }
    }
}

From source file:com.msopentech.odatajclient.engine.data.metadata.edm.SchemaDeserializer.java

@Override
public Schema deserialize(final JsonParser jp, final DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

    final Schema schema = new Schema();

    for (; jp.getCurrentToken() != JsonToken.END_OBJECT; jp.nextToken()) {
        final JsonToken token = jp.getCurrentToken();
        if (token == JsonToken.FIELD_NAME) {
            if ("Namespace".equals(jp.getCurrentName())) {
                schema.setNamespace(jp.nextTextValue());
            } else if ("Alias".equals(jp.getCurrentName())) {
                schema.setAlias(jp.nextTextValue());
            } else if ("Using".equals(jp.getCurrentName())) {
                jp.nextToken();//from   www  .ja  v  a2s  . co  m
                schema.getUsings().add(jp.getCodec().readValue(jp, Using.class));
            } else if ("Association".equals(jp.getCurrentName())) {
                jp.nextToken();
                schema.getAssociations().add(jp.getCodec().readValue(jp, Association.class));
            } else if ("ComplexType".equals(jp.getCurrentName())) {
                jp.nextToken();
                schema.getComplexTypes().add(jp.getCodec().readValue(jp, ComplexType.class));
            } else if ("EntityType".equals(jp.getCurrentName())) {
                jp.nextToken();
                schema.getEntityTypes().add(jp.getCodec().readValue(jp, EntityType.class));
            } else if ("EnumType".equals(jp.getCurrentName())) {
                jp.nextToken();
                schema.getEnumTypes().add(jp.getCodec().readValue(jp, EnumType.class));
            } else if ("ValueTerm".equals(jp.getCurrentName())) {
                jp.nextToken();
                schema.getValueTerms().add(jp.getCodec().readValue(jp, ValueTerm.class));
            } else if ("EntityContainer".equals(jp.getCurrentName())) {
                jp.nextToken();
                schema.getEntityContainers().add(jp.getCodec().readValue(jp, EntityContainer.class));
            } else if ("Annotations".equals(jp.getCurrentName())) {
                jp.nextToken();
                schema.getAnnotations().add(jp.getCodec().readValue(jp, Annotations.class));
            }
        }
    }

    return schema;
}

From source file:com.msopentech.odatajclient.engine.data.metadata.edm.AssociationSetDeserializer.java

@Override
public AssociationSet deserialize(final JsonParser jp, final DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

    final AssociationSet associationSet = new AssociationSet();

    for (; jp.getCurrentToken() != JsonToken.END_OBJECT; jp.nextToken()) {
        final JsonToken token = jp.getCurrentToken();
        if (token == JsonToken.FIELD_NAME) {
            if ("Name".equals(jp.getCurrentName())) {
                associationSet.setName(jp.nextTextValue());
            } else if ("Association".equals(jp.getCurrentName())) {
                associationSet.setAssociation(jp.nextTextValue());
            } else if ("End".equals(jp.getCurrentName())) {
                jp.nextToken();/*from ww  w  . ja  v a2 s  .  co m*/
                associationSet.getEnds().add(jp.getCodec().readValue(jp, AssociationSetEnd.class));
            }
        }
    }

    return associationSet;
}

From source file:com.msopentech.odatajclient.engine.data.metadata.edm.TypeAnnotationDeserializer.java

@Override
public TypeAnnotation deserialize(final JsonParser jp, final DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

    final TypeAnnotation typeAnnot = new TypeAnnotation();

    for (; jp.getCurrentToken() != JsonToken.END_OBJECT; jp.nextToken()) {
        final JsonToken token = jp.getCurrentToken();
        if (token == JsonToken.FIELD_NAME) {
            if ("Term".equals(jp.getCurrentName())) {
                typeAnnot.setTerm(jp.nextTextValue());
            } else if ("Qualifier".equals(jp.getCurrentName())) {
                typeAnnot.setQualifier(jp.nextTextValue());
            } else if ("Documentation".equals(jp.getCurrentName())) {
                jp.nextToken();/*from  w ww  .ja  v  a 2s  .  c o  m*/
                typeAnnot.setDocumentation(jp.getCodec().readValue(jp, Documentation.class));
            } else if ("PropertyValue".equals(jp.getCurrentName())) {
                jp.nextToken();
                typeAnnot.getPropertyValues().add(jp.getCodec().readValue(jp, PropertyValue.class));
            }
        }
    }

    return typeAnnot;
}

From source file:com.msopentech.odatajclient.engine.data.metadata.edm.FunctionImportDeserializer.java

@Override
public FunctionImport deserialize(final JsonParser jp, final DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

    final FunctionImport funcImp = new FunctionImport();

    for (; jp.getCurrentToken() != JsonToken.END_OBJECT; jp.nextToken()) {
        final JsonToken token = jp.getCurrentToken();
        if (token == JsonToken.FIELD_NAME) {
            if ("Name".equals(jp.getCurrentName())) {
                funcImp.setName(jp.nextTextValue());
            } else if ("ReturnType".equals(jp.getCurrentName())) {
                funcImp.setReturnType(jp.nextTextValue());
            } else if ("EntitySet".equals(jp.getCurrentName())) {
                funcImp.setEntitySet(jp.nextTextValue());
            } else if ("EntitySetPath".equals(jp.getCurrentName())) {
                funcImp.setEntitySetPath(jp.nextTextValue());
            } else if ("IsComposable".equals(jp.getCurrentName())) {
                funcImp.setComposable(Boolean.valueOf(jp.nextTextValue()));
            } else if ("IsSideEffecting".equals(jp.getCurrentName())) {
                funcImp.setSideEffecting(Boolean.valueOf(jp.nextTextValue()));
            } else if ("IsBindable".equals(jp.getCurrentName())) {
                funcImp.setBindable(Boolean.valueOf(jp.nextTextValue()));
            } else if ("IsAlwaysBindable".equals(jp.getCurrentName())) {
                funcImp.setAlwaysBindable(Boolean.valueOf(jp.nextTextValue()));
            } else if ("HttpMethod".equals(jp.getCurrentName())) {
                funcImp.setHttpMethod(jp.nextTextValue());
            } else if ("Parameter".equals(jp.getCurrentName())) {
                jp.nextToken();/*  ww w  .  j  av  a  2  s  . c o  m*/
                funcImp.getParameters().add(jp.getCodec().readValue(jp, Parameter.class));
            }
        }
    }

    return funcImp;
}

From source file:com.msopentech.odatajclient.engine.data.metadata.edm.ReferentialConstraintRoleDeserializer.java

@Override
public ReferentialConstraintRole deserialize(final JsonParser jp, final DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

    final ReferentialConstraintRole refConstRole = new ReferentialConstraintRole();

    for (; jp.getCurrentToken() != JsonToken.END_OBJECT; jp.nextToken()) {
        final JsonToken token = jp.getCurrentToken();
        if (token == JsonToken.FIELD_NAME) {
            if ("Role".equals(jp.getCurrentName())) {
                refConstRole.setRole(jp.nextTextValue());
            } else if ("PropertyRef".equals(jp.getCurrentName())) {
                jp.nextToken();//from w  w w  .j  a va2s. c  om
                refConstRole.getPropertyRefs().add(jp.getCodec().readValue(jp, PropertyRef.class));
            }
        }
    }

    return refConstRole;
}