List of usage examples for com.fasterxml.jackson.databind DeserializationContext getParser
public final JsonParser getParser()
From source file:com.msopentech.odatajclient.engine.data.Deserializer.java
public static Edmx toMetadata(final InputStream input) { try {//from ww w . j a v a 2 s.com final XmlMapper xmlMapper = new XmlMapper( new XmlFactory(new InputFactoryImpl(), new OutputFactoryImpl()), new JacksonXmlModule()); xmlMapper.addHandler(new DeserializationProblemHandler() { @Override public boolean handleUnknownProperty(final DeserializationContext ctxt, final JsonParser jp, final JsonDeserializer<?> deserializer, final Object beanOrClass, final String propertyName) throws IOException, JsonProcessingException { // 1. special handling of AbstractAnnotatedEdm's fields if (beanOrClass instanceof AbstractAnnotatedEdm && AbstractAnnotatedEdmUtils.isAbstractAnnotatedProperty(propertyName)) { AbstractAnnotatedEdmUtils.parseAnnotatedEdm((AbstractAnnotatedEdm) beanOrClass, jp); } // 2. skip any other unknown property else { ctxt.getParser().skipChildren(); } return true; } }); return xmlMapper.readValue(input, Edmx.class); } catch (Exception e) { throw new IllegalArgumentException("Could not parse as Edmx document", e); } }
From source file:com.addthis.codec.jackson.CodecBeanDeserializer.java
private void handleDefaultsAndRequiredAndNull(DeserializationContext ctxt, ObjectNode fieldValues) throws JsonMappingException { Iterator<SettableBeanProperty> propertyIterator = getDelegatee().properties(); while (propertyIterator.hasNext()) { SettableBeanProperty prop = propertyIterator.next(); String propertyName = prop.getName(); JsonNode fieldValue = fieldValues.path(propertyName); if (fieldValue.isMissingNode() || fieldValue.isNull()) { if (fieldDefaults.hasNonNull(propertyName)) { fieldValue = fieldDefaults.get(propertyName).deepCopy(); fieldValues.set(propertyName, fieldValue); } else if (prop.isRequired()) { throw MissingPropertyException.from(ctxt.getParser(), prop.getType().getRawClass(), propertyName, getKnownPropertyNames()); } else if (fieldValue.isNull() && (prop.getType().isPrimitive() || (prop.getValueDeserializer().getNullValue() == null))) { // don't overwrite possible hard-coded defaults/ values with nulls unless they are fancy fieldValues.remove(propertyName); }//from ww w .j a v a2s . c o m } if (fieldValue.isTextual()) { try { // sometimes we erroneously get strings that would parse into valid numbers and maybe other edge // cases (eg. when using system property overrides in typesafe-config). So we'll go ahead and guard // with this regex to make sure we only get reasonable candidates. Time time = prop.getAnnotation(Time.class); if ((time != null) && NUMBER_UNIT.matcher(fieldValue.textValue()).matches()) { Duration dropWizardDuration = Duration.parse(fieldValue.asText()); long asLong = time.value().convert(dropWizardDuration.getQuantity(), dropWizardDuration.getUnit()); fieldValues.put(propertyName, asLong); } else if ((prop.getAnnotation(Bytes.class) != null) && NUMBER_UNIT.matcher(fieldValue.textValue()).matches()) { Size dropWizardSize = Size.parse(fieldValue.asText()); long asLong = dropWizardSize.toBytes(); fieldValues.put(propertyName, asLong); } } catch (Throwable cause) { throw JsonMappingException.wrapWithPath(cause, prop.getType().getRawClass(), propertyName); } } } }
From source file:org.apache.olingo.client.core.serialization.ClientODataDeserializerImpl.java
protected XmlMapper getXmlMapper() { final XmlMapper xmlMapper = new XmlMapper(new XmlFactory(new InputFactoryImpl(), new OutputFactoryImpl()), new JacksonXmlModule()); xmlMapper.setInjectableValues(new InjectableValues.Std().addValue(Boolean.class, Boolean.FALSE)); xmlMapper.addHandler(new DeserializationProblemHandler() { @Override//ww w .j a va 2 s .c o m public boolean handleUnknownProperty(final DeserializationContext ctxt, final JsonParser jp, final com.fasterxml.jackson.databind.JsonDeserializer<?> deserializer, final Object beanOrClass, final String propertyName) throws IOException, JsonProcessingException { // skip any unknown property ctxt.getParser().skipChildren(); return true; } }); return xmlMapper; }