Search in sources :

Example 61 with JsonParser

use of com.fasterxml.jackson.core.JsonParser in project opentsdb by OpenTSDB.

the class TestJSON method parseToStreamUTFSByte.

// parseToStream - Byte
@Test
public void parseToStreamUTFSByte() throws Exception {
    JsonParser jp = JSON.parseToStream("{\"utf\":\"aƩriennes\",\"ascii\":\"aariennes\"}".getBytes("UTF8"));
    HashMap<String, String> map = this.parseToMap(jp);
    assertEquals("aƩriennes", map.get("utf"));
    assertEquals("aariennes", map.get("ascii"));
}
Also used : JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.junit.Test)

Example 62 with JsonParser

use of com.fasterxml.jackson.core.JsonParser in project opentsdb by OpenTSDB.

the class TestJSON method parseToStreamASCIIByte.

@Test
public void parseToStreamASCIIByte() throws Exception {
    JsonParser jp = JSON.parseToStream("{\"utf\":\"aeriennes\",\"ascii\":\"aariennes\"}".getBytes());
    HashMap<String, String> map = this.parseToMap(jp);
    assertEquals("aeriennes", map.get("utf"));
    assertEquals("aariennes", map.get("ascii"));
}
Also used : JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.junit.Test)

Example 63 with JsonParser

use of com.fasterxml.jackson.core.JsonParser in project opentsdb by OpenTSDB.

the class TestJSON method parseToMap.

/** Helper to parse an input stream into a map */
private HashMap<String, String> parseToMap(final InputStream is) throws Exception {
    JsonParser jp = JSON.parseToStream(is);
    HashMap<String, String> map = new HashMap<String, String>();
    String field = "";
    String value;
    while (jp.nextToken() != null) {
        if (jp.getCurrentToken() == JsonToken.FIELD_NAME && jp.getCurrentName() != null) {
            field = jp.getCurrentName();
        } else if (jp.getCurrentToken() == JsonToken.VALUE_STRING) {
            value = jp.getText();
            map.put(field, value);
        }
    }
    return map;
}
Also used : HashMap(java.util.HashMap) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 64 with JsonParser

use of com.fasterxml.jackson.core.JsonParser in project beam by apache.

the class TestPipeline method convertToArgs.

public static String[] convertToArgs(PipelineOptions options) {
    try {
        byte[] opts = MAPPER.writeValueAsBytes(options);
        JsonParser jsonParser = MAPPER.getFactory().createParser(opts);
        TreeNode node = jsonParser.readValueAsTree();
        ObjectNode optsNode = (ObjectNode) node.get("options");
        ArrayList<String> optArrayList = new ArrayList<>();
        Iterator<Entry<String, JsonNode>> entries = optsNode.fields();
        while (entries.hasNext()) {
            Entry<String, JsonNode> entry = entries.next();
            if (entry.getValue().isNull()) {
                continue;
            } else if (entry.getValue().isTextual()) {
                optArrayList.add("--" + entry.getKey() + "=" + entry.getValue().asText());
            } else {
                optArrayList.add("--" + entry.getKey() + "=" + entry.getValue());
            }
        }
        return optArrayList.toArray(new String[optArrayList.size()]);
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) Entry(java.util.Map.Entry) TreeNode(com.fasterxml.jackson.core.TreeNode) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 65 with JsonParser

use of com.fasterxml.jackson.core.JsonParser in project CzechIdMng by bcvsolutions.

the class IdentityReportRenderer method render.

@Override
public InputStream render(RptReportDto report) {
    try {
        // read json stream
        JsonParser jParser = getMapper().getFactory().createParser(getReportData(report));
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("Report");
        // header
        Row row = sheet.createRow(0);
        Cell cell = row.createCell(0);
        cell.setCellValue("Id");
        cell = row.createCell(1);
        cell.setCellValue("Username");
        cell = row.createCell(2);
        cell.setCellValue("First name");
        cell = row.createCell(3);
        cell.setCellValue("Last name");
        cell = row.createCell(4);
        cell.setCellValue("Disabled");
        int rowNum = 1;
        // json is array of identities
        if (jParser.nextToken() == JsonToken.START_ARRAY) {
            // write single identity
            while (jParser.nextToken() == JsonToken.START_OBJECT) {
                IdmIdentityDto identity = getMapper().readValue(jParser, IdmIdentityDto.class);
                row = sheet.createRow(rowNum++);
                cell = row.createCell(0);
                cell.setCellValue(identity.getId().toString());
                cell = row.createCell(1);
                cell.setCellValue(identity.getUsername());
                cell = row.createCell(2);
                cell.setCellValue(identity.getFirstName());
                cell = row.createCell(3);
                cell.setCellValue(identity.getLastName());
                cell = row.createCell(4);
                cell.setCellValue(identity.isDisabled());
            }
        }
        // close json stream
        jParser.close();
        // close and return input stream
        return getInputStream(workbook);
    } catch (IOException ex) {
        throw new ReportRenderException(report.getName(), ex);
    }
}
Also used : XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) ReportRenderException(eu.bcvsolutions.idm.rpt.api.exception.ReportRenderException) Row(org.apache.poi.ss.usermodel.Row) IOException(java.io.IOException) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) Cell(org.apache.poi.ss.usermodel.Cell) JsonParser(com.fasterxml.jackson.core.JsonParser)

Aggregations

JsonParser (com.fasterxml.jackson.core.JsonParser)144 IOException (java.io.IOException)43 Test (org.junit.Test)35 JsonFactory (com.fasterxml.jackson.core.JsonFactory)26 StringWriter (java.io.StringWriter)17 JsonNode (com.fasterxml.jackson.databind.JsonNode)15 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)15 ExtensibleJSONWriter (com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter)15 JsonToken (com.fasterxml.jackson.core.JsonToken)14 JsonUtil.createJsonParser (com.facebook.presto.util.JsonUtil.createJsonParser)12 SqlNullable (com.facebook.presto.spi.function.SqlNullable)11 SqlType (com.facebook.presto.spi.function.SqlType)11 BaseTest (com.fasterxml.jackson.core.BaseTest)11 UTF8DataInputJsonParser (com.fasterxml.jackson.core.json.UTF8DataInputJsonParser)11 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)10 JsonParseException (com.fasterxml.jackson.core.JsonParseException)9 SimpleParseUUT (com.instagram.common.json.annotation.processor.uut.SimpleParseUUT)7 PrestoException (com.facebook.presto.spi.PrestoException)6 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)6 ScalarOperator (com.facebook.presto.spi.function.ScalarOperator)5