Example usage for com.fasterxml.jackson.core JsonFactory JsonFactory

List of usage examples for com.fasterxml.jackson.core JsonFactory JsonFactory

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonFactory JsonFactory.

Prototype

public JsonFactory() 

Source Link

Document

Default constructor used to create factory instances.

Usage

From source file:at.ac.univie.isc.asio.tool.MediaTypeSerializerTest.java

public MediaTypeSerializerTest() throws IOException {
    sink = new StringWriter();
    generator = new JsonFactory().createGenerator(sink);
    subject = new MediaTypeSerializer();
}

From source file:com.pursuer.reader.easyrss.data.parser.ItemJSONParser.java

public ItemJSONParser(final InputStream input) throws JsonParseException, IOException {
    final JsonFactory factory = new JsonFactory();
    this.parser = factory.createJsonParser(input);
}

From source file:com.pursuer.reader.easyrss.data.parser.SubscriptionJSONParser.java

public void parse() throws JsonParseException, IOException, IllegalStateException {
    final JsonFactory factory = new JsonFactory();
    final JsonParser parser = factory.createJsonParser(input);
    Subscription sub = new Subscription();
    int level = 0;
    boolean found = false;
    while (parser.nextToken() != null) {
        final String name = parser.getCurrentName();
        switch (parser.getCurrentToken()) {
        case START_OBJECT:
        case START_ARRAY:
            level++;//from w ww. ja v a2  s  .  c  o m
            break;
        case END_OBJECT:
        case END_ARRAY:
            level--;
            break;
        case VALUE_STRING:
            if (level == 3) {
                if ("id".equals(name)) {
                    sub.setUid(parser.getText());
                } else if ("htmlUrl".equals(name)) {
                    sub.setUrl(parser.getText());
                } else if ("title".equals(name)) {
                    sub.setTitle(Html.fromHtml(parser.getText()).toString());
                } else if ("sortid".equals(name)) {
                    sub.setSortId(parser.getText());
                } else if ("firstitemmsec".equals(name)) {
                    sub.setFirstItemMsec(Long.valueOf(parser.getText()));
                }
            } else if (level == 5 && "id".equals(name)) {
                sub.addTag(parser.getText());
            }
            break;
        case FIELD_NAME:
            if (level == 1 && "subscriptions".equals(name)) {
                found = true;
            }
            break;
        default:
        }
        if (level == 2) {
            if (sub.getUid() != null && listener != null) {
                listener.onSubscriptionRetrieved(sub);
            }
            sub = new Subscription();
        }
    }
    parser.close();
    if (!found) {
        throw new IllegalStateException("Invalid JSON input");
    }
}

From source file:de.odysseus.staxon.json.stream.jackson.JacksonStreamSourceTest.java

@Test
public void testObjectValue() throws IOException {
    StringReader reader = new StringReader("{\"alice\":\"bob\"}");
    JacksonStreamSource source = new JacksonStreamSource(new JsonFactory().createParser(reader));

    Assert.assertEquals(JsonStreamToken.START_OBJECT, source.peek());
    source.startObject();/* w w  w.j  av a2s  .  c o m*/

    Assert.assertEquals(JsonStreamToken.NAME, source.peek());
    Assert.assertEquals("alice", source.name());

    Assert.assertEquals(JsonStreamToken.VALUE, source.peek());
    Assert.assertEquals("bob", source.value().text);

    Assert.assertEquals(JsonStreamToken.END_OBJECT, source.peek());
    source.endObject();

    Assert.assertEquals(JsonStreamToken.NONE, source.peek());
    source.close();
}

From source file:com.ysheng.auth.frontend.configuration.ConfigurationParser.java

/**
 * Parses the given configuration file and returns a configuration object.
 *
 * @param configurationFileName The name of the configuration file.
 * @return A configuration object./*from  w  w  w .ja v a  2s . c  o  m*/
 * @throws IOException The IO error that contains detail information.
 * @throws ConfigurationException The configuration error that contains detail information.
 */
public static ApiConfiguration parse(String configurationFileName) throws IOException, ConfigurationException {
    if (StringUtils.isBlank(configurationFileName)) {
        throw new IllegalArgumentException("Configuration file cannot be blank");
    }

    ObjectMapper objectMapper = null;
    if (configurationFileName.endsWith("yml") || configurationFileName.endsWith("yaml")) {
        objectMapper = Jackson.newObjectMapper(new YAMLFactory());
    } else if (configurationFileName.endsWith("json")) {
        objectMapper = Jackson.newObjectMapper(new JsonFactory());
    } else {
        throw new IllegalArgumentException("Unrecognized configuration file type");
    }

    ValidatorFactory validatorFactory = Validation.byProvider(HibernateValidator.class).configure()
            .addValidatedValueHandler(new OptionalValidatedValueUnwrapper()).buildValidatorFactory();

    final ConfigurationFactory<ApiConfiguration> configurationFactory = new DefaultConfigurationFactoryFactory<ApiConfiguration>()
            .create(ApiConfiguration.class, validatorFactory.getValidator(), objectMapper, "dw");

    final File file = new File(configurationFileName);
    if (!file.exists()) {
        throw new FileNotFoundException("Configuration file " + configurationFileName + " not found");
    }

    return configurationFactory.build(file);
}

From source file:com.github.heuermh.ensemblrestclient.EnsemblRestClientModule.java

@Provides
@Singleton
static JsonFactory createJsonFactory() {
    return new JsonFactory();
}

From source file:org.talend.components.dataprep.connection.DataPrepStreamMapper.java

public DataPrepStreamMapper(InputStream inputStream) throws IOException {
    this();//w  w  w .  j av  a 2s  . c  om
    this.jsonParser = new JsonFactory().createParser(inputStream);
}

From source file:com.sdl.odata.renderer.json.util.JsonWriterUtilTest.java

@Test
public void testWritePrimitiveValues() throws Exception {

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    JsonGenerator jsonGenerator = new JsonFactory().createGenerator(stream, JsonEncoding.UTF8);

    jsonGenerator.writeStartObject();//from  ww  w  .  j  a va 2s. com
    appendPrimitiveValue("MyString", "Some text", jsonGenerator);
    appendPrimitiveValue("MyByteProperty", Byte.MAX_VALUE, jsonGenerator);
    appendPrimitiveValue("MyShortProperty", (short) 1, jsonGenerator);
    appendPrimitiveValue("MyIntegerProperty", 2, jsonGenerator);
    appendPrimitiveValue("MyFloatProperty", 3.0f, jsonGenerator);
    appendPrimitiveValue("MyDoubleProperty", 4.0d, jsonGenerator);
    appendPrimitiveValue("MyLongProperty", (long) 5, jsonGenerator);
    appendPrimitiveValue("MyBooleanProperty", true, jsonGenerator);
    appendPrimitiveValue("MyUUIDProperty", UUID.fromString("23492a5b-c4f1-4a50-b7a5-d8ebd6067902"),
            jsonGenerator);
    appendPrimitiveValue("DecimalValueProperty", BigDecimal.valueOf(21), jsonGenerator);

    jsonGenerator.writeEndObject();
    jsonGenerator.close();

    assertEquals(prettyPrintJson(readContent(EXPECTED_PRIMITIVE_VALUES_PATH)),
            prettyPrintJson(stream.toString()));
}

From source file:com.wdhis.util.JacksonUtil.java

public static String bean2Json(Object obj) throws IOException {
    StringWriter sw = new StringWriter();
    JsonGenerator gen = new JsonFactory().createJsonGenerator(sw);
    mapper.writeValue(gen, obj);//  w  ww  .j  ava 2 s. c o  m
    gen.close();
    return sw.toString();
}

From source file:io.pdef.json.JsonJacksonFormat.java

private JsonJacksonFormat() {
    factory = new JsonFactory().enable(JsonParser.Feature.ALLOW_COMMENTS);
    objectFormat = JsonObjectFormat.getInstance();
}