List of usage examples for com.fasterxml.jackson.core JsonParser getCurrentToken
public abstract JsonToken getCurrentToken();
From source file:net.floodlightcontroller.loadbalancer.PoolsResource.java
protected LBPool jsonToPool(String json) throws IOException { if (json == null) return null; MappingJsonFactory f = new MappingJsonFactory(); JsonParser jp; LBPool pool = new LBPool(); try {// w ww .j av a2 s . c o m jp = f.createJsonParser(json); } catch (JsonParseException e) { throw new IOException(e); } jp.nextToken(); if (jp.getCurrentToken() != JsonToken.START_OBJECT) { throw new IOException("Expected START_OBJECT"); } while (jp.nextToken() != JsonToken.END_OBJECT) { if (jp.getCurrentToken() != JsonToken.FIELD_NAME) { throw new IOException("Expected FIELD_NAME"); } String n = jp.getCurrentName(); jp.nextToken(); if (jp.getText().equals("")) continue; if (n.equals("id")) { pool.id = jp.getText(); continue; } if (n.equals("tenant_id")) { pool.tenantId = jp.getText(); continue; } if (n.equals("name")) { pool.name = jp.getText(); continue; } if (n.equals("network_id")) { pool.netId = jp.getText(); continue; } if (n.equals("lb_method")) { pool.lbMethod = Short.parseShort(jp.getText()); continue; } if (n.equals("protocol")) { String tmp = jp.getText(); if (tmp.equalsIgnoreCase("TCP")) { pool.protocol = IPv4.PROTOCOL_TCP; } else if (tmp.equalsIgnoreCase("UDP")) { pool.protocol = IPv4.PROTOCOL_UDP; } else if (tmp.equalsIgnoreCase("ICMP")) { pool.protocol = IPv4.PROTOCOL_ICMP; } continue; } if (n.equals("vip_id")) { pool.vipId = jp.getText(); continue; } log.warn("Unrecognized field {} in " + "parsing Pools", jp.getText()); } jp.close(); return pool; }
From source file:net.floodlightcontroller.loadbalancer.VipsResource.java
protected LBVip jsonToVip(String json) throws IOException { if (json == null) return null; MappingJsonFactory f = new MappingJsonFactory(); JsonParser jp; LBVip vip = new LBVip(); try {/*w w w . j av a 2 s . c o m*/ jp = f.createJsonParser(json); } catch (JsonParseException e) { throw new IOException(e); } jp.nextToken(); if (jp.getCurrentToken() != JsonToken.START_OBJECT) { throw new IOException("Expected START_OBJECT"); } while (jp.nextToken() != JsonToken.END_OBJECT) { if (jp.getCurrentToken() != JsonToken.FIELD_NAME) { throw new IOException("Expected FIELD_NAME"); } String n = jp.getCurrentName(); jp.nextToken(); if (jp.getText().equals("")) continue; if (n.equals("id")) { vip.id = jp.getText(); continue; } if (n.equals("tenant_id")) { vip.tenantId = jp.getText(); continue; } if (n.equals("name")) { vip.name = jp.getText(); continue; } if (n.equals("network_id")) { vip.netId = jp.getText(); continue; } if (n.equals("protocol")) { String tmp = jp.getText(); if (tmp.equalsIgnoreCase("TCP")) { vip.protocol = IPv4.PROTOCOL_TCP; } else if (tmp.equalsIgnoreCase("UDP")) { vip.protocol = IPv4.PROTOCOL_UDP; } else if (tmp.equalsIgnoreCase("ICMP")) { vip.protocol = IPv4.PROTOCOL_ICMP; } continue; } if (n.equals("address")) { vip.address = IPv4.toIPv4Address(jp.getText()); continue; } if (n.equals("port")) { vip.port = Short.parseShort(jp.getText()); continue; } if (n.equals("pool_id")) { vip.pools.add(jp.getText()); continue; } log.warn("Unrecognized field {} in " + "parsing Vips", jp.getText()); } jp.close(); return vip; }
From source file:org.nuxeo.client.test.marshallers.DocumentMarshaller.java
protected static Document readDocument(JsonParser jp) throws IOException { String uid = null;/*from www.j a v a2 s .co m*/ String type = null; String path = null; String state = null; String versionLabel = null; String isCheckedOut = null; String lockCreated = null; String lockOwner = null; String repository = null; String changeToken = null; boolean isProxy = false; JsonToken tok = jp.nextToken(); Map<String, Object> properties = new HashMap<>(); while (tok != null && tok != JsonToken.END_OBJECT) { tok = jp.nextToken(); String key = jp.getText(); tok = jp.nextToken(); if ("uid".equals(key)) { uid = jp.getText(); } else if ("path".equals(key)) { path = jp.getText(); } else if ("type".equals(key)) { type = jp.getText(); } else if ("state".equals(key)) { state = jp.getText(); } else if ("versionLabel".equals(key)) { versionLabel = jp.getText(); } else if ("isCheckedOut".equals(key)) { isCheckedOut = jp.getText(); } else if ("lock".equals(key)) { if (!JsonToken.VALUE_NULL.equals(jp.getCurrentToken())) { String[] lock = jp.getText().split(":"); lockOwner = lock[0]; lockCreated = lock[1]; } } else if ("lockCreated".equals(key)) { lockCreated = jp.getText(); } else if ("lockOwner".equals(key)) { lockOwner = jp.getText(); } else if ("repository".equals(key)) { repository = jp.getText(); } else if ("properties".equals(key)) { readProperties(jp, properties); } else if ("changeToken".equals(key)) { changeToken = jp.getText(); } else if ("isProxy".equals(key)) { isProxy = jp.getBooleanValue(); } } return new Document(uid, type, null, changeToken, path, state, lockOwner, lockCreated, repository, versionLabel, isCheckedOut, isProxy, properties, null); }
From source file:internal.product.ProductImportResource.java
@POST @Produces(MediaType.APPLICATION_JSON)// w w w . j a va 2s . c o m @Path("/batch") public Response batch(InputStream batch, @Context HttpServletRequest request) { try { JsonParser parser = jfactory.createParser(batch); while (parser.nextToken() != JsonToken.END_OBJECT) { System.out.println(parser.getCurrentToken()); } } catch (IOException e) { return Response.status(Status.INTERNAL_SERVER_ERROR).entity(UTF8.encode(e.toString())).build(); } return Response.status(Status.OK).entity(UTF8.encode("Hello world:")).build(); }
From source file:org.elasticsearch.client.sniff.ElasticsearchNodesSniffer.java
private static Node readNode(String nodeId, JsonParser parser, Scheme scheme) throws IOException { HttpHost publishedHost = null;/*from ww w . j a va2 s .c om*/ /* * We sniff the bound hosts so we can look up the node based on any * address on which it is listening. This is useful in Elasticsearch's * test framework where we sometimes publish ipv6 addresses but the * tests contact the node on ipv4. */ Set<HttpHost> boundHosts = new HashSet<>(); String name = null; String version = null; /* * Multi-valued attributes come with key = `real_key.index` and we * unflip them after reading them because we can't rely on the order * that they arive. */ final Map<String, String> protoAttributes = new HashMap<String, String>(); boolean sawRoles = false; boolean master = false; boolean data = false; boolean ingest = false; String fieldName = null; while (parser.nextToken() != JsonToken.END_OBJECT) { if (parser.getCurrentToken() == JsonToken.FIELD_NAME) { fieldName = parser.getCurrentName(); } else if (parser.getCurrentToken() == JsonToken.START_OBJECT) { if ("http".equals(fieldName)) { while (parser.nextToken() != JsonToken.END_OBJECT) { if (parser.getCurrentToken() == JsonToken.VALUE_STRING && "publish_address".equals(parser.getCurrentName())) { URI publishAddressAsURI = URI.create(scheme + "://" + parser.getValueAsString()); publishedHost = new HttpHost(publishAddressAsURI.getHost(), publishAddressAsURI.getPort(), publishAddressAsURI.getScheme()); } else if (parser.currentToken() == JsonToken.START_ARRAY && "bound_address".equals(parser.getCurrentName())) { while (parser.nextToken() != JsonToken.END_ARRAY) { URI boundAddressAsURI = URI.create(scheme + "://" + parser.getValueAsString()); boundHosts.add(new HttpHost(boundAddressAsURI.getHost(), boundAddressAsURI.getPort(), boundAddressAsURI.getScheme())); } } else if (parser.getCurrentToken() == JsonToken.START_OBJECT) { parser.skipChildren(); } } } else if ("attributes".equals(fieldName)) { while (parser.nextToken() != JsonToken.END_OBJECT) { if (parser.getCurrentToken() == JsonToken.VALUE_STRING) { String oldValue = protoAttributes.put(parser.getCurrentName(), parser.getValueAsString()); if (oldValue != null) { throw new IOException("repeated attribute key [" + parser.getCurrentName() + "]"); } } else { parser.skipChildren(); } } } else { parser.skipChildren(); } } else if (parser.currentToken() == JsonToken.START_ARRAY) { if ("roles".equals(fieldName)) { sawRoles = true; while (parser.nextToken() != JsonToken.END_ARRAY) { switch (parser.getText()) { case "master": master = true; break; case "data": data = true; break; case "ingest": ingest = true; break; default: logger.warn("unknown role [" + parser.getText() + "] on node [" + nodeId + "]"); } } } else { parser.skipChildren(); } } else if (parser.currentToken().isScalarValue()) { if ("version".equals(fieldName)) { version = parser.getText(); } else if ("name".equals(fieldName)) { name = parser.getText(); } } } //http section is not present if http is not enabled on the node, ignore such nodes if (publishedHost == null) { logger.debug("skipping node [" + nodeId + "] with http disabled"); return null; } Map<String, List<String>> realAttributes = new HashMap<>(protoAttributes.size()); List<String> keys = new ArrayList<>(protoAttributes.keySet()); for (String key : keys) { if (key.endsWith(".0")) { String realKey = key.substring(0, key.length() - 2); List<String> values = new ArrayList<>(); int i = 0; while (true) { String value = protoAttributes.remove(realKey + "." + i); if (value == null) { break; } values.add(value); i++; } realAttributes.put(realKey, unmodifiableList(values)); } } for (Map.Entry<String, String> entry : protoAttributes.entrySet()) { realAttributes.put(entry.getKey(), singletonList(entry.getValue())); } if (version.startsWith("2.")) { /* * 2.x doesn't send roles, instead we try to read them from * attributes. */ boolean clientAttribute = v2RoleAttributeValue(realAttributes, "client", false); Boolean masterAttribute = v2RoleAttributeValue(realAttributes, "master", null); Boolean dataAttribute = v2RoleAttributeValue(realAttributes, "data", null); master = masterAttribute == null ? false == clientAttribute : masterAttribute; data = dataAttribute == null ? false == clientAttribute : dataAttribute; } else { assert sawRoles : "didn't see roles for [" + nodeId + "]"; } assert boundHosts.contains(publishedHost) : "[" + nodeId + "] doesn't make sense! publishedHost should be in boundHosts"; logger.trace("adding node [" + nodeId + "]"); return new Node(publishedHost, boundHosts, name, version, new Roles(master, data, ingest), unmodifiableMap(realAttributes)); }
From source file:org.emfjson.jackson.tests.custom.CustomDeserializersTest.java
@Test public void testDeserializeReferenceAsStrings() throws JsonProcessingException { EMFModule module = new EMFModule(); module.configure(EMFModule.Feature.OPTION_USE_ID, true); module.configure(EMFModule.Feature.OPTION_SERIALIZE_TYPE, false); module.setReferenceDeserializer(new JsonDeserializer<ReferenceEntry>() { @Override//from ww w. j a v a2 s . c om public ReferenceEntry deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { final EObject parent = EMFContext.getParent(ctxt); final EReference reference = EMFContext.getReference(ctxt); if (p.getCurrentToken() == JsonToken.FIELD_NAME) { p.nextToken(); } return new ReferenceEntry.Base(parent, reference, p.getText()); } }); mapper.registerModule(module); JsonNode data = mapper.createArrayNode() .add(mapper.createObjectNode().put("@id", "1").put("name", "Paul").put("uniqueFriend", "2")) .add(mapper.createObjectNode().put("@id", "2").put("name", "Franck")); Resource resource = mapper.reader().withAttribute(RESOURCE_SET, resourceSet) .withAttribute(ROOT_ELEMENT, ModelPackage.Literals.USER).treeToValue(data, Resource.class); assertEquals(2, resource.getContents().size()); User u1 = (User) resource.getContents().get(0); User u2 = (User) resource.getContents().get(1); assertSame(u2, u1.getUniqueFriend()); }
From source file:com.msopentech.odatajclient.engine.metadata.edm.EntitySetDeserializer.java
@Override protected AbstractEntitySet doDeserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException, JsonProcessingException { final AbstractEntitySet entitySet = ODataVersion.V3 == client.getWorkingVersion() ? new com.msopentech.odatajclient.engine.metadata.edm.v3.EntitySet() : new com.msopentech.odatajclient.engine.metadata.edm.v4.EntitySet(); 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())) { ((com.msopentech.odatajclient.engine.metadata.edm.v4.EntitySet) entitySet) .setIncludeInServiceDocument(BooleanUtils.toBoolean(jp.nextTextValue())); } else if ("NavigationPropertyBinding".equals(jp.getCurrentName())) { jp.nextToken();//w w w . jav a 2 s . c o m ((com.msopentech.odatajclient.engine.metadata.edm.v4.EntitySet) entitySet) .getNavigationPropertyBindings() .add(jp.getCodec().readValue(jp, NavigationPropertyBinding.class)); } else if ("Annotation".equals(jp.getCurrentName())) { jp.nextToken(); ((com.msopentech.odatajclient.engine.metadata.edm.v4.EntitySet) entitySet) .setAnnotation(jp.getCodec().readValue(jp, Annotation.class)); } } } return entitySet; }
From source file:com.msopentech.odatajclient.engine.metadata.edm.EnumTypeDeserializer.java
@Override protected AbstractEnumType doDeserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException, JsonProcessingException { final AbstractEnumType enumType = ODataVersion.V3 == client.getWorkingVersion() ? new com.msopentech.odatajclient.engine.metadata.edm.v3.EnumType() : new com.msopentech.odatajclient.engine.metadata.edm.v4.EnumType(); for (; jp.getCurrentToken() != JsonToken.END_OBJECT; jp.nextToken()) { final JsonToken token = jp.getCurrentToken(); if (token == JsonToken.FIELD_NAME) { if ("Name".equals(jp.getCurrentName())) { enumType.setName(jp.nextTextValue()); } else if ("UnderlyingType".equals(jp.getCurrentName())) { enumType.setUnderlyingType(jp.nextTextValue()); } else if ("IsFlags".equals(jp.getCurrentName())) { enumType.setFlags(BooleanUtils.toBoolean(jp.nextTextValue())); } else if ("Member".equals(jp.getCurrentName())) { jp.nextToken();//from ww w . j a va2s. com if (enumType instanceof com.msopentech.odatajclient.engine.metadata.edm.v3.EnumType) { ((com.msopentech.odatajclient.engine.metadata.edm.v3.EnumType) enumType).getMembers() .add(jp.getCodec().readValue(jp, com.msopentech.odatajclient.engine.metadata.edm.v3.Member.class)); } else { ((com.msopentech.odatajclient.engine.metadata.edm.v4.EnumType) enumType).getMembers() .add(jp.getCodec().readValue(jp, com.msopentech.odatajclient.engine.metadata.edm.v4.Member.class)); } } else if ("Annotation".equals(jp.getCurrentName())) { jp.nextToken(); ((com.msopentech.odatajclient.engine.metadata.edm.v4.EnumType) enumType) .setAnnotation(jp.getCodec().readValue(jp, Annotation.class)); } } } return enumType; }
From source file:net.floodlightcontroller.loadbalancer.MonitorsResource.java
protected LBMonitor jsonToMonitor(String json) throws IOException { MappingJsonFactory f = new MappingJsonFactory(); JsonParser jp; LBMonitor monitor = new LBMonitor(); try {// ww w . j a va2 s. co m jp = f.createJsonParser(json); } catch (JsonParseException e) { throw new IOException(e); } jp.nextToken(); if (jp.getCurrentToken() != JsonToken.START_OBJECT) { throw new IOException("Expected START_OBJECT"); } while (jp.nextToken() != JsonToken.END_OBJECT) { if (jp.getCurrentToken() != JsonToken.FIELD_NAME) { throw new IOException("Expected FIELD_NAME"); } String n = jp.getCurrentName(); jp.nextToken(); if (jp.getText().equals("")) continue; else if (n.equals("monitor")) { while (jp.nextToken() != JsonToken.END_OBJECT) { String field = jp.getCurrentName(); if (field.equals("id")) { monitor.id = jp.getText(); continue; } if (field.equals("name")) { monitor.name = jp.getText(); continue; } if (field.equals("type")) { monitor.type = Short.parseShort(jp.getText()); continue; } if (field.equals("delay")) { monitor.delay = Short.parseShort(jp.getText()); continue; } if (field.equals("timeout")) { monitor.timeout = Short.parseShort(jp.getText()); continue; } if (field.equals("attempts_before_deactivation")) { monitor.attemptsBeforeDeactivation = Short.parseShort(jp.getText()); continue; } if (field.equals("network_id")) { monitor.netId = jp.getText(); continue; } if (field.equals("address")) { monitor.address = Integer.parseInt(jp.getText()); continue; } if (field.equals("protocol")) { monitor.protocol = Byte.parseByte(jp.getText()); continue; } if (field.equals("port")) { monitor.port = Short.parseShort(jp.getText()); continue; } if (field.equals("admin_state")) { monitor.adminState = Short.parseShort(jp.getText()); continue; } if (field.equals("status")) { monitor.status = Short.parseShort(jp.getText()); continue; } log.warn("Unrecognized field {} in " + "parsing Vips", jp.getText()); } } } jp.close(); return monitor; }
From source file:org.datagator.api.client.MatrixDeserializer.java
@Override public SimpleMatrix deserialize(JsonParser jp, DeserializationContext cntx) throws IOException, JsonProcessingException { int rowsCount = -1; int columnsCount = -1; int bodyRow = -1; int bodyColumn = -1; ArrayList<ArrayList<Object>> columnHeaders = null; JsonToken token = jp.getCurrentToken(); // FIELD_NAME if (!token.equals(JsonToken.FIELD_NAME)) { throw new RuntimeException(String.format("Unexpected token %s", token)); }//from www. ja va2 s .c om while (token.equals(JsonToken.FIELD_NAME)) { String name = jp.getText(); token = jp.nextToken(); if (name.equals("columnHeaders")) { if (!token.equals(JsonToken.VALUE_NUMBER_INT)) { throw new RuntimeException(String.format("Unexpected token %s", token)); } bodyRow = jp.getIntValue(); } else if (name.equals("rowHeaders")) { if (!token.equals(JsonToken.VALUE_NUMBER_INT)) { throw new RuntimeException(String.format("Unexpected token %s", token)); } bodyColumn = jp.getIntValue(); } else if (name.equals("rows")) { if (bodyRow < 0 || bodyColumn < 0) { throw new RuntimeException( "Unexpected property order 'columnHeaders' and 'rowHeaders' should precede 'rows'."); } columnHeaders = parseRows(jp, bodyRow, bodyColumn); } else if (name.equals("rowsCount")) { if (!token.equals(JsonToken.VALUE_NUMBER_INT)) { throw new RuntimeException(String.format("Unexpected token %s", token)); } rowsCount = jp.getIntValue(); } else if (name.equals("columnsCount")) { if (!token.equals(JsonToken.VALUE_NUMBER_INT)) { throw new RuntimeException(String.format("Unexpected token %s", token)); } columnsCount = jp.getIntValue(); } else { throw new RuntimeException(String.format("Unexpected property '%s'", name)); } token = jp.nextToken(); // FIELD_NAME } if (!(0 <= bodyRow && bodyRow <= rowsCount)) { throw new RuntimeException("Invalid Matrix shape"); } if (!(0 <= bodyColumn && bodyColumn <= columnsCount)) { throw new RuntimeException("Invalid Matrix shape"); } // special case: size of empty matrix is 1 x 0 if ((columnsCount == 0) && (rowsCount != 1)) { throw new RuntimeException("Invalid Matrix shape"); } Object[][] rows = new Object[bodyRow][]; for (int r = 0; r < bodyRow; r++) { rows[r] = columnHeaders.get(r).toArray(); } return new SimpleMatrix(bodyRow, bodyColumn, rows, rowsCount, columnsCount); }