use of org.codehaus.jackson.JsonParser in project gwt-test-utils by gwt-test-utils.
the class JsonUtilsPatcher method eval.
private static <T extends JavaScriptObject> T eval(String json) {
JsonParser jp = null;
try {
jp = getFactory().createJsonParser(json);
// will return JsonToken.START_OBJECT (verify?)
jp.nextToken();
return extractJso(json, jp.getCurrentToken(), jp).<T>cast();
} catch (Exception e) {
if (e instanceof GwtTestException) {
throw (GwtTestException) e;
}
throw new GwtTestJSONException("Error while parsing JSON string '" + json + "'", e);
} finally {
if (jp != null) {
try {
// ensure resources get cleaned up timely and properly
jp.close();
} catch (IOException e) {
// should never happen
}
}
}
}
use of org.codehaus.jackson.JsonParser in project hbase by apache.
the class TestTableScan method testStreamingJSON.
@Test
public void testStreamingJSON() throws Exception {
// Test scanning particular columns with limit.
StringBuilder builder = new StringBuilder();
builder.append("/*");
builder.append("?");
builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
builder.append("&");
builder.append(Constants.SCAN_LIMIT + "=20");
Response response = client.get("/" + TABLE + builder.toString(), Constants.MIMETYPE_JSON);
assertEquals(200, response.getCode());
assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type"));
ObjectMapper mapper = new JacksonJaxbJsonProvider().locateMapper(CellSetModel.class, MediaType.APPLICATION_JSON_TYPE);
CellSetModel model = mapper.readValue(response.getStream(), CellSetModel.class);
int count = TestScannerResource.countCellSet(model);
assertEquals(20, count);
checkRowsNotNull(model);
//Test scanning with no limit.
builder = new StringBuilder();
builder.append("/*");
builder.append("?");
builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_2);
response = client.get("/" + TABLE + builder.toString(), Constants.MIMETYPE_JSON);
assertEquals(200, response.getCode());
assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type"));
model = mapper.readValue(response.getStream(), CellSetModel.class);
count = TestScannerResource.countCellSet(model);
assertEquals(expectedRows2, count);
checkRowsNotNull(model);
//Test with start row and end row.
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 = client.get("/" + TABLE + builder.toString(), Constants.MIMETYPE_JSON);
assertEquals(200, response.getCode());
count = 0;
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);
}
use of org.codehaus.jackson.JsonParser in project glacier-cli by carlossg.
the class Glacier method waitForJobToComplete.
private Boolean waitForJobToComplete(String jobId, String sqsQueueUrl) throws InterruptedException, JsonParseException, IOException {
Boolean messageFound = false;
Boolean jobSuccessful = false;
ObjectMapper mapper = new ObjectMapper();
JsonFactory factory = mapper.getJsonFactory();
Date start = new Date();
while (!messageFound) {
long minutes = (new Date().getTime() - start.getTime()) / 1000 / 60;
System.out.println("Checking for messages: " + minutes + " elapsed");
List<Message> msgs = sqsClient.receiveMessage(new ReceiveMessageRequest(sqsQueueUrl).withMaxNumberOfMessages(10)).getMessages();
if (msgs.size() > 0) {
for (Message m : msgs) {
JsonParser jpMessage = factory.createJsonParser(m.getBody());
JsonNode jobMessageNode = mapper.readTree(jpMessage);
String jobMessage = jobMessageNode.get("Message").getTextValue();
JsonParser jpDesc = factory.createJsonParser(jobMessage);
JsonNode jobDescNode = mapper.readTree(jpDesc);
String retrievedJobId = jobDescNode.get("JobId").getTextValue();
String statusCode = jobDescNode.get("StatusCode").getTextValue();
if (retrievedJobId.equals(jobId)) {
messageFound = true;
if (statusCode.equals("Succeeded")) {
jobSuccessful = true;
}
}
}
} else {
Thread.sleep(sleepTime * 1000);
}
}
return (messageFound && jobSuccessful);
}
use of org.codehaus.jackson.JsonParser in project OpenRefine by OpenRefine.
the class JsonImporter method createParserUIInitializationData.
@Override
public JSONObject createParserUIInitializationData(ImportingJob job, List<JSONObject> fileRecords, String format) {
JSONObject options = super.createParserUIInitializationData(job, fileRecords, format);
if (fileRecords.size() > 0) {
try {
JSONObject firstFileRecord = fileRecords.get(0);
File file = ImportingUtilities.getFile(job, firstFileRecord);
JsonFactory factory = new JsonFactory();
JsonParser parser = factory.createJsonParser(file);
PreviewParsingState state = new PreviewParsingState();
Object rootValue = parseForPreview(parser, state);
if (rootValue != null) {
JSONUtilities.safePut(options, "dom", rootValue);
}
} catch (IOException e) {
logger.error("Error generating parser UI initialization data for JSON file", e);
}
}
return options;
}
use of org.codehaus.jackson.JsonParser in project OpenRefine by OpenRefine.
the class Row method loadStreaming.
public static Row loadStreaming(String s, Pool pool) throws Exception {
JsonFactory jsonFactory = new JsonFactory();
JsonParser jp = jsonFactory.createJsonParser(s);
if (jp.nextToken() != JsonToken.START_OBJECT) {
return null;
}
List<Cell> cells = new ArrayList<Cell>();
boolean starred = false;
boolean flagged = false;
while (jp.nextToken() != JsonToken.END_OBJECT) {
String fieldName = jp.getCurrentName();
jp.nextToken();
if (STARRED.equals(fieldName)) {
starred = jp.getBooleanValue();
} else if (FLAGGED.equals(fieldName)) {
flagged = jp.getBooleanValue();
} else if ("cells".equals(fieldName)) {
if (jp.getCurrentToken() != JsonToken.START_ARRAY) {
return null;
}
while (jp.nextToken() != JsonToken.END_ARRAY) {
Cell cell = Cell.loadStreaming(jp, pool);
cells.add(cell);
}
}
}
return (cells.size() > 0) ? new Row(cells, flagged, starred) : new Row(0);
}
Aggregations