List of usage examples for com.fasterxml.jackson.core JsonParser readValueAs
@SuppressWarnings("unchecked") public <T> T readValueAs(TypeReference<?> valueTypeRef) throws IOException, JsonProcessingException
From source file:org.instagram4j.DefaultInstagramClient.java
@SuppressWarnings("unchecked") private <T> Result<T[]> requestEntities(HttpRequestBase method, Class<T> type) throws InstagramException { method.getParams().setParameter("http.useragent", "Instagram4j/1.0"); JsonParser jp = null; HttpResponse response = null;/*from w w w . j a v a 2 s .c o m*/ ResultMeta meta = null; try { method.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8"); setEnforceHeader(method); HttpClient client = new DefaultHttpClient(); client.getParams().setParameter(AllClientPNames.CONNECTION_TIMEOUT, 15000); client.getParams().setParameter(AllClientPNames.SO_TIMEOUT, 30000); if (LOG.isDebugEnabled()) LOG.debug(String.format("Requesting entities entry point %s, method %s", method.getURI().toString(), method.getMethod())); autoThrottle(); response = client.execute(method); jp = createParser(response, method); JsonToken tok = jp.nextToken(); if (tok != JsonToken.START_OBJECT) { if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) throw createInstagramException("Instagram request failed", method.getURI().toString(), response, null, null); throw createInstagramException("Invalid response format from Instagram API", method.getURI().toString(), response, null, null); } Pagination pagination = null; T[] data = null; while (true) { tok = jp.nextValue(); if (tok == JsonToken.START_ARRAY) { // Should be "data" String name = jp.getCurrentName(); if (!"data".equals(name)) throw createInstagramException("Unexpected field name " + name, method.getURI().toString(), response, meta, null); List<T> items = new ArrayList<T>(); tok = jp.nextToken(); if (tok == JsonToken.START_OBJECT) { if (type != null) { T item; while ((item = jp.readValueAs(type)) != null) items.add(item); } else jp.readValueAs(Map.class); // Consume & ignore } data = (T[]) Array.newInstance(type, items.size()); System.arraycopy(items.toArray(), 0, data, 0, items.size()); } else if (tok == JsonToken.START_OBJECT) { // Should be "pagination" or "meta" String name = jp.getCurrentName(); if ("pagination".equals(name)) pagination = jp.readValueAs(Pagination.class); else if ("meta".equals(name)) meta = jp.readValueAs(ResultMeta.class); else throw createInstagramException("Unexpected field name " + name, method.getURI().toString(), response, meta, null); } else break; } if (data == null && meta == null && response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) throw createInstagramException("Instagram request failed", method.getURI().toString(), response, null, null); Result<T[]> result = new Result<T[]>(pagination, meta, data); setRateLimits(response, result); return result; } catch (JsonParseException e) { throw createInstagramException("Error parsing response from Instagram: " + e.getMessage(), method.getURI().toString(), response, meta, e); } catch (JsonProcessingException e) { throw createInstagramException("Error parsing response from Instagram: " + e.getMessage(), method.getURI().toString(), response, meta, e); } catch (IOException e) { throw createInstagramException("Error communicating with Instagram: " + e.getMessage(), method.getURI().toString(), response, meta, e); } finally { if (jp != null) try { jp.close(); } catch (IOException e) { } method.releaseConnection(); } }
From source file:com.zenesis.qx.remote.RequestHandler.java
/** * Handles creating a server object to match one created on the client; expects className, * clientId, properties//from w w w .j a v a2 s. c o m * @param jp * @throws ServletException * @throws IOException */ protected void cmdNewObject(JsonParser jp) throws ServletException, IOException { // Get the basics String className = getFieldValue(jp, "className", String.class); int clientId = getFieldValue(jp, "clientId", Integer.class); // Get the class Class<? extends Proxied> clazz; try { clazz = (Class<? extends Proxied>) Class.forName(className); } catch (ClassNotFoundException e) { throw new ServletException("Unknown class " + className); } ProxyType type = ProxyTypeManager.INSTANCE.getProxyType(clazz); // Create the instance Proxied proxied; try { proxied = type.newInstance(clazz); } catch (InstantiationException e) { throw new ServletException("Cannot create class " + className + ": " + e.getMessage(), e); } catch (InvocationTargetException e) { throw new ServletException("Cannot create class " + className + ": " + e.getMessage(), e); } catch (IllegalAccessException e) { throw new ServletException("Cannot create class " + className + ": " + e.getMessage(), e); } // Get the server ID int serverId = tracker.addClientObject(proxied); // Remember the client ID, in case there are subsequent commands which refer to it if (clientObjects == null) clientObjects = new HashMap<Integer, Proxied>(); clientObjects.put(clientId, proxied); // Tell the client about the new ID - do this before changing properties tracker.invalidateCache(proxied); tracker.getQueue().queueCommand(CommandId.CommandType.MAP_CLIENT_ID, proxied, null, new MapClientId(serverId, clientId)); // Set property values if (jp.nextToken() == JsonToken.FIELD_NAME) { if (jp.nextToken() != JsonToken.START_OBJECT) throw new ServletException("Unexpected properties definiton for 'new' command"); while (jp.nextToken() != JsonToken.END_OBJECT) { String propertyName = jp.getCurrentName(); jp.nextToken(); // Read a Proxied object? ProxyProperty prop = getProperty(type, propertyName); MetaClass propClass = prop.getPropertyClass(); Object value = null; if (propClass.isSubclassOf(Proxied.class)) { if (propClass.isArray() || propClass.isCollection()) { value = readArray(jp, propClass.getJavaType()); } else if (propClass.isMap()) { value = readMap(jp, propClass.getKeyClass(), propClass.getJavaType()); } else { Integer id = jp.readValueAs(Integer.class); if (id != null) value = getProxied(id); } } else { value = jp.readValueAs(Object.class); if (value != null && Enum.class.isAssignableFrom(propClass.getJavaType())) { String str = Helpers.camelCaseToEnum(value.toString()); value = Enum.valueOf(propClass.getJavaType(), str); } } setPropertyValue(type, proxied, propertyName, value); } } // Done jp.nextToken(); }
From source file:org.apache.hadoop.hbase.rest.TestTableScan.java
@Test public void testStreamingJSON() throws Exception { //Test with start row and end row. StringBuilder builder = new StringBuilder(); builder.append("/*"); builder.append("?"); builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1); builder.append("&"); builder.append(Constants.SCAN_START_ROW + "=aaa"); builder.append("&"); builder.append(Constants.SCAN_END_ROW + "=aay"); Response response = client.get("/" + TABLE + builder.toString(), Constants.MIMETYPE_JSON); assertEquals(200, response.getCode()); int count = 0; ObjectMapper mapper = new JacksonJaxbJsonProvider().locateMapper(CellSetModel.class, MediaType.APPLICATION_JSON_TYPE); JsonFactory jfactory = new JsonFactory(mapper); JsonParser jParser = jfactory.createJsonParser(response.getStream()); boolean found = false; while (jParser.nextToken() != JsonToken.END_OBJECT) { if (jParser.getCurrentToken() == JsonToken.START_OBJECT && found) { RowModel row = jParser.readValueAs(RowModel.class); assertNotNull(row.getKey()); for (int i = 0; i < row.getCells().size(); i++) { if (count == 0) { assertEquals("aaa", Bytes.toString(row.getKey())); }//from w w w . java 2 s. com if (count == 23) { assertEquals("aax", Bytes.toString(row.getKey())); } count++; } jParser.skipChildren(); } else { found = jParser.getCurrentToken() == JsonToken.START_ARRAY; } } assertEquals(24, count); }
From source file:org.apache.olingo.client.core.edm.xml.ActionDeserializer.java
@Override protected ActionImpl doDeserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException, JsonProcessingException { final ActionImpl action = new ActionImpl(); for (; jp.getCurrentToken() != JsonToken.END_OBJECT; jp.nextToken()) { final JsonToken token = jp.getCurrentToken(); if (token == JsonToken.FIELD_NAME) { if ("Name".equals(jp.getCurrentName())) { action.setName(jp.nextTextValue()); } else if ("IsBound".equals(jp.getCurrentName())) { action.setBound(BooleanUtils.toBoolean(jp.nextTextValue())); } else if ("EntitySetPath".equals(jp.getCurrentName())) { action.setEntitySetPath(jp.nextTextValue()); } else if ("Parameter".equals(jp.getCurrentName())) { jp.nextToken();/*from w ww . j a v a 2 s. c o m*/ action.getParameters().add(jp.readValueAs(ParameterImpl.class)); } else if ("ReturnType".equals(jp.getCurrentName())) { action.setReturnType(parseReturnType(jp, "Action")); } else if ("Annotation".equals(jp.getCurrentName())) { jp.nextToken(); action.getAnnotations().add(jp.readValueAs(AnnotationImpl.class)); } } } return action; }
From source file:org.apache.olingo.client.core.edm.xml.annotation.DynamicAnnotationExpressionDeserializer.java
private AbstractAnnotationExpression parseConstOrEnumExpression(final JsonParser jp) throws IOException { AbstractAnnotationExpression result; if (isAnnotationConstExprConstruct(jp)) { result = parseAnnotationConstExprConstruct(jp); } else {// w ww. j a v a 2 s. c o m result = jp.readValueAs(AbstractDynamicAnnotationExpression.class); } jp.nextToken(); return result; }
From source file:org.apache.olingo.client.core.edm.xml.annotation.DynamicAnnotationExpressionDeserializer.java
@Override protected AbstractDynamicAnnotationExpression doDeserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException, JsonProcessingException { AbstractDynamicAnnotationExpression expression = null; if ("Not".equals(jp.getCurrentName())) { final NotImpl not = new NotImpl(); jp.nextToken();//from w w w . j a va2 s . c o m for (; jp.getCurrentToken() != JsonToken.FIELD_NAME; jp.nextToken()) { } not.setExpression(jp.readValueAs(AbstractDynamicAnnotationExpression.class)); for (; jp.getCurrentToken() != JsonToken.END_OBJECT || !jp.getCurrentName().equals("Not"); jp .nextToken()) { } expression = not; } else if (TwoParamsOpDynamicAnnotationExpression.Type.fromString(jp.getCurrentName()) != null) { final TwoParamsOpDynamicAnnotationExpressionImpl dynExprDoubleParamOp = new TwoParamsOpDynamicAnnotationExpressionImpl(); dynExprDoubleParamOp .setType(TwoParamsOpDynamicAnnotationExpression.Type.fromString(jp.getCurrentName())); jp.nextToken(); for (; jp.getCurrentToken() != JsonToken.FIELD_NAME; jp.nextToken()) { } dynExprDoubleParamOp.setLeftExpression(jp.readValueAs(AbstractDynamicAnnotationExpression.class)); dynExprDoubleParamOp.setRightExpression(jp.readValueAs(AbstractDynamicAnnotationExpression.class)); for (; jp.getCurrentToken() != JsonToken.END_OBJECT || !jp.getCurrentName().equals(dynExprDoubleParamOp.getType().name()); jp.nextToken()) { } expression = dynExprDoubleParamOp; } else if (ArrayUtils.contains(EL_OR_ATTR, jp.getCurrentName())) { final AbstractElementOrAttributeExpression elOrAttr = getElementOrAttributeExpressio( jp.getCurrentName()); elOrAttr.setValue(jp.nextTextValue()); expression = elOrAttr; } else if (APPLY.equals(jp.getCurrentName())) { jp.nextToken(); expression = jp.readValueAs(ApplyImpl.class); } else if (CAST.equals(jp.getCurrentName())) { jp.nextToken(); expression = jp.readValueAs(CastImpl.class); } else if (COLLECTION.equals(jp.getCurrentName())) { jp.nextToken(); expression = jp.readValueAs(CollectionImpl.class); } else if (IF.equals(jp.getCurrentName())) { jp.nextToken(); jp.nextToken(); final IfImpl _if = new IfImpl(); _if.setGuard(parseConstOrEnumExpression(jp)); _if.setThen(parseConstOrEnumExpression(jp)); _if.setElse(parseConstOrEnumExpression(jp)); expression = _if; } else if (IS_OF.equals(jp.getCurrentName())) { jp.nextToken(); expression = jp.readValueAs(IsOfImpl.class); } else if (LABELED_ELEMENT.equals(jp.getCurrentName())) { jp.nextToken(); expression = jp.readValueAs(LabeledElementImpl.class); } else if (NULL.equals(jp.getCurrentName())) { jp.nextToken(); expression = jp.readValueAs(NullImpl.class); } else if (RECORD.equals(jp.getCurrentName())) { jp.nextToken(); expression = jp.readValueAs(RecordImpl.class); } else if (URL_REF.equals(jp.getCurrentName())) { jp.nextToken(); expression = jp.readValueAs(UrlRefImpl.class); } return expression; }
From source file:org.apache.olingo.client.core.edm.xml.ComplexTypeDeserializer.java
@Override protected AbstractComplexType doDeserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException, JsonProcessingException { final AbstractComplexType complexType = ODataServiceVersion.V30 == version ? new org.apache.olingo.client.core.edm.xml.v3.ComplexTypeImpl() : new org.apache.olingo.client.core.edm.xml.v4.ComplexTypeImpl(); for (; jp.getCurrentToken() != JsonToken.END_OBJECT; jp.nextToken()) { final JsonToken token = jp.getCurrentToken(); if (token == JsonToken.FIELD_NAME) { if ("Name".equals(jp.getCurrentName())) { complexType.setName(jp.nextTextValue()); } else if ("Abstract".equals(jp.getCurrentName())) { ((org.apache.olingo.client.core.edm.xml.v4.ComplexTypeImpl) complexType) .setAbstractEntityType(BooleanUtils.toBoolean(jp.nextTextValue())); } else if ("BaseType".equals(jp.getCurrentName())) { ((org.apache.olingo.client.core.edm.xml.v4.ComplexTypeImpl) complexType) .setBaseType(jp.nextTextValue()); } else if ("OpenType".equals(jp.getCurrentName())) { ((org.apache.olingo.client.core.edm.xml.v4.ComplexTypeImpl) complexType) .setOpenType(BooleanUtils.toBoolean(jp.nextTextValue())); } else if ("Property".equals(jp.getCurrentName())) { jp.nextToken();/*ww w . ja v a 2 s . c om*/ if (complexType instanceof org.apache.olingo.client.core.edm.xml.v3.ComplexTypeImpl) { ((org.apache.olingo.client.core.edm.xml.v3.ComplexTypeImpl) complexType).getProperties() .add(jp.readValueAs(org.apache.olingo.client.core.edm.xml.v3.PropertyImpl.class)); } else { ((org.apache.olingo.client.core.edm.xml.v4.ComplexTypeImpl) complexType).getProperties() .add(jp.readValueAs(org.apache.olingo.client.core.edm.xml.v4.PropertyImpl.class)); } } else if ("NavigationProperty".equals(jp.getCurrentName())) { jp.nextToken(); ((org.apache.olingo.client.core.edm.xml.v4.ComplexTypeImpl) complexType) .getNavigationProperties().add(jp.readValueAs( org.apache.olingo.client.core.edm.xml.v4.NavigationPropertyImpl.class)); } else if ("Annotation".equals(jp.getCurrentName())) { jp.nextToken(); ((org.apache.olingo.client.core.edm.xml.v4.ComplexTypeImpl) complexType).getAnnotations() .add(jp.readValueAs(AnnotationImpl.class)); } } } return complexType; }
From source file:org.apache.olingo.client.core.edm.xml.EntityContainerDeserializer.java
@Override protected AbstractEntityContainer doDeserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException, JsonProcessingException { final AbstractEntityContainer entityContainer = ODataServiceVersion.V30 == version ? new org.apache.olingo.client.core.edm.xml.v3.EntityContainerImpl() : new org.apache.olingo.client.core.edm.xml.v4.EntityContainerImpl(); for (; jp.getCurrentToken() != JsonToken.END_OBJECT; jp.nextToken()) { final JsonToken token = jp.getCurrentToken(); if (token == JsonToken.FIELD_NAME) { if ("Name".equals(jp.getCurrentName())) { entityContainer.setName(jp.nextTextValue()); } else if ("Extends".equals(jp.getCurrentName())) { entityContainer.setExtends(jp.nextTextValue()); } else if ("LazyLoadingEnabled".equals(jp.getCurrentName())) { entityContainer.setLazyLoadingEnabled(BooleanUtils.toBoolean(jp.nextTextValue())); } else if ("IsDefaultEntityContainer".equals(jp.getCurrentName())) { entityContainer.setDefaultEntityContainer(BooleanUtils.toBoolean(jp.nextTextValue())); } else if ("EntitySet".equals(jp.getCurrentName())) { jp.nextToken();//from ww w . ja v a2 s .c om if (entityContainer instanceof org.apache.olingo.client.core.edm.xml.v3.EntityContainerImpl) { ((org.apache.olingo.client.core.edm.xml.v3.EntityContainerImpl) entityContainer) .getEntitySets() .add(jp.readValueAs(org.apache.olingo.client.core.edm.xml.v3.EntitySetImpl.class)); } else { ((org.apache.olingo.client.core.edm.xml.v4.EntityContainerImpl) entityContainer) .getEntitySets() .add(jp.readValueAs(org.apache.olingo.client.core.edm.xml.v4.EntitySetImpl.class)); } } else if ("AssociationSet".equals(jp.getCurrentName())) { jp.nextToken(); ((org.apache.olingo.client.core.edm.xml.v3.EntityContainerImpl) entityContainer) .getAssociationSets().add(jp.readValueAs(AssociationSetImpl.class)); } else if ("Singleton".equals(jp.getCurrentName())) { jp.nextToken(); ((org.apache.olingo.client.core.edm.xml.v4.EntityContainerImpl) entityContainer).getSingletons() .add(jp.readValueAs(SingletonImpl.class)); } else if ("ActionImport".equals(jp.getCurrentName())) { jp.nextToken(); ((org.apache.olingo.client.core.edm.xml.v4.EntityContainerImpl) entityContainer) .getActionImports().add(jp.readValueAs(ActionImportImpl.class)); } else if ("FunctionImport".equals(jp.getCurrentName())) { jp.nextToken(); if (entityContainer instanceof org.apache.olingo.client.core.edm.xml.v3.EntityContainerImpl) { ((org.apache.olingo.client.core.edm.xml.v3.EntityContainerImpl) entityContainer) .getFunctionImports().add(jp.readValueAs( org.apache.olingo.client.core.edm.xml.v3.FunctionImportImpl.class)); } else { ((org.apache.olingo.client.core.edm.xml.v4.EntityContainerImpl) entityContainer) .getFunctionImports().add(jp.readValueAs( org.apache.olingo.client.core.edm.xml.v4.FunctionImportImpl.class)); } } else if ("Annotation".equals(jp.getCurrentName())) { jp.nextToken(); ((org.apache.olingo.client.core.edm.xml.v4.EntityContainerImpl) entityContainer) .getAnnotations().add(jp.readValueAs(AnnotationImpl.class)); } } } return entityContainer; }
From source file:org.apache.olingo.client.core.edm.xml.EntitySetDeserializer.java
@Override protected AbstractEntitySet doDeserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException, JsonProcessingException { final AbstractEntitySet entitySet = ODataServiceVersion.V30 == version ? new org.apache.olingo.client.core.edm.xml.v3.EntitySetImpl() : new org.apache.olingo.client.core.edm.xml.v4.EntitySetImpl(); for (; jp.getCurrentToken() != JsonToken.END_OBJECT; jp.nextToken()) { final JsonToken token = jp.getCurrentToken(); if (token == JsonToken.FIELD_NAME) { if ("Name".equals(jp.getCurrentName())) { entitySet.setName(jp.nextTextValue()); } else if ("EntityType".equals(jp.getCurrentName())) { entitySet.setEntityType(jp.nextTextValue()); } else if ("IncludeInServiceDocument".equals(jp.getCurrentName())) { ((org.apache.olingo.client.core.edm.xml.v4.EntitySetImpl) entitySet) .setIncludeInServiceDocument(BooleanUtils.toBoolean(jp.nextTextValue())); } else if ("NavigationPropertyBinding".equals(jp.getCurrentName())) { jp.nextToken();/*from w ww. j a v a 2 s . c o m*/ ((org.apache.olingo.client.core.edm.xml.v4.EntitySetImpl) entitySet) .getNavigationPropertyBindings() .add(jp.readValueAs(NavigationPropertyBindingImpl.class)); } else if ("Annotation".equals(jp.getCurrentName())) { jp.nextToken(); ((org.apache.olingo.client.core.edm.xml.v4.EntitySetImpl) entitySet).getAnnotations() .add(jp.readValueAs(AnnotationImpl.class)); } } } return entitySet; }
From source file:org.apache.olingo.client.core.edm.xml.EntityTypeDeserializer.java
@Override protected AbstractEntityType doDeserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException, JsonProcessingException { final AbstractEntityType entityType = ODataServiceVersion.V30 == version ? new org.apache.olingo.client.core.edm.xml.v3.EntityTypeImpl() : new org.apache.olingo.client.core.edm.xml.v4.EntityTypeImpl(); for (; jp.getCurrentToken() != JsonToken.END_OBJECT; jp.nextToken()) { final JsonToken token = jp.getCurrentToken(); if (token == JsonToken.FIELD_NAME) { if ("Name".equals(jp.getCurrentName())) { entityType.setName(jp.nextTextValue()); } else if ("Abstract".equals(jp.getCurrentName())) { entityType.setAbstractEntityType(BooleanUtils.toBoolean(jp.nextTextValue())); } else if ("BaseType".equals(jp.getCurrentName())) { entityType.setBaseType(jp.nextTextValue()); } else if ("OpenType".equals(jp.getCurrentName())) { entityType.setOpenType(BooleanUtils.toBoolean(jp.nextTextValue())); } else if ("HasStream".equals(jp.getCurrentName())) { entityType.setHasStream(BooleanUtils.toBoolean(jp.nextTextValue())); } else if ("Key".equals(jp.getCurrentName())) { jp.nextToken();/* www. ja v a2 s. com*/ entityType.setKey(jp.readValueAs(EntityKeyImpl.class)); } else if ("Property".equals(jp.getCurrentName())) { jp.nextToken(); if (entityType instanceof org.apache.olingo.client.core.edm.xml.v3.EntityTypeImpl) { ((org.apache.olingo.client.core.edm.xml.v3.EntityTypeImpl) entityType).getProperties() .add(jp.readValueAs(org.apache.olingo.client.core.edm.xml.v3.PropertyImpl.class)); } else { ((org.apache.olingo.client.core.edm.xml.v4.EntityTypeImpl) entityType).getProperties() .add(jp.readValueAs(org.apache.olingo.client.core.edm.xml.v4.PropertyImpl.class)); } } else if ("NavigationProperty".equals(jp.getCurrentName())) { jp.nextToken(); if (entityType instanceof org.apache.olingo.client.core.edm.xml.v3.EntityTypeImpl) { ((org.apache.olingo.client.core.edm.xml.v3.EntityTypeImpl) entityType) .getNavigationProperties().add(jp.readValueAs( org.apache.olingo.client.core.edm.xml.v3.NavigationPropertyImpl.class)); } else { ((org.apache.olingo.client.core.edm.xml.v4.EntityTypeImpl) entityType) .getNavigationProperties().add(jp.readValueAs( org.apache.olingo.client.core.edm.xml.v4.NavigationPropertyImpl.class)); } } else if ("Annotation".equals(jp.getCurrentName())) { jp.nextToken(); ((org.apache.olingo.client.core.edm.xml.v4.EntityTypeImpl) entityType).getAnnotations() .add(jp.readValueAs(AnnotationImpl.class)); } } } return entityType; }