Java Code Examples for com.fasterxml.jackson.core.JsonFactory#createJsonParser()

The following examples show how to use com.fasterxml.jackson.core.JsonFactory#createJsonParser() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestJsonParser.java    From geometry-api-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testGeometryToJSONOldID() throws Exception {// CR
	Polygon geom = new Polygon();
	geom.startPath(new Point(-113, 34));
	geom.lineTo(new Point(-105, 34));
	geom.lineTo(new Point(-108, 40));
	String outputPolygon = GeometryEngine.geometryToJson(SpatialReference.create(3857), geom);// Test
																								// WKID
																								// ==
																								// -1
	String correctPolygon = "{\"rings\":[[[-113,34],[-105,34],[-108,40],[-113,34]]],\"spatialReference\":{\"wkid\":102100,\"latestWkid\":3857}}";
	assertTrue(outputPolygon.equals(correctPolygon));
	JsonFactory jf = new JsonFactory();
	JsonParser jp = jf.createJsonParser(outputPolygon);
	jp.nextToken();
	MapGeometry mg = GeometryEngine.jsonToGeometry(jp);
	@SuppressWarnings("unused")
	int srId = mg.getSpatialReference().getID();
	@SuppressWarnings("unused")
	int srOldId = mg.getSpatialReference().getOldID();
	Assert.assertTrue(mg.getSpatialReference().getID() == 3857);
	Assert.assertTrue(mg.getSpatialReference().getLatestID() == 3857);
	Assert.assertTrue(mg.getSpatialReference().getOldID() == 102100);
}
 
Example 2
Source File: ComplexTypeITCase.java    From aws-dynamodb-encryption-java with Apache License 2.0 5 votes vote down vote up
@Override
public List<ComplexNestedType> unmarshall(Class<List<ComplexNestedType>> clazz, String obj) {
    try {
        JsonFactory jsonFactory = new MappingJsonFactory();
        JsonParser jsonParser = jsonFactory.createJsonParser(new StringReader(obj));
        return jsonParser.readValueAs(new TypeReference<List<ComplexNestedType>>() {
        });
    } catch ( Exception e ) {
        throw new RuntimeException(e);
    }            
}
 
Example 3
Source File: BasicJavaClientREST.java    From java-client-api with Apache License 2.0 5 votes vote down vote up
/**
 * Get the expected JSON document
 * 
 * @param filename
 * @return
 * @throws JsonParseException
 * @throws IOException
 */
public JsonNode expectedJSONDocument(String filename) throws JsonParseException, IOException
{
  // get json document for expected result
  ObjectMapper mapper = new ObjectMapper();
  JsonFactory jfactory = new JsonFactory();
  JsonParser jParser = jfactory.createJsonParser(new File("src/test/java/com/marklogic/client/functionaltest/data/" + filename));
  JsonNode expectedDoc = mapper.readTree(jParser);
  return expectedDoc;
}
 
Example 4
Source File: BasicJavaClientREST.java    From java-client-api with Apache License 2.0 5 votes vote down vote up
/**
 * Get the expected JSON query option
 * 
 * @param filename
 * @return
 * @throws JsonParseException
 * @throws IOException
 */
public JsonNode expectedJSONQueryOption(String filename) throws JsonParseException, IOException
{
  // get json document for expected result
  ObjectMapper mapper = new ObjectMapper();
  JsonFactory jfactory = new JsonFactory();
  JsonParser jParser = jfactory.createJsonParser(new File("src/test/java/com/marklogic/client/functionaltest/queryoptions/" + filename));
  JsonNode expectedDoc = mapper.readTree(jParser);
  return expectedDoc;
}
 
Example 5
Source File: ImportUtils.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public static void importUsersFromStream(KeycloakSession session, String realmName, ObjectMapper mapper, InputStream is) throws IOException {
    RealmProvider model = session.realms();
    JsonFactory factory = mapper.getJsonFactory();
    JsonParser parser = factory.createJsonParser(is);
    try {
        parser.nextToken();

        while (parser.nextToken() == JsonToken.FIELD_NAME) {
            if ("realm".equals(parser.getText())) {
                parser.nextToken();
                String currRealmName = parser.getText();
                if (!currRealmName.equals(realmName)) {
                    throw new IllegalStateException("Trying to import users into invalid realm. Realm name: " + realmName + ", Expected realm name: " + currRealmName);
                }
            } else if ("users".equals(parser.getText())) {
                parser.nextToken();

                if (parser.getCurrentToken() == JsonToken.START_ARRAY) {
                    parser.nextToken();
                }

                // TODO: support for more transactions per single users file (if needed)
                List<UserRepresentation> userReps = new ArrayList<UserRepresentation>();
                while (parser.getCurrentToken() == JsonToken.START_OBJECT) {
                    UserRepresentation user = parser.readValueAs(UserRepresentation.class);
                    userReps.add(user);
                    parser.nextToken();
                }

                importUsers(session, model, realmName, userReps);

                if (parser.getCurrentToken() == JsonToken.END_ARRAY) {
                    parser.nextToken();
                }
            }
        }
    } finally {
        parser.close();
    }
}
 
Example 6
Source File: ImportUtils.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public static void importFederatedUsersFromStream(KeycloakSession session, String realmName, ObjectMapper mapper, InputStream is) throws IOException {
    RealmProvider model = session.realms();
    JsonFactory factory = mapper.getJsonFactory();
    JsonParser parser = factory.createJsonParser(is);
    try {
        parser.nextToken();

        while (parser.nextToken() == JsonToken.FIELD_NAME) {
            if ("realm".equals(parser.getText())) {
                parser.nextToken();
                String currRealmName = parser.getText();
                if (!currRealmName.equals(realmName)) {
                    throw new IllegalStateException("Trying to import users into invalid realm. Realm name: " + realmName + ", Expected realm name: " + currRealmName);
                }
            } else if ("federatedUsers".equals(parser.getText())) {
                parser.nextToken();

                if (parser.getCurrentToken() == JsonToken.START_ARRAY) {
                    parser.nextToken();
                }

                // TODO: support for more transactions per single users file (if needed)
                List<UserRepresentation> userReps = new ArrayList<UserRepresentation>();
                while (parser.getCurrentToken() == JsonToken.START_OBJECT) {
                    UserRepresentation user = parser.readValueAs(UserRepresentation.class);
                    userReps.add(user);
                    parser.nextToken();
                }

                importFederatedUsers(session, model, realmName, userReps);

                if (parser.getCurrentToken() == JsonToken.END_ARRAY) {
                    parser.nextToken();
                }
            }
        }
    } finally {
        parser.close();
    }
}
 
Example 7
Source File: TestTableScan.java    From hbase with Apache License 2.0 4 votes vote down vote up
@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()));
        }
        if (count == 23) {
          assertEquals("aax", Bytes.toString(row.getKey()));
        }
        count++;
      }
      jParser.skipChildren();
    } else {
      found = jParser.getCurrentToken() == JsonToken.START_ARRAY;
    }
  }
  assertEquals(24, count);
}
 
Example 8
Source File: AwsGlacierInventoryRetriever.java    From core with GNU General Public License v3.0 4 votes vote down vote up
/**
 * For retrieving vault inventory. 
 * 
 * @param jobId
 * @param sqsQueueUrl
 * @return
 * @throws JsonParseException
 * @throws IOException
 */
private Boolean waitForJobToComplete(String jobId, String sqsQueueUrl)
		throws JsonParseException, IOException {
	logger.info("Waiting for job to complete. jobId={}", jobId);
	
	Boolean messageFound = false;
	Boolean jobSuccessful = false;
	ObjectMapper mapper = new ObjectMapper();
	JsonFactory factory = mapper.getFactory();

	while (!messageFound) {
		List<Message> msgs = sqsClient.receiveMessage(
				new ReceiveMessageRequest(sqsQueueUrl)
						.withMaxNumberOfMessages(10)).getMessages();

		if (msgs.size() > 0) {
			for (Message m : msgs) {
				@SuppressWarnings("deprecation")
				JsonParser jpMessage = factory
						.createJsonParser(m.getBody());
				JsonNode jobMessageNode = mapper.readTree(jpMessage);
				String jobMessage = jobMessageNode.get("Message")
						.textValue();

				@SuppressWarnings("deprecation")
				JsonParser jpDesc = factory.createJsonParser(jobMessage);
				JsonNode jobDescNode = mapper.readTree(jpDesc);
				String retrievedJobId = jobDescNode.get("JobId")
						.textValue();
				String statusCode = jobDescNode.get("StatusCode")
						.textValue();
				if (retrievedJobId.equals(jobId)) {
					messageFound = true;
					if (statusCode.equals("Succeeded")) {
						jobSuccessful = true;
					}
				}
			}

		} else {
			Time.sleep(sleepTimeMsec);
		}
	}
	return (messageFound && jobSuccessful);
}
 
Example 9
Source File: TestJsonParser.java    From geometry-api-java with Apache License 2.0 4 votes vote down vote up
@Test
public void testMP2onCR175871() throws Exception {
	Polygon pg = new Polygon();
	pg.startPath(-50, 10);
	pg.lineTo(-50, 12);
	pg.lineTo(-45, 12);
	pg.lineTo(-45, 10);

	Polygon pg1 = new Polygon();
	pg1.startPath(-45, 10);
	pg1.lineTo(-40, 10);
	pg1.lineTo(-40, 8);
	pg.add(pg1, false);

	SpatialReference spatialReference = SpatialReference.create(4326);

	try {
		String jSonStr = GeometryEngine.geometryToJson(spatialReference, pg);
		JsonFactory jf = new JsonFactory();

		JsonParser jp = jf.createJsonParser(jSonStr);
		jp.nextToken();
		MapGeometry mg = GeometryEngine.jsonToGeometry(jp);
		Geometry gm = mg.getGeometry();
		Assert.assertEquals(Geometry.Type.Polygon, gm.getType());
		Assert.assertTrue(mg.getSpatialReference().getID() == 4326);

		Polygon pgNew = (Polygon) gm;

		Assert.assertEquals(pgNew.getPathCount(), pg.getPathCount());
		Assert.assertEquals(pgNew.getPointCount(), pg.getPointCount());
		Assert.assertEquals(pgNew.getSegmentCount(), pg.getSegmentCount());

		Assert.assertEquals(pgNew.getPoint(0).getX(), pg.getPoint(0).getX(), 0.000000001);
		Assert.assertEquals(pgNew.getPoint(1).getX(), pg.getPoint(1).getX(), 0.000000001);
		Assert.assertEquals(pgNew.getPoint(2).getX(), pg.getPoint(2).getX(), 0.000000001);
		Assert.assertEquals(pgNew.getPoint(3).getX(), pg.getPoint(3).getX(), 0.000000001);

		Assert.assertEquals(pgNew.getPoint(0).getY(), pg.getPoint(0).getY(), 0.000000001);
		Assert.assertEquals(pgNew.getPoint(1).getY(), pg.getPoint(1).getY(), 0.000000001);
		Assert.assertEquals(pgNew.getPoint(2).getY(), pg.getPoint(2).getY(), 0.000000001);
		Assert.assertEquals(pgNew.getPoint(3).getY(), pg.getPoint(3).getY(), 0.000000001);
	} catch (Exception ex) {
		String err = ex.getMessage();
		System.out.print(err);
		throw ex;
	}
}
 
Example 10
Source File: TestJSonToGeomFromWkiOrWkt_CR177613.java    From geometry-api-java with Apache License 2.0 4 votes vote down vote up
@Test
public void testMP2onCR175871() throws Exception {
	Polygon pg = new Polygon();
	pg.startPath(-50, 10);
	pg.lineTo(-50, 12);
	pg.lineTo(-45, 12);
	pg.lineTo(-45, 10);

	Polygon pg1 = new Polygon();
	pg1.startPath(-45, 10);
	pg1.lineTo(-40, 10);
	pg1.lineTo(-40, 8);
	pg.add(pg1, false);

	try {
		String jSonStr = GeometryEngine.geometryToJson(4326, pg);
		JsonFactory jf = new JsonFactory();

		JsonParser jp = jf.createJsonParser(jSonStr);
		jp.nextToken();
		MapGeometry mg = GeometryEngine.jsonToGeometry(jp);
		Geometry gm = mg.getGeometry();
		assertEquals(Geometry.Type.Polygon, gm.getType());

		Polygon pgNew = (Polygon) gm;

		assertEquals(pgNew.getPathCount(), pg.getPathCount());
		assertEquals(pgNew.getPointCount(), pg.getPointCount());
		assertEquals(pgNew.getSegmentCount(), pg.getSegmentCount());

		assertEquals(pgNew.getPoint(0).getX(), pg.getPoint(0).getX(),
				0.000000001);
		assertEquals(pgNew.getPoint(1).getX(), pg.getPoint(1).getX(),
				0.000000001);
		assertEquals(pgNew.getPoint(2).getX(), pg.getPoint(2).getX(),
				0.000000001);
		assertEquals(pgNew.getPoint(3).getX(), pg.getPoint(3).getX(),
				0.000000001);

		assertEquals(pgNew.getPoint(0).getY(), pg.getPoint(0).getY(),
				0.000000001);
		assertEquals(pgNew.getPoint(1).getY(), pg.getPoint(1).getY(),
				0.000000001);
		assertEquals(pgNew.getPoint(2).getY(), pg.getPoint(2).getY(),
				0.000000001);
		assertEquals(pgNew.getPoint(3).getY(), pg.getPoint(3).getY(),
				0.000000001);
	} catch (Exception ex) {
		String err = ex.getMessage();
		System.out.print(err);
		throw ex;
	}
}