List of usage examples for com.fasterxml.jackson.core JsonFactory JsonFactory
public JsonFactory()
From source file:com.marklogic.client.functionaltest.TestBulkReadWriteWithJacksonParserHandle.java
@Test public void testWriteMultipleJSONDocs() throws Exception { String docId[] = { "/a.json", "/b.json", "/c.json" }; String json1 = new String("{\"animal\":\"dog\", \"says\":\"woof\"}"); String json2 = new String("{\"animal\":\"cat\", \"says\":\"meow\"}"); String json3 = new String("{\"animal\":\"rat\", \"says\":\"keek\"}"); JsonFactory f = new JsonFactory(); JSONDocumentManager docMgr = client.newJSONDocumentManager(); docMgr.setMetadataCategories(Metadata.ALL); DocumentWriteSet writeset = docMgr.newWriteSet(); JacksonParserHandle jacksonParserHandle1 = new JacksonParserHandle(); JacksonParserHandle jacksonParserHandle2 = new JacksonParserHandle(); JacksonParserHandle jacksonParserHandle3 = new JacksonParserHandle(); jacksonParserHandle1.set(f.createParser(json1)); jacksonParserHandle2.set(f.createParser(json2)); jacksonParserHandle3.set(f.createParser(json3)); writeset.add(docId[0], jacksonParserHandle1); writeset.add(docId[1], jacksonParserHandle2); writeset.add(docId[2], jacksonParserHandle3); docMgr.write(writeset);//from ww w . ja va2 s . com //Using JacksonHandle to read back from database. JacksonHandle jacksonhandle = new JacksonHandle(); docMgr.read(docId[0], jacksonhandle); JSONAssert.assertEquals(json1, jacksonhandle.toString(), true); docMgr.read(docId[1], jacksonhandle); JSONAssert.assertEquals(json2, jacksonhandle.toString(), true); docMgr.read(docId[2], jacksonhandle); JSONAssert.assertEquals(json3, jacksonhandle.toString(), true); // Close handles. jacksonParserHandle1.close(); jacksonParserHandle2.close(); jacksonParserHandle3.close(); }
From source file:com.github.lynxdb.server.api.http.handlers.EpQuery.java
private void saveResponse(OutputStream _output, List<Query> _queries) throws IOException { JsonFactory jFactory = new JsonFactory(); JsonGenerator jGenerator;//from ww w. jav a 2 s. c o m jGenerator = jFactory.createGenerator(_output, JsonEncoding.UTF8); jGenerator.writeStartArray(); for (Query q : _queries) { TimeSerie ts; ts = engine.query(q); jGenerator.writeStartObject(); jGenerator.writeStringField("metric", q.getName()); //tags jGenerator.writeObjectFieldStart("tags"); if (q.getTags() != null) { for (String tagk : q.getTags().keySet()) { jGenerator.writeStringField(tagk, q.getTags().get(tagk)); } } jGenerator.writeEndObject(); //dps jGenerator.writeObjectFieldStart("dps"); while (ts.hasNext()) { Entry e = ts.next(); jGenerator.writeNumberField(String.valueOf(e.getTime()), e.getValue()); } jGenerator.writeEndObject(); //endQuery jGenerator.writeEndObject(); } jGenerator.writeEndArray(); jGenerator.close(); _output.flush(); _output.close(); }
From source file:org.graylog.plugins.beats.BeatsFrameDecoder.java
/** * @see <a href="https://github.com/logstash-plugins/logstash-input-beats/blob/master/PROTOCOL.md#data-frame-type">'data' frame type</a> *///from www .j a v a2 s .co m private ChannelBuffer[] parseDataFrame(Channel channel, ChannelBuffer channelBuffer) throws IOException { if (channelBuffer.readableBytes() >= 8) { sequenceNum = channelBuffer.readUnsignedInt(); LOG.trace("Received sequence number {}", sequenceNum); final int pairs = Ints.saturatedCast(channelBuffer.readUnsignedInt()); final JsonFactory jsonFactory = new JsonFactory(); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try (final JsonGenerator jg = jsonFactory.createGenerator(outputStream)) { jg.writeStartObject(); for (int i = 0; i < pairs; i++) { final String key = parseDataItem(channelBuffer); final String value = parseDataItem(channelBuffer); jg.writeStringField(key, value); } jg.writeEndObject(); } final ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(outputStream.toByteArray()); sendACK(channel); return new ChannelBuffer[] { buffer }; } return null; }
From source file:com.infinities.skyport.util.JsonUtil.java
public static String toLegendJson(Throwable e) { StringWriter wtr = new StringWriter(); try {// ww w . j a va 2 s . com JsonGenerator g = new JsonFactory().createGenerator(wtr); g.writeStartArray(); g.writeStartObject(); g.writeStringField("RES", "FALSE"); g.writeStringField("REASON", e.toString()); // g.writeStringField("REASON", Objects.firstNonNull(e.getMessage(), // e.toString())); g.writeEndObject(); g.writeEndArray(); g.close(); } catch (Exception ee) { ArrayNode array = getObjectMapper().createArrayNode(); ObjectNode reason = getObjectMapper().createObjectNode(); ObjectNode status = getObjectMapper().createObjectNode(); status.put(JsonConstants.STATUS, String.valueOf("FALSE")); reason.put(JsonConstants.REASON, "an unexpected error occurred"); array.add(status).add(reason); // "[{\"RES\":\"FALSE\"}, {\"REASON\":\"an unexpected error occurred\"}]"; return array.toString(); } return wtr.toString(); }
From source file:de.odysseus.staxon.json.stream.jackson.JacksonStreamSourceTest.java
@Test public void testNumberValues() throws IOException { StringReader reader = new StringReader("[123,12e3,12E3,12.3,1.2e3,1.2E3]"); JacksonStreamSource source = new JacksonStreamSource(new JsonFactory().createParser(reader)); JsonStreamSource.Value value = null; Assert.assertEquals(JsonStreamToken.START_ARRAY, source.peek()); source.startArray();/*from ww w .j ava 2 s .c o m*/ Assert.assertEquals(JsonStreamToken.VALUE, source.peek()); value = source.value(); Assert.assertEquals("123", value.text); Assert.assertEquals(new Long("123"), value.data); Assert.assertEquals(JsonStreamToken.VALUE, source.peek()); value = source.value(); Assert.assertEquals("12e3", value.text); Assert.assertEquals(new BigDecimal("12e3"), value.data); Assert.assertEquals(JsonStreamToken.VALUE, source.peek()); value = source.value(); Assert.assertEquals("12E3", value.text); Assert.assertEquals(new BigDecimal("12E3"), value.data); Assert.assertEquals(JsonStreamToken.VALUE, source.peek()); value = source.value(); Assert.assertEquals("12.3", value.text); Assert.assertEquals(new BigDecimal("12.3"), value.data); Assert.assertEquals(JsonStreamToken.VALUE, source.peek()); value = source.value(); Assert.assertEquals("1.2e3", value.text); Assert.assertEquals(new BigDecimal("1.2e3"), value.data); Assert.assertEquals(JsonStreamToken.VALUE, source.peek()); value = source.value(); Assert.assertEquals("1.2E3", value.text); Assert.assertEquals(new BigDecimal("1.2E3"), value.data); Assert.assertEquals(JsonStreamToken.END_ARRAY, source.peek()); source.endArray(); Assert.assertEquals(JsonStreamToken.NONE, source.peek()); source.close(); }
From source file:org.apache.olingo.server.core.serializer.json.ODataJsonSerializer.java
@Override public SerializerResult metadataDocument(final ServiceMetadata serviceMetadata) throws SerializerException { OutputStream outputStream = null; SerializerException cachedException = null; try {/*w w w . j av a2 s.com*/ CircleStreamBuffer buffer = new CircleStreamBuffer(); outputStream = buffer.getOutputStream(); JsonGenerator json = new JsonFactory().createGenerator(outputStream); new MetadataDocumentJsonSerializer(serviceMetadata).writeMetadataDocument(json); json.close(); outputStream.close(); return SerializerResultImpl.with().content(buffer.getInputStream()).build(); } catch (final IOException e) { cachedException = new SerializerException(IO_EXCEPTION_TEXT, e, SerializerException.MessageKeys.IO_EXCEPTION); throw cachedException; } finally { closeCircleStreamBufferOutput(outputStream, cachedException); } }
From source file:com.apteligent.ApteligentJavaClient.java
/** * @param appID appId (string, optional): The app to retrieve data about, * @param metricType The metric to retrieve * @param duration can only be 1440 (24 hours) or 43200 (1 month) * @param groupBy TODO FILL IN THIS COMMENT * @return//from www .j a v a2 s. c o m */ public CrashSummary getErrorSparklines(String appID, CrashSummary.MetricType metricType, int duration, Sparklines.GroupBy groupBy) { String params = "{ \"params\": " + "{\"duration\": " + duration + "," + " \"graph\": \"" + metricType.toString() + "\"," + " \"appId\": \"" + appID + "\"" + ",\"groupBy\": \"" + groupBy.toString() + "\"}" + "}"; CrashSummary crashSummary = null; try { HttpsURLConnection conn = sendPostRequest(API_ERROR_SPARKLINES, params); JsonFactory jsonFactory = new JsonFactory(); JsonParser jp = jsonFactory.createParser(conn.getInputStream()); ObjectMapper mapper = getObjectMapper(); TreeNode node = mapper.readTree(jp); crashSummary = mapper.treeToValue(node.get("data"), CrashSummary.class); if (crashSummary != null) { crashSummary.setParams(appID, metricType, duration); } } catch (IOException ioex) { ioex.printStackTrace(); } return crashSummary; }
From source file:com.sdl.odata.renderer.json.writer.JsonPropertyWriterTest.java
private Map<String, Object> getMapFromJson(String json) throws IOException { Map<String, Object> map = new HashMap<>(); JsonParser jsonParser = new JsonFactory().createParser(json); jsonParser.nextToken();//from w ww. j av a 2 s . co m while (jsonParser.nextToken() != null) { String key = jsonParser.getText(); jsonParser.nextToken(); if (jsonParser.getCurrentToken() == JsonToken.START_ARRAY) { map.put(key, getJsonArray(jsonParser)); } else { map.put(key, jsonParser.getText()); } } return map; }
From source file:com.kaloer.yummly.Yummly.java
/** * Requests information about a recipe with the given id. * /*from ww w . jav a 2s.c o m*/ * @param recipeId * The id of the recipe. * @return The requested recipe. * @throws IOException * Thrown if network errors or parsing errors occur. */ public Recipe getRecipe(String recipeId) throws IOException { // Perform recipe request. InputStream in = performRequest(String.format("recipe/%s", URLEncoder.encode(recipeId, "utf8")), null); // Parse json. JsonFactory factory = new JsonFactory(); ObjectMapper mapper = new ObjectMapper(factory); Recipe result = mapper.readValue(in, Recipe.class); in.close(); return result; }