List of usage examples for com.fasterxml.jackson.databind DeserializationContext mappingException
public JsonMappingException mappingException(String paramString)
From source file:org.agorava.linkedin.jackson.ConnectionAuthorizationDeserializer.java
@Override public ConnectionAuthorization deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { ObjectMapper mapper = BeanResolver.getInstance().resolve(ObjectMapper.class); if (jp.hasCurrentToken() && jp.getCurrentToken().equals(JsonToken.START_OBJECT)) { JsonNode dataNode = jp.readValueAs(JsonNode.class).get("headers").get("values").get(0); if (dataNode != null) { return mapper.reader(new TypeReference<ConnectionAuthorization>() { }).readValue(dataNode);/*from ww w .j a v a2 s . c om*/ } } throw ctxt.mappingException("Expected JSON object"); }
From source file:org.springframework.social.linkedin.api.impl.json.ConnectionAuthorizationDeserializer.java
public ConnectionAuthorization deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new LinkedInModule()); if (jp.hasCurrentToken() && jp.getCurrentToken().equals(JsonToken.START_OBJECT)) { JsonNode dataNode = jp.readValueAs(JsonNode.class).get("headers").get("values").get(0); if (dataNode != null) { return mapper.reader(new TypeReference<ConnectionAuthorization>() { }).readValue(dataNode);// w w w.ja v a2 s . c o m } } throw ctxt.mappingException("Expected JSON object"); }
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 ww .j ava 2 s . c om*/ } 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:com.bitsofproof.dropwizard.supernode.jackson.AddressDeserializer.java
@Override public Address deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { JsonToken t = jp.getCurrentToken();// w ww . j a va2s . c o m if (t == JsonToken.VALUE_STRING) { try { String satoshiStyle = jp.getText().trim(); if (satoshiStyle.length() == 0) { return null; } return Address.fromSatoshiStyle(satoshiStyle); } catch (ValidationException e) { throw JsonMappingException.from(jp, "Error deserializing bitcoin address", e); } } throw ctxt.mappingException(getValueClass()); }
From source file:com.inversoft.json.ZonedDateTimeDeserializer.java
@Override public ZonedDateTime deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { JsonToken t = jp.getCurrentToken();//from w w w. j a v a 2 s . co m long value; if (t == JsonToken.VALUE_NUMBER_INT || t == JsonToken.VALUE_NUMBER_FLOAT) { value = jp.getLongValue(); } else if (t == JsonToken.VALUE_STRING) { String str = jp.getText().trim(); if (str.length() == 0) { return null; } try { value = Long.parseLong(str); } catch (NumberFormatException e) { throw ctxt.mappingException(handledType()); } } else { throw ctxt.mappingException(handledType()); } return ZonedDateTime.ofInstant(Instant.ofEpochMilli(value), ZoneOffset.UTC); }
From source file:com.boxedfolder.carrot.domain.util.DateTimeDeserializer.java
@Override public DateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { DateTime dateTime = null;/*www . j a v a 2 s. c om*/ try { JsonToken currentToken = jsonParser.getCurrentToken(); System.out.println(jsonParser.getText()); if (currentToken == JsonToken.VALUE_STRING) { String dateTimeAsString = jsonParser.getText().trim(); DateTimeFormatter formatter = ISODateTimeFormat.dateTime().withZoneUTC(); dateTime = formatter.parseDateTime(dateTimeAsString); } } catch (Exception e) { throw deserializationContext.mappingException(getClass()); } return dateTime; }
From source file:org.apache.ode.jacob.soup.jackson.ChannelRefDeserializer.java
@Override public ChannelRef deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { Object target = null;/*w w w . j av a 2 s . c o m*/ while (jp.nextToken() != JsonToken.END_OBJECT) { String fieldname = jp.getCurrentName(); if (jp.getCurrentToken() == JsonToken.FIELD_NAME) { // if we're not already on the field, advance by one. jp.nextToken(); } if ("target".equals(fieldname)) { target = jp.readValueAs(Object.class); } } if (target == null) { throw ctxt.mappingException(ChannelRef.class); } return new ChannelRef(target); }
From source file:javaslang.jackson.datatype.deserialize.EitherDeserializer.java
@Override public Either<?, ?> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { boolean right = false; Object value = null;//from w w w.j ava 2 s. co m int cnt = 0; for (JsonToken jsonToken = p.nextToken(); jsonToken != END_ARRAY; jsonToken = p.nextToken()) { cnt++; switch (cnt) { case 1: String def = (String) stringDeserializer.deserialize(p, ctxt); if ("right".equals(def)) { right = true; } else if ("left".equals(def)) { right = false; } else { throw ctxt.mappingException(javaType.getRawClass()); } break; case 2: JsonDeserializer<?> deserializer = right ? deserializer(1) : deserializer(0); value = (jsonToken != VALUE_NULL) ? deserializer.deserialize(p, ctxt) : deserializer.getNullValue(ctxt); break; } } if (cnt != 2) { throw ctxt.mappingException(javaType.getRawClass()); } return right ? Either.right(value) : Either.left(value); }
From source file:org.springframework.data.convert.JsonBooleanDeserializer.java
@Override public Boolean deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { JsonToken currentToken = jp.getCurrentToken(); if (currentToken.equals(JsonToken.VALUE_STRING)) { String text = jp.getText().trim(); if (YES.equalsIgnoreCase(text)) { return Boolean.TRUE; } else if (NO.equalsIgnoreCase(text)) { return Boolean.FALSE; }/* w w w . j av a 2 s. c o m*/ throw ctxt.weirdStringException(text, Boolean.class, "Only \"" + YES + "\" or \"" + NO + "\" values supported"); } else if (currentToken.equals(JsonToken.VALUE_NULL)) { return getNullValue(); } throw ctxt.mappingException(Boolean.class); }
From source file:org.apache.ode.jacob.soup.jackson.ChannelProxyDeserializer.java
@Override public Channel deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { String type = null;//from w w w . ja va 2s . co m int id = -1; while (jp.nextToken() != JsonToken.END_OBJECT) { String fieldname = jp.getCurrentName(); if (jp.getCurrentToken() == JsonToken.FIELD_NAME) { // if we're not already on the field, advance by one. jp.nextToken(); } if ("channelType".equals(fieldname)) { type = jp.getText(); } else if ("channelId".equals(fieldname)) { id = jp.getIntValue(); } } if (type == null) { throw ctxt.mappingException(Channel.class); } if (id < 0) { throw ctxt.mappingException(Channel.class); } try { CommChannel cchannel = new CommChannel(ctxt.findClass(type)); cchannel.setId(id); return (Channel) ChannelFactory.createChannel(cchannel, cchannel.getType()); } catch (ClassNotFoundException e) { throw ctxt.instantiationException(Channel.class, e); } }