Search in sources :

Example 11 with JsonParser

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
            }
        }
    }
}
Also used : GwtTestException(com.googlecode.gwt.test.exceptions.GwtTestException) GwtTestJSONException(com.googlecode.gwt.test.exceptions.GwtTestJSONException) IOException(java.io.IOException) GwtTestException(com.googlecode.gwt.test.exceptions.GwtTestException) IOException(java.io.IOException) GwtTestJSONException(com.googlecode.gwt.test.exceptions.GwtTestJSONException) JsonParseException(org.codehaus.jackson.JsonParseException) JsonParser(org.codehaus.jackson.JsonParser)

Example 12 with JsonParser

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);
}
Also used : Response(org.apache.hadoop.hbase.rest.client.Response) CellSetModel(org.apache.hadoop.hbase.rest.model.CellSetModel) JsonFactory(org.codehaus.jackson.JsonFactory) RowModel(org.apache.hadoop.hbase.rest.model.RowModel) JacksonJaxbJsonProvider(org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) JsonParser(org.codehaus.jackson.JsonParser) Test(org.junit.Test)

Example 13 with JsonParser

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);
}
Also used : ReceiveMessageRequest(com.amazonaws.services.sqs.model.ReceiveMessageRequest) Message(com.amazonaws.services.sqs.model.Message) JsonFactory(org.codehaus.jackson.JsonFactory) JsonNode(org.codehaus.jackson.JsonNode) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Date(java.util.Date) JsonParser(org.codehaus.jackson.JsonParser)

Example 14 with JsonParser

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;
}
Also used : JSONObject(org.json.JSONObject) JsonFactory(org.codehaus.jackson.JsonFactory) JSONObject(org.json.JSONObject) IOException(java.io.IOException) File(java.io.File) JsonParser(org.codehaus.jackson.JsonParser)

Example 15 with JsonParser

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);
}
Also used : JsonFactory(org.codehaus.jackson.JsonFactory) ArrayList(java.util.ArrayList) JsonParser(org.codehaus.jackson.JsonParser)

Aggregations

JsonParser (org.codehaus.jackson.JsonParser)21 JsonFactory (org.codehaus.jackson.JsonFactory)13 IOException (java.io.IOException)6 JsonParseException (org.codehaus.jackson.JsonParseException)5 ArrayList (java.util.ArrayList)4 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)4 List (java.util.List)3 GwtTestException (com.googlecode.gwt.test.exceptions.GwtTestException)2 GwtTestJSONException (com.googlecode.gwt.test.exceptions.GwtTestJSONException)2 File (java.io.File)2 Map (java.util.Map)2 PlanJSONDumpGenerator (org.apache.flink.optimizer.plandump.PlanJSONDumpGenerator)2 JsonNode (org.codehaus.jackson.JsonNode)2 JsonToken (org.codehaus.jackson.JsonToken)2 TypeReference (org.codehaus.jackson.type.TypeReference)2 Message (com.amazonaws.services.sqs.model.Message)1 ReceiveMessageRequest (com.amazonaws.services.sqs.model.ReceiveMessageRequest)1 PatchMethod (com.googlecode.gwt.test.patchers.PatchMethod)1 HarRequest (edu.umass.cs.benchlab.har.HarRequest)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1