Search in sources :

Example 41 with JsonFactory

use of com.fasterxml.jackson.core.JsonFactory in project CFLint by cflint.

the class TestCFLintConfig method test2.

@Test
public void test2() throws IOException {
    StringWriter writer = new StringWriter();
    JsonFactory jsonF = new JsonFactory();
    JsonGenerator jg = jsonF.createGenerator(writer);
    jg.writeStartArray();
    jg.writeEndArray();
    jg.close();
    writer.close();
    System.out.println(writer);
}
Also used : StringWriter(java.io.StringWriter) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) Test(org.junit.Test)

Example 42 with JsonFactory

use of com.fasterxml.jackson.core.JsonFactory in project pyramid by cheng-li.

the class Visualizer method getPositions.

private List<Integer> getPositions(Object docId, Object field, Object keywords, Object slop, Object in_order) throws IOException {
    System.out.println(docId + " " + field + " " + keywords + " " + slop + " " + in_order);
    final URL url = new URL("http://localhost:9200/ohsumed_20000/document/_search");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("POST");
    conn.setDoOutput(true);
    conn.addRequestProperty("Content-Type", "application/" + "POST");
    final String clauses = getJsonString(newSpanTerm((String) keywords, (String) field));
    final String body = "{\"explain\":\"false\"," + "                              \"query\":{" + "                                  \"filtered\":{" + "                                      \"query\":{" + "                                          \"span_near\": {" + "                                              \"clauses\":" + clauses + "," + "                                              \"slop\":" + slop.toString() + "," + "                                              \"in_order\":" + in_order.toString() + "," + "                                              \"collect_payloads\": false}}," + "                                      \"filter\":{\"ids\":{\"values\":[\"" + docId + "\"]}}}}," + "                              \"highlight\":{\"fields\":{\"" + field + "\":{}}}," + "                              \"size\":1}";
    System.out.println(body);
    conn.setRequestProperty("Content-Length", Integer.toString(body.length()));
    conn.getOutputStream().write(body.getBytes("UTF-8"));
    final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    final StringBuilder result = new StringBuilder();
    for (String line = null; (line = reader.readLine()) != null; ) {
        result.append(line);
    }
    final JsonFactory factory = new JsonFactory();
    final ObjectMapper mapper = new ObjectMapper(factory);
    final Map<String, Object> hits = mapper.readValue(result.toString(), new TypeReference<Map<String, Object>>() {
    });
    final List<Integer> positions = new ArrayList<>();
    if (hits.get("hits") != null && ((Map<String, Object>) hits.get("hits")).containsKey("hits")) {
        for (String hlField : ((Map<String, Object>) ((Map<String, Object>) ((List<Map<String, Object>>) ((Map<String, Object>) hits.get("hits")).get("hits")).get(0)).get("highlight")).keySet()) {
            final String text = (String) ((Map<String, Object>) (((List<Map<String, Object>>) ((Map<String, Object>) hits.get("hits")).get("hits")).get(0).get("_source"))).get(hlField);
            final List<String> highlights = (List<String>) ((Map<String, Object>) (((List<Map<String, Object>>) ((Map<String, Object>) hits.get("hits")).get("hits")).get(0).get("highlight"))).get(hlField);
            for (String HL : highlights) {
                String cleanHL = HL.replaceAll("<em>", "");
                cleanHL = cleanHL.replaceAll("</em>", "");
                int baseindex = text.indexOf(cleanHL);
                // in case the highlight not found in body
                if (baseindex == -1) {
                    continue;
                }
                while (HL.indexOf("<em>") != -1) {
                    int start = HL.indexOf("<em>") + baseindex;
                    HL = HL.replace("<em>", "");
                    int end = HL.indexOf("</em>") + baseindex;
                    HL = HL.replace("</em>", "");
                    positions.add(start);
                    positions.add(end);
                }
            }
        }
    }
    return positions;
}
Also used : InputStreamReader(java.io.InputStreamReader) JsonFactory(com.fasterxml.jackson.core.JsonFactory) ArrayList(java.util.ArrayList) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 43 with JsonFactory

use of com.fasterxml.jackson.core.JsonFactory in project pyramid by cheng-li.

the class Visualizer method loadJson.

private <T> T loadJson(File file, TypeReference<T> typeRef) throws IOException {
    final JsonFactory factory = new JsonFactory();
    final ObjectMapper mapper = new ObjectMapper(factory);
    return mapper.readValue(file, typeRef);
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 44 with JsonFactory

use of com.fasterxml.jackson.core.JsonFactory in project drill by apache.

the class Metadata method writeFile.

/**
   * Serialize parquet metadata to json and write to a file
   *
   * @param parquetTableMetadata
   * @param p
   * @throws IOException
   */
private void writeFile(ParquetTableMetadata_v3 parquetTableMetadata, Path p) throws IOException {
    JsonFactory jsonFactory = new JsonFactory();
    jsonFactory.configure(Feature.AUTO_CLOSE_TARGET, false);
    jsonFactory.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false);
    ObjectMapper mapper = new ObjectMapper(jsonFactory);
    SimpleModule module = new SimpleModule();
    module.addSerializer(ColumnMetadata_v3.class, new ColumnMetadata_v3.Serializer());
    mapper.registerModule(module);
    FSDataOutputStream os = fs.create(p);
    mapper.writerWithDefaultPrettyPrinter().writeValue(os, parquetTableMetadata);
    os.flush();
    os.close();
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 45 with JsonFactory

use of com.fasterxml.jackson.core.JsonFactory in project geode by apache.

the class PdxToJSON method getJSON.

public String getJSON() {
    JsonFactory jf = new JsonFactory();
    // OutputStream os = new ByteArrayOutputStream();
    HeapDataOutputStream hdos = new HeapDataOutputStream(org.apache.geode.internal.Version.CURRENT);
    try {
        JsonGenerator jg = jf.createJsonGenerator(hdos, JsonEncoding.UTF8);
        enableDisableJSONGeneratorFeature(jg);
        getJSONString(jg, m_pdxInstance);
        jg.close();
        return new String(hdos.toByteArray());
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage());
    } finally {
        hdos.close();
    }
}
Also used : HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) IOException(java.io.IOException)

Aggregations

JsonFactory (com.fasterxml.jackson.core.JsonFactory)115 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)45 Test (org.junit.Test)37 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)35 StringWriter (java.io.StringWriter)34 JsonParser (com.fasterxml.jackson.core.JsonParser)26 IOException (java.io.IOException)16 ExtensibleJSONWriter (com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter)15 Map (java.util.Map)14 HashMap (java.util.HashMap)11 ArrayList (java.util.ArrayList)9 SimpleParseUUT (com.instagram.common.json.annotation.processor.uut.SimpleParseUUT)8 List (java.util.List)8 Reader (java.io.Reader)6 ServletOutputStream (javax.servlet.ServletOutputStream)6 RemoteSession (org.apache.jackrabbit.oak.remote.RemoteSession)5 FileInputStream (java.io.FileInputStream)4 JSONWriter (org.json.JSONWriter)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 BaseJsonHttpResponseHandler (com.loopj.android.http.BaseJsonHttpResponseHandler)3