List of usage examples for com.fasterxml.jackson.core JsonParser getCurrentToken
public abstract JsonToken getCurrentToken();
From source file:io.coala.json.DynaBean.java
/** * @param referenceType/*from www. j a v a2 s . c om*/ * @param <S> * @param <T> * @return */ static final <S, T> JsonDeserializer<T> createJsonDeserializer(final ObjectMapper om, final Class<T> resultType, final Properties... imports) { return new JsonDeserializer<T>() { @Override public T deserializeWithType(final JsonParser jp, final DeserializationContext ctxt, final TypeDeserializer typeDeserializer) throws IOException, JsonProcessingException { return deserialize(jp, ctxt); } @Override public T deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException, JsonProcessingException { if (jp.getCurrentToken() == JsonToken.VALUE_NULL) return null; // if( Wrapper.class.isAssignableFrom( resultType ) ) // { // // FIXME // LOG.trace( "deser wrapper intf of {}", jp.getText() ); // return (T) Wrapper.Util.valueOf( jp.getText(), // resultType.asSubclass( Wrapper.class ) ); // } if (Config.class.isAssignableFrom(resultType)) { final Map<String, Object> entries = jp.readValueAs(new TypeReference<Map<String, Object>>() { }); final Iterator<Entry<String, Object>> it = entries.entrySet().iterator(); for (Entry<String, Object> next = null; it.hasNext(); next = it.next()) if (next != null && next.getValue() == null) { LOG.trace("Ignoring null value: {}", next); it.remove(); } return resultType.cast(ConfigFactory.create(resultType.asSubclass(Config.class), entries)); } // else if (Config.class.isAssignableFrom(resultType)) // throw new JsonGenerationException( // "Config does not extend "+Mutable.class.getName()+" required for deserialization: " // + Arrays.asList(resultType // .getInterfaces())); // can't parse directly to interface type final DynaBean bean = new DynaBean(); final TreeNode tree = jp.readValueAsTree(); // override attributes as defined in interface getters final Set<String> attributes = new HashSet<>(); for (Method method : resultType.getMethods()) { if (method.getReturnType().equals(Void.TYPE) || method.getParameterTypes().length != 0) continue; final String attribute = method.getName(); if (attribute.equals("toString") || attribute.equals("hashCode")) continue; attributes.add(attribute); final TreeNode value = tree.get(attribute);// bean.any().get(attributeName); if (value == null) continue; bean.set(method.getName(), om.treeToValue(value, JsonUtil.checkRegistered(om, method.getReturnType(), imports))); } if (tree.isObject()) { // keep superfluous properties as TreeNodes, just in case final Iterator<String> fieldNames = tree.fieldNames(); while (fieldNames.hasNext()) { final String fieldName = fieldNames.next(); if (!attributes.contains(fieldName)) bean.set(fieldName, tree.get(fieldName)); } } else if (tree.isValueNode()) { for (Class<?> type : resultType.getInterfaces()) for (Method method : type.getDeclaredMethods()) { // LOG.trace( "Scanning {}", method ); if (method.isAnnotationPresent(JsonProperty.class)) { final String property = method.getAnnotation(JsonProperty.class).value(); // LOG.trace( "Setting {}: {}", property, // ((ValueNode) tree).textValue() ); bean.set(property, ((ValueNode) tree).textValue()); } } } else throw ExceptionFactory.createUnchecked("Expected {} but parsed: {}", resultType, tree.getClass()); return DynaBean.proxyOf(om, resultType, bean, imports); } }; }
From source file:com.sdl.odata.unmarshaller.json.core.JsonProcessor.java
/** * Process all things that do not contain special ODataTags. * * @param jsonParser the parser/*from w ww .j a v a 2 s .c o m*/ * @throws ODataUnmarshallingException If unable to unmarshall * @throws IOException If unable to read input parser */ private void process(JsonParser jsonParser) throws IOException, ODataUnmarshallingException { if (jsonParser.getCurrentToken() == JsonToken.FIELD_NAME) { LOG.info("Starting to parse {} token", jsonParser.getCurrentName()); String key = jsonParser.getCurrentName(); jsonParser.nextToken(); JsonToken token = jsonParser.getCurrentToken(); if (token == JsonToken.START_ARRAY) { if (JsonConstants.VALUE.equals(key)) { throw new ODataUnmarshallingException("Feed is not supported"); } values.put(key, getCollectionValue(jsonParser)); } else if (token == JsonToken.START_OBJECT) { values.put(key, getEmbeddedObject(jsonParser)); } else { if (token.equals(JsonToken.VALUE_NULL)) { values.put(key, null); } else { values.put(key, jsonParser.getText()); } } } }
From source file:com.joliciel.jochre.search.webClient.SearchResults.java
public SearchResults(String json) { try {//from w w w. ja v a 2s . com scoreDocs = new ArrayList<SearchDocument>(); Reader reader = new StringReader(json); JsonFactory jsonFactory = new JsonFactory(); // or, for data binding, org.codehaus.jackson.mapper.MappingJsonFactory JsonParser jsonParser = jsonFactory.createJsonParser(reader); // Sanity check: verify that we got "Json Object": if (jsonParser.nextToken() != JsonToken.START_OBJECT) throw new RuntimeException("Expected START_OBJECT, but was " + jsonParser.getCurrentToken() + " at " + jsonParser.getCurrentLocation()); while (jsonParser.nextToken() != JsonToken.END_OBJECT) { String baseName = jsonParser.getCurrentName(); LOG.debug("Found baseName: " + baseName); if (jsonParser.nextToken() != JsonToken.START_OBJECT) throw new RuntimeException("Expected START_OBJECT, but was " + jsonParser.getCurrentToken() + " at " + jsonParser.getCurrentLocation()); SearchDocument doc = new SearchDocument(baseName, jsonParser); scoreDocs.add(doc); } // next scoreDoc } catch (JsonParseException e) { LOG.error(e); throw new RuntimeException(e); } catch (IOException e) { LOG.error(e); throw new RuntimeException(e); } }
From source file:org.emfjson.jackson.streaming.StreamReader.java
public void parse(Resource resource, JsonParser parser) throws IOException { prepare(resource);/*from ww w . j av a2 s . co m*/ if (parser.getCurrentToken() == null) { parser.nextToken(); } switch (parser.getCurrentToken()) { case START_OBJECT: EObject result = parseObject(parser, null, null, options.rootElement); if (result != null) { resource.getContents().add(result); } break; case START_ARRAY: parseArray(parser); break; default: break; } resolve(); entries.clear(); }
From source file:no.ssb.jsonstat.v2.deser.DatasetDeserializer.java
List<Number> parseValues(JsonParser p, DeserializationContext ctxt) throws IOException { List<Number> result = Collections.emptyList(); switch (p.getCurrentToken()) { case START_OBJECT: SortedMap<Integer, Number> map = p.readValueAs(VALUES_MAP); result = new AbstractList<Number>() { @Override/*from ww w . ja v a 2s . co m*/ public int size() { return map.lastKey() + 1; } @Override public Number get(int index) { return map.get(index); } }; break; case START_ARRAY: result = p.readValueAs(VALUES_LIST); break; default: ctxt.handleUnexpectedToken(this._valueClass, p.getCurrentToken(), p, "msg"); } return result; }
From source file:com.pursuer.reader.easyrss.data.parser.TagJSONParser.java
public void parse() throws JsonParseException, IOException, IllegalStateException { final JsonFactory factory = new JsonFactory(); final JsonParser parser = factory.createJsonParser(input); Tag tag = new Tag(); int level = 0; boolean found = false; while (parser.nextToken() != null) { final String name = parser.getCurrentName(); switch (parser.getCurrentToken()) { case START_OBJECT: case START_ARRAY: level++;// w w w . ja v a 2 s . co m break; case END_OBJECT: case END_ARRAY: level--; break; case VALUE_STRING: if (level == 3) { if ("id".equals(name)) { tag.setUid(parser.getText()); } else if ("sortid".equals(name)) { tag.setSortId(parser.getText()); } } case FIELD_NAME: if (level == 1 && "tags".equals(name)) { found = true; } default: } if (level == 2) { if (tag.getUid() != null && listener != null) { listener.onTagRetrieved(tag); } tag = new Tag(); } } parser.close(); if (!found) { throw new IllegalStateException("Invalid JSON input"); } }
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 . j a va 2s . c o 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); } }
From source file:org.emfjson.jackson.streaming.StreamReader.java
private void parseArray(JsonParser parser) throws IOException { while (parser.nextToken() != JsonToken.END_ARRAY) { if (parser.getCurrentToken() == JsonToken.START_OBJECT) { EObject result = parseObject(parser, null, null, options.rootElement); if (result != null) { resource.getContents().add(result); }/*w w w .j a v a 2 s . co m*/ } } }
From source file:com.joliciel.jochre.search.highlight.Snippet.java
private void read(JsonParser jsonParser) { try {// w w w . j a v a 2 s . c om if (jsonParser.getCurrentToken() != JsonToken.START_OBJECT) throw new RuntimeException("Expected START_OBJECT, but was " + jsonParser.getCurrentToken() + " at " + jsonParser.getCurrentLocation()); while (jsonParser.nextToken() != JsonToken.END_OBJECT) { String fieldName = jsonParser.getCurrentName(); if (fieldName.equals("docId")) { this.docId = jsonParser.nextIntValue(0); } else if (fieldName.equals("field")) { this.field = jsonParser.nextTextValue(); } else if (fieldName.equals("start")) { this.startOffset = jsonParser.nextIntValue(0); } else if (fieldName.equals("end")) { this.endOffset = jsonParser.nextIntValue(0); } else if (fieldName.equals("score")) { jsonParser.nextValue(); this.score = jsonParser.getDoubleValue(); this.scoreCalculated = true; } else if (fieldName.equals("terms")) { if (jsonParser.nextToken() != JsonToken.START_ARRAY) throw new RuntimeException("Expected START_ARRAY, but was " + jsonParser.getCurrentToken() + " at " + jsonParser.getCurrentLocation()); while (jsonParser.nextToken() != JsonToken.END_ARRAY) { if (jsonParser.getCurrentToken() != JsonToken.START_OBJECT) throw new RuntimeException("Expected START_OBJECT, but was " + jsonParser.getCurrentToken() + " at " + jsonParser.getCurrentLocation()); int termDocId = docId; String termField = field; int termStart = 0; int termEnd = 0; int pageIndex = 0; int imageIndex = 0; double weight = 0.0; while (jsonParser.nextToken() != JsonToken.END_OBJECT) { String termFieldName = jsonParser.getCurrentName(); if (termFieldName.equals("docId")) { termDocId = jsonParser.nextIntValue(0); } else if (termFieldName.equals("field")) { termField = jsonParser.nextTextValue(); } else if (termFieldName.equals("start")) { termStart = jsonParser.nextIntValue(0); } else if (termFieldName.equals("end")) { termEnd = jsonParser.nextIntValue(0); } else if (termFieldName.equals("pageIndex")) { pageIndex = jsonParser.nextIntValue(0); } else if (termFieldName.equals("imageIndex")) { imageIndex = jsonParser.nextIntValue(0); } else if (termFieldName.equals("weight")) { jsonParser.nextValue(); weight = jsonParser.getDoubleValue(); } else { throw new RuntimeException("Unexpected term field name: " + termFieldName + " at " + jsonParser.getCurrentLocation()); } } HighlightTerm highlightTerm = new HighlightTerm(termDocId, termField, termStart, termEnd, imageIndex, pageIndex); highlightTerm.setWeight(weight); this.highlightTerms.add(highlightTerm); } } else { throw new RuntimeException( "Unexpected field name: " + fieldName + " at " + jsonParser.getCurrentLocation()); } } } catch (JsonParseException e) { LOG.error(e); throw new RuntimeException(e); } catch (IOException e) { LOG.error(e); throw new RuntimeException(e); } }