Search in sources :

Example 11 with JsonFactory

use of com.fasterxml.jackson.core.JsonFactory in project wiquery by WiQuery.

the class AutocompleteComponent method onBeforeRenderAutocomplete.

@Override
protected void onBeforeRenderAutocomplete(Autocomplete<?> autocomplete) {
    StringWriter sw = new StringWriter();
    try {
        JsonGenerator gen = new JsonFactory().createGenerator(sw);
        List<Object> json = new ArrayList<Object>();
        T defaultValue = AutocompleteComponent.this.getModelObject();
        AutocompleteJson value = null;
        Integer index = 0;
        for (T obj : AutocompleteComponent.this.list.getObject()) {
            index++;
            value = newAutocompleteJson(index, obj);
            json.add(value);
            if (obj.equals(defaultValue)) {
                autocomplete.setDefaultModelObject(value.getLabel());
                getAutocompleteHidden().setModelObject(value.getValueId());
            }
        }
        new ObjectMapper().writeValue(gen, json);
    } catch (IOException e) {
        throw new WicketRuntimeException(e);
    }
    autocomplete.getOptions().put("source", sw.toString());
}
Also used : StringWriter(java.io.StringWriter) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) JsonFactory(com.fasterxml.jackson.core.JsonFactory) ArrayList(java.util.ArrayList) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 12 with JsonFactory

use of com.fasterxml.jackson.core.JsonFactory in project fastjson by alibaba.

the class JacksonPageModelParser method parse.

/**
     * @param content
     * @throws JsonParseException
     * @throws IOException
     */
public PageInstance parse(String content) throws JsonParseException, IOException {
    JsonFactory f = new JsonFactory();
    JsonParser parser = f.createJsonParser(content);
    // move to the start of the
    JsonToken current = parser.nextToken();
    // object
    // get instanceId
    String instanceId = getNextTextValue("sid", parser);
    // get pageId
    String pageId = getNextTextValue("cid", parser);
    // move to field: segments
    current = parser.nextToken();
    assertExpectedFiled(parser.getCurrentName(), "segments", parser.getCurrentLocation());
    PageInstance pageInstance = new PageInstance();
    pageInstance.setCid(pageId);
    pageInstance.setSid(Long.valueOf(instanceId));
    pageInstance.setSegments(parseSegments(parser));
    return pageInstance;
// 构建组件树,用于递归渲染
// pageInstance.buildComponentTree();
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonToken(com.fasterxml.jackson.core.JsonToken) JsonParserHelper.assertExpectedJsonToken(com.alibaba.json.test.performance.JacksonPageModelParser.JsonParserHelper.assertExpectedJsonToken) PageInstance(com.alibaba.json.test.entity.pagemodel.PageInstance) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 13 with JsonFactory

use of com.fasterxml.jackson.core.JsonFactory in project jmxtrans by jmxtrans.

the class SensuWriter2Test method metricsAreFormattedCorrectly.

@Test
public void metricsAreFormattedCorrectly() throws IOException {
    StringWriter writer = new StringWriter();
    SensuWriter2 sensuWriter = new SensuWriter2(new GraphiteWriter2(ImmutableList.<String>of(), null), new JsonFactory());
    sensuWriter.write(writer, ServerFixtures.dummyServer(), QueryFixtures.dummyQuery(), ResultFixtures.dummyResults());
    assertThat(writer.toString()).isEqualTo("{\n" + "  \"name\" : \"jmxtrans\",\n" + "  \"type\" : \"metric\",\n" + "  \"handler\" : \"graphite\",\n" + "  \"output\" : \"host_example_net_4321.MemoryAlias.ObjectPendingFinalizationCount 10 0\\n\"\n" + "}");
}
Also used : StringWriter(java.io.StringWriter) JsonFactory(com.fasterxml.jackson.core.JsonFactory) Test(org.junit.Test)

Example 14 with JsonFactory

use of com.fasterxml.jackson.core.JsonFactory in project rest.li by linkedin.

the class TestJacksonCodec method testNoStringIntern.

/**
   * Test to make sure that field names are not interned.
   *
   * @throws IOException
   */
@Test
public void testNoStringIntern() throws IOException {
    final String keyName = "testKey";
    final String json = "{ \"" + keyName + "\" : 1 }";
    final byte[] jsonAsBytes = json.getBytes(Data.UTF_8_CHARSET);
    {
        final JsonFactory jsonFactory = new JsonFactory();
        final JacksonDataCodec codec = new JacksonDataCodec(jsonFactory);
        // make sure intern field names is not enabled
        assertFalse(jsonFactory.isEnabled(JsonFactory.Feature.INTERN_FIELD_NAMES));
        assertTrue(jsonFactory.isEnabled(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES));
        final DataMap map = codec.bytesToMap(jsonAsBytes);
        final String key = map.keySet().iterator().next();
        assertNotSame(key, keyName);
    }
    {
        final JsonFactory jsonFactory = new JsonFactory();
        final JacksonDataCodec codec = new JacksonDataCodec(jsonFactory);
        // enable intern field names
        jsonFactory.enable(JsonFactory.Feature.INTERN_FIELD_NAMES);
        assertTrue(jsonFactory.isEnabled(JsonFactory.Feature.INTERN_FIELD_NAMES));
        assertTrue(jsonFactory.isEnabled(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES));
        final DataMap map = codec.bytesToMap(jsonAsBytes);
        final String key = map.keySet().iterator().next();
        assertSame(key, keyName);
    }
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 15 with JsonFactory

use of com.fasterxml.jackson.core.JsonFactory in project pinpoint by naver.

the class AgentTimeHistogramTest method testViewModel.

@Test
public void testViewModel() throws IOException {
    Application app = new Application("test", ServiceType.STAND_ALONE);
    AgentTimeHistogramBuilder builder = new AgentTimeHistogramBuilder(app, new Range(0, 1000 * 60));
    List<ResponseTime> responseHistogramList = createResponseTime(app, "test1", "test2");
    AgentTimeHistogram histogram = builder.build(responseHistogramList);
    List<AgentResponseTimeViewModel> viewModel = histogram.createViewModel();
    logger.debug("{}", viewModel);
    JsonFactory jsonFactory = mapper.getFactory();
    StringWriter stringWriter = new StringWriter();
    JsonGenerator jsonGenerator = jsonFactory.createGenerator(stringWriter);
    jsonGenerator.writeStartObject();
    for (AgentResponseTimeViewModel agentResponseTimeViewModel : viewModel) {
        jsonGenerator.writeObject(agentResponseTimeViewModel);
    }
    jsonGenerator.writeEndObject();
    jsonGenerator.flush();
    jsonGenerator.close();
    logger.debug(stringWriter.toString());
}
Also used : AgentTimeHistogramBuilder(com.navercorp.pinpoint.web.applicationmap.histogram.AgentTimeHistogramBuilder) AgentTimeHistogram(com.navercorp.pinpoint.web.applicationmap.histogram.AgentTimeHistogram) AgentResponseTimeViewModel(com.navercorp.pinpoint.web.view.AgentResponseTimeViewModel) StringWriter(java.io.StringWriter) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ResponseTime(com.navercorp.pinpoint.web.vo.ResponseTime) Range(com.navercorp.pinpoint.web.vo.Range) Application(com.navercorp.pinpoint.web.vo.Application) Test(org.junit.Test)

Aggregations

JsonFactory (com.fasterxml.jackson.core.JsonFactory)101 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)39 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)33 Test (org.junit.Test)31 StringWriter (java.io.StringWriter)29 JsonParser (com.fasterxml.jackson.core.JsonParser)24 ExtensibleJSONWriter (com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter)15 IOException (java.io.IOException)14 Map (java.util.Map)14 HashMap (java.util.HashMap)11 SimpleParseUUT (com.instagram.common.json.annotation.processor.uut.SimpleParseUUT)8 ArrayList (java.util.ArrayList)8 List (java.util.List)8 FileInputStream (java.io.FileInputStream)6 ServletOutputStream (javax.servlet.ServletOutputStream)6 Reader (java.io.Reader)5 RemoteSession (org.apache.jackrabbit.oak.remote.RemoteSession)5 File (java.io.File)4 JSONWriter (org.json.JSONWriter)4 BaseJsonHttpResponseHandler (com.loopj.android.http.BaseJsonHttpResponseHandler)3