Example usage for com.fasterxml.jackson.core JsonParser getCodec

List of usage examples for com.fasterxml.jackson.core JsonParser getCodec

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonParser getCodec.

Prototype

public abstract ObjectCodec getCodec();

Source Link

Document

Accessor for ObjectCodec associated with this parser, if any.

Usage

From source file:org.apache.druid.query.aggregation.AggregationTestHelper.java

private List readQueryResultArrayFromString(String str) throws Exception {
    List result = new ArrayList();

    JsonParser jp = mapper.getFactory().createParser(str);

    if (jp.nextToken() != JsonToken.START_ARRAY) {
        throw new IAE("not an array [%s]", str);
    }/*from   w ww .j av a2s.  c o  m*/

    ObjectCodec objectCodec = jp.getCodec();

    while (jp.nextToken() != JsonToken.END_ARRAY) {
        result.add(objectCodec.readValue(jp, toolChest.getResultTypeReference()));
    }
    return result;
}

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();/*from w w w. j av  a  2 s  . c  om*/
                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.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();//w  ww  .  j a va2s.  c o m
                refConstRole.getPropertyRefs().add(jp.getCodec().readValue(jp, PropertyRef.class));
            }
        }
    }

    return refConstRole;
}

From source file:com.msopentech.odatajclient.engine.metadata.edm.v4.annotation.DynExprConstructDeserializer.java

@Override
protected DynExprConstruct doDeserialize(final JsonParser jp, final DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

    DynExprConstruct construct = null;//w w w. j av  a2s . c  om

    if (DynExprSingleParamOp.Type.fromString(jp.getCurrentName()) != null) {
        final DynExprSingleParamOp dynExprSingleParamOp = new DynExprSingleParamOp();
        dynExprSingleParamOp.setType(DynExprSingleParamOp.Type.fromString(jp.getCurrentName()));

        jp.nextToken();
        jp.nextToken();
        dynExprSingleParamOp.setExpression(jp.getCodec().readValue(jp, DynExprConstruct.class));

        construct = dynExprSingleParamOp;
    } else if (DynExprDoubleParamOp.Type.fromString(jp.getCurrentName()) != null) {
        final DynExprDoubleParamOp dynExprDoubleParamOp = new DynExprDoubleParamOp();
        dynExprDoubleParamOp.setType(DynExprDoubleParamOp.Type.fromString(jp.getCurrentName()));

        jp.nextToken();
        jp.nextToken();
        dynExprDoubleParamOp.setLeft(jp.getCodec().readValue(jp, DynExprConstruct.class));
        dynExprDoubleParamOp.setRight(jp.getCodec().readValue(jp, DynExprConstruct.class));

        construct = dynExprDoubleParamOp;
    } else if (ArrayUtils.contains(EL_OR_ATTR, jp.getCurrentName())) {
        final AbstractElOrAttrConstruct elOrAttr = getElOrAttrInstance(jp.getCurrentName());
        elOrAttr.setValue(jp.nextTextValue());

        construct = elOrAttr;
    } else if (APPLY.equals(jp.getCurrentName())) {
        jp.nextToken();
        construct = jp.getCodec().readValue(jp, Apply.class);
    } else if (CAST.equals(jp.getCurrentName())) {
        jp.nextToken();
        construct = jp.getCodec().readValue(jp, Cast.class);
    } else if (COLLECTION.equals(jp.getCurrentName())) {
        jp.nextToken();
        construct = jp.getCodec().readValue(jp, Collection.class);
    } else if (IF.equals(jp.getCurrentName())) {
        jp.nextToken();
        jp.nextToken();

        final If _if = new If();
        _if.setGuard(parseConstOrEnumExprConstruct(jp));
        _if.setThen(parseConstOrEnumExprConstruct(jp));
        _if.setElse(parseConstOrEnumExprConstruct(jp));

        construct = _if;
    } else if (IS_OF.equals(jp.getCurrentName())) {
        jp.nextToken();
        construct = jp.getCodec().readValue(jp, IsOf.class);
    } else if (LABELED_ELEMENT.equals(jp.getCurrentName())) {
        jp.nextToken();
        construct = jp.getCodec().readValue(jp, LabeledElement.class);
    } else if (NULL.equals(jp.getCurrentName())) {
        jp.nextToken();
        construct = jp.getCodec().readValue(jp, Null.class);
    } else if (RECORD.equals(jp.getCurrentName())) {
        jp.nextToken();
        construct = jp.getCodec().readValue(jp, Record.class);
    } else if (URL_REF.equals(jp.getCurrentName())) {
        jp.nextToken();
        construct = jp.getCodec().readValue(jp, UrlRef.class);
    }

    return construct;
}

From source file:eu.europa.ec.fisheries.uvms.reporting.service.util.ReportDeserializer.java

@Override
public ReportDTO deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
        throws IOException {

    ObjectCodec oc = jsonParser.getCodec();
    JsonNode node = oc.readTree(jsonParser);

    ReportTypeEnum reportTypeEnum = node.get(REPORT_TYPE) != null
            ? ReportTypeEnum.getReportTypeEnum(node.get(REPORT_TYPE).textValue())
            : ReportTypeEnum.STANDARD;/* w  ww. j a  va2 s .c  om*/

    List<FilterDTO> filterDTOList = new ArrayList<>();

    JsonNode reportIdNode = node.get(ID);
    Long reportId = null;
    if (reportIdNode != null) {
        reportId = reportIdNode.longValue();
    }

    JsonNode filterNode = node.get(FILTER_EXPRESSION);

    if (filterNode != null) {
        addVmsFilters(filterNode.get("vms"), filterDTOList, reportId);
        addAssets(filterNode.get(ASSETS), filterDTOList, reportId);
        addArea(filterNode.get("areas"), filterDTOList, reportId);
        addCommon(filterNode.get("common"), filterDTOList, reportId);
        addFaFilters(filterNode.get("fa"), filterDTOList, reportId);
        if (ReportTypeEnum.SUMMARY.equals(reportTypeEnum)) {
            addGroupCriteria(filterNode.get("criteria"), filterDTOList, reportId, jsonParser);
        }
    }

    boolean withMap = false;
    JsonNode witMapNode = node.get(WITH_MAP);
    if (witMapNode != null) {
        withMap = witMapNode.booleanValue();
    }

    VisibilityEnum visibilityEnum = null;
    JsonNode visibilityNode = node.get(VISIBILITY);
    if (visibilityNode != null) {
        String s = visibilityNode.textValue();
        if (s != null) {
            visibilityEnum = VisibilityEnum.valueOf(s.toUpperCase());
        }
    }

    String nameValue = null;
    JsonNode nameNode = node.get(NAME);
    if (nameNode != null) {
        nameValue = nameNode.textValue();
    }

    JsonNode createdOnNode = node.get(CREATED_ON);
    String createdOnValue = null;
    if (createdOnNode != null) {
        createdOnValue = createdOnNode.textValue();
    }

    JsonNode editableNode = node.get("editable");
    boolean editableValue = false;
    if (createdOnNode != null) {
        editableValue = editableNode.booleanValue();
    }

    ReportDTO build = ReportDTO.builder()
            .description(node.get(DESC) != null ? node.get(DESC).textValue() : null)
            .id(node.get(ID) != null ? node.get(ID).longValue() : null).name(nameValue).withMap(withMap)
            .createdBy(node.get(CREATED_BY) != null ? node.get(CREATED_BY).textValue() : null)
            .filters(filterDTOList).visibility(visibilityEnum)
            .mapConfiguration(createMapConfigurationDTO(withMap, node.get(MAP_CONFIGURATION))).build();
    build.setReportTypeEnum(reportTypeEnum);
    build.setAudit(new AuditDTO(createdOnValue));
    build.setEditable(editableValue);
    return build;
}

From source file:de.fraunhofer.iosb.ilt.sta.deserialize.custom.CustomEntityDeserializer.java

@Override
public T deserialize(JsonParser parser, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    Entity result;//from  www  .  j a  v a 2  s .co  m
    try {
        result = clazz.newInstance();
    } catch (InstantiationException | IllegalAccessException ex) {
        throw new IOException("Error deserializing JSON!");
    }
    // need to make subclass of this class for every Entity subclass with custom field to get expected class!!!
    BeanDescription beanDescription = ctxt.getConfig().introspect(ctxt.constructType(clazz));
    ObjectMapper mapper = (ObjectMapper) parser.getCodec();
    JsonNode obj = (JsonNode) mapper.readTree(parser);
    List<BeanPropertyDefinition> properties = beanDescription.findProperties();
    Iterator<Map.Entry<String, JsonNode>> i = obj.fields();

    // First check if we know all properties that are present.
    if (ctxt.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)) {
        for (; i.hasNext();) {
            Map.Entry<String, JsonNode> next = i.next();
            String fieldName = next.getKey();
            JsonNode field = next.getValue();
            Optional<BeanPropertyDefinition> findFirst = properties.stream()
                    .filter(p -> p.getName().equals(fieldName)).findFirst();
            if (!findFirst.isPresent()) {
                throw new UnrecognizedPropertyException(parser, "Unknown field: " + fieldName,
                        parser.getCurrentLocation(), clazz, fieldName, null);
            }
        }
    }

    for (BeanPropertyDefinition classProperty : properties) {
        if (obj.has(classProperty.getName())) {
            // property is present in class and json
            Annotation annotation = classProperty.getAccessor().getAnnotation(CustomSerialization.class);
            if (annotation != null) {
                // property has custom annotation
                // check if encoding property is also present in json (and also in class itself for sanity reasons)
                CustomSerialization customAnnotation = (CustomSerialization) annotation;
                Optional<BeanPropertyDefinition> encodingClassProperty = properties.stream()
                        .filter(p -> p.getName().equals(customAnnotation.encoding())).findFirst();
                if (!encodingClassProperty.isPresent()) {
                    throw new IOException("Error deserializing JSON as class '" + clazz.toString() + "' \n"
                            + "Reason: field '" + customAnnotation.encoding()
                            + "' specified by annotation as encoding field is not defined in class!");
                }
                String customEncoding = null;
                if (obj.has(customAnnotation.encoding())) {
                    customEncoding = obj.get(customAnnotation.encoding()).asText();
                }
                Object customDeserializedValue = CustomDeserializationManager.getInstance()
                        .getDeserializer(customEncoding)
                        .deserialize(mapper.writeValueAsString(obj.get(classProperty.getName())));
                classProperty.getMutator().setValue(result, customDeserializedValue);
            } else {
                // TODO type identificatin is not safe beacuase may ne be backed by field. Rather do multiple checks
                Object value = mapper.readValue(mapper.writeValueAsString(obj.get(classProperty.getName())),
                        classProperty.getField().getType());
                classProperty.getMutator().setValue(result, value);
            }
        }
    }
    return (T) result;
}

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   ww w  . 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.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();//  ww w  . j a v  a 2s .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 w  w w  .j  a  v  a2  s  .  c  om*/
                associationSet.getEnds().add(jp.getCodec().readValue(jp, AssociationSetEnd.class));
            }
        }
    }

    return associationSet;
}