List of usage examples for com.fasterxml.jackson.core JsonParser getText
public abstract String getText() throws IOException, JsonParseException;
From source file:com.netflix.suro.jackson.DefaultObjectMapper.java
@Inject public DefaultObjectMapper(final Injector injector, Set<TypeHolder> crossInjectable) { SimpleModule serializerModule = new SimpleModule("SuroServer default serializers"); serializerModule.addSerializer(ByteOrder.class, ToStringSerializer.instance); serializerModule.addDeserializer(ByteOrder.class, new JsonDeserializer<ByteOrder>() { @Override//from w w w . j a v a 2 s . c om public ByteOrder deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { if (ByteOrder.BIG_ENDIAN.toString().equals(jp.getText())) { return ByteOrder.BIG_ENDIAN; } return ByteOrder.LITTLE_ENDIAN; } }); registerModule(serializerModule); registerModule(new GuavaModule()); if (injector != null) { setInjectableValues(new InjectableValues() { @Override public Object findInjectableValue(Object valueId, DeserializationContext ctxt, BeanProperty forProperty, Object beanInstance) { LOG.info("Looking for " + valueId); try { return injector.getInstance( Key.get(forProperty.getType().getRawClass(), Names.named((String) valueId))); } catch (Exception e) { try { return injector.getInstance(forProperty.getType().getRawClass()); } catch (Exception ex) { LOG.info("No implementation found, returning null"); } return null; } } }); } configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); configure(MapperFeature.AUTO_DETECT_GETTERS, false); configure(MapperFeature.AUTO_DETECT_CREATORS, false); configure(MapperFeature.AUTO_DETECT_FIELDS, false); configure(MapperFeature.AUTO_DETECT_IS_GETTERS, false); configure(MapperFeature.AUTO_DETECT_SETTERS, false); configure(SerializationFeature.INDENT_OUTPUT, false); if (crossInjectable != null) { for (TypeHolder entry : crossInjectable) { LOG.info("Registering subtype : " + entry.getName() + " -> " + entry.getRawType().getCanonicalName()); registerSubtypes(new NamedType(entry.getRawType(), entry.getName())); } } }
From source file:org.helm.notation2.wsadapter.MonomerWSLoader.java
/** * Private routine to deserialize JSON containing monomer categorization data. * This is done manually to give more freedom regarding data returned by the * webservice./* w w w.j a v a 2 s .c o m*/ * * @param parser the JSONParser containing JSONData. * @return List containing the monomer categorization * * @throws JsonParseException * @throws IOException */ private static List<CategorizedMonomer> deserializeEditorCategorizationConfig(JsonParser parser) throws JsonParseException, IOException { List<CategorizedMonomer> config = new LinkedList<CategorizedMonomer>(); CategorizedMonomer currentMonomer = null; parser.nextToken(); while (parser.hasCurrentToken()) { String fieldName = parser.getCurrentName(); JsonToken token = parser.getCurrentToken(); if (JsonToken.START_OBJECT.equals(token)) { currentMonomer = new CategorizedMonomer(); } else if (JsonToken.END_OBJECT.equals(token)) { config.add(currentMonomer); } if (fieldName != null) { switch (fieldName) { // id is first field case "monomerID": parser.nextToken(); currentMonomer.setMonomerID(parser.getText()); break; case "monomerName": parser.nextToken(); currentMonomer.setMonomerName(parser.getText()); break; case "naturalAnalogon": parser.nextToken(); currentMonomer.setNaturalAnalogon(parser.getText()); break; case "monomerType": parser.nextToken(); currentMonomer.setMonomerType(parser.getText()); break; case "polymerType": parser.nextToken(); currentMonomer.setPolymerType(parser.getText()); break; case "category": parser.nextToken(); currentMonomer.setCategory(parser.getText()); break; case "shape": parser.nextToken(); currentMonomer.setShape(parser.getText()); break; case "fontColor": parser.nextToken(); currentMonomer.setFontColor(parser.getText()); break; case "backgroundColor": parser.nextToken(); currentMonomer.setBackgroundColor(parser.getText()); break; default: break; } } parser.nextToken(); } return config; }
From source file:com.basistech.rosette.dm.jackson.ListAttributeDeserializer.java
@SuppressWarnings("unchecked") private ListAttribute deserialize(JsonParser jp, DeserializationContext ctxt, TokenBuffer tb) throws IOException { jp.nextToken();/* www.j a va 2 s .c o m*/ String keyName = jp.getText(); if (tb != null) { // need to put back skipped properties? jp = JsonParserSequence.createFlattened(tb.asParser(jp), jp); } // Must point to the next value; tb had no current, jp pointed to VALUE_STRING: KnownAttribute attribute = KnownAttribute.getAttributeForKey(keyName); 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(); JsonToken nextToken; while ((nextToken = jp.nextToken()) != JsonToken.END_OBJECT) { if (nextToken != JsonToken.FIELD_NAME) { throw ctxt.wrongTokenException(jp, JsonToken.END_OBJECT, "Expected field name."); } else { String name = jp.getCurrentName(); if ("items".equals(name)) { // the actual list items. nextToken = jp.nextToken(); if (nextToken == JsonToken.VALUE_EMBEDDED_OBJECT) { Object o = jp.getEmbeddedObject(); if (o instanceof List) { // could it be an array, also?!? // when using JsonTree, sometimes Jackson just sticks the entire Java object in here. items.addAll((List) o); } else { throw ctxt.mappingException( "List contains VALUE_EMBEDDED_OBJECT for items, but it wasn't a list."); } } else if (nextToken != JsonToken.START_ARRAY) { // what about nothing? throw ctxt.wrongTokenException(jp, JsonToken.START_ARRAY, "Expected array of items"); } else { // the START_ARRAY case, which is _normal_. Read the elements. while (jp.nextToken() != JsonToken.END_ARRAY) { items.add(jp.readValueAs(itemClass)); } } } else { nextToken = jp.nextToken(); Object value; if (nextToken == JsonToken.VALUE_EMBEDDED_OBJECT) { value = jp.getEmbeddedObject(); } else { value = jp.readValueAs(Object.class); } builder.extendedProperty(name, value); } } } builder.setItems(items); return builder.build(); }
From source file:de.terrestris.shogun.deserializer.DateDeserializer.java
/** * Overwrite the deserializer for date strings in the form of a * unix timestamp (1293836400).// w w w. j av a 2 s.c o m * * This method will also try to interpret strings like "30.04.2013 13:43:55" * as dates. */ @Override public Date deserialize(JsonParser jsonParser, DeserializationContext context) throws IOException, JsonProcessingException { Date deserializedDate = null; String s = jsonParser.getText(); if (DateDeserializer.looksLikeFormattedDateString(s)) { deserializedDate = DateDeserializer.parseCommonlyFormattedDate(s); } else { String failNotice = "Failed to interpret string '" + s + "' as java.lang.Long. That string did not look like a " + "formatted date either."; try { Long number = new Long(s); deserializedDate = new Date(number); } catch (NumberFormatException e) { LOGGER.error(failNotice + " : " + e.getMessage()); } } return deserializedDate; }
From source file:com.wavemaker.commons.json.deserializer.WMLocalDateTimeDeSerializer.java
@Override public LocalDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { JsonToken currentToken = jsonParser.getCurrentToken(); if (currentToken == JsonToken.VALUE_STRING) { String value = jsonParser.getText(); return getLocalDateTime(value); }//from www.j av a 2 s.co m throw new WMRuntimeException("Not a String value"); }
From source file:com.wavemaker.commons.json.deserializer.WMLocalTimeDeserializer.java
@Override public LocalTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { JsonToken currentToken = jsonParser.getCurrentToken(); if (currentToken == JsonToken.VALUE_STRING) { String value = jsonParser.getText(); return getLocalDateTime(value); }/*w w w.j a v a 2 s .com*/ throw new WMRuntimeException("Not a String value"); }
From source file:com.sdl.odata.unmarshaller.json.core.JsonProcessor.java
/** * Process OData link.//from w w w. java 2 s . c om * * @param jsonParser the parser * @throws IOException If unable to read input parser * @return the link */ private String processLink(JsonParser jsonParser) throws IOException { final String link = jsonParser.getText(); if (link.contains(SVC_EXTENSION)) { return link.substring(link.indexOf(SVC_EXTENSION) + SVC_EXTENSION.length()); } return link; }
From source file:com.wavemaker.commons.json.deserializer.WMSqlDateDeSerializer.java
@Override public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { JsonToken currentToken = jsonParser.getCurrentToken(); if (currentToken == JsonToken.VALUE_STRING) { String value = jsonParser.getText(); return getDate(value); }//from ww w .ja va2 s. c o m throw new WMRuntimeException("Unable to read the token as java.sql.Date"); }
From source file:YexOpenRtbJsonReader.java
@Override protected void readBidField(JsonParser par, OpenRtb.BidResponse.SeatBid.Bid.Builder bid, String fieldName) throws IOException { switch (fieldName) { case "id": bid.setId(par.getText()); break;/*from ww w. j a v a2 s. c o m*/ case "impid": bid.setImpid(par.getText()); break; case "price": bid.setPrice(getDoubleValue(par)); break; case "adid": bid.setAdid(par.getText()); break; case "nurl": bid.setNurl(par.getText()); break; case "adm": bid.setAdmNative(factory().newNativeReader().readNativeResponse(par)); break; case "adomain": for (startArray(par); endArray(par); par.nextToken()) { bid.addAdomain(par.getText()); } break; case "bundle": bid.setBundle(par.getText()); break; case "iurl": bid.setIurl(par.getText()); break; case "cid": bid.setCid(par.getText()); break; case "crid": bid.setCrid(par.getText()); break; case "cat": for (startArray(par); endArray(par); par.nextToken()) { String cat = par.getText(); if (OpenRtbUtils.categoryFromName(cat) != null) { bid.addCat(cat); } } break; case "attr": for (startArray(par); endArray(par); par.nextToken()) { OpenRtb.CreativeAttribute value = OpenRtb.CreativeAttribute.valueOf(par.getIntValue()); if (checkEnum(value)) { bid.addAttr(value); } } break; case "dealid": bid.setDealid(par.getText()); break; case "w": bid.setW(par.getIntValue()); break; case "h": bid.setH(par.getIntValue()); break; default: readOther(bid, par, fieldName); } }
From source file:com.clicktravel.infrastructure.persistence.aws.cloudsearch.client.JodaDateTimeDeserializer.java
@Override public DateTime deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException, JsonProcessingException { if (jp.getCurrentToken() != JsonToken.VALUE_STRING) { throw ctxt.mappingException("Expected JSON string"); }//from w w w . j a v a2s.c o m return formatter.parseDateTime(jp.getText()); }