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:com.cedarsoft.serialization.test.performance.JacksonTest.java

@Before
public void setUp() throws Exception {
    jsonFactory = new JsonFactory();
}

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

@Test
public void testConstructorJsonFactory() {
    assertNotNull(new EnsemblRestClientFactory(new JsonFactory()));
}

From source file:org.culturegraph.mf.stream.converter.JsonEncoder.java

public JsonEncoder() {
    try {/*from   w w w. ja v a2s. c om*/
        jsonGenerator = new JsonFactory().createGenerator(writer);
        jsonGenerator.setRootValueSeparator(null);
    } catch (final IOException e) {
        throw new MetafactureException(e);
    }
}

From source file:org.wikimedia.analytics.kraken.schemas.JsonToClassConverter.java

/**
 * @param className refers to the name of the class that maps to the JSON file. Make sure that
 * all properties in the JSON file are defined in the Java class, else it will throw an error
 * @param file contains the name of the JSON file to be loaded. The default place to put this
 * file is in the src/main/resource folder
 * @param key name of the field from the JSON object that should be used as key to store the
 * JSON object in the HashMap. Suppose the field containing the key is called 'foo' then the
 * java Class should have a getter called getFoo.
 * @return//w  w  w.j a v  a2 s . co  m
 * @throws JsonMappingException
 * @throws JsonParseException
 */
public final HashMap<String, Schema> construct(final String className, final String file, final String key)
        throws JsonMappingException, JsonParseException {
    JsonFactory jfactory = new JsonFactory();
    HashMap<String, Schema> map = new HashMap<String, Schema>();
    List<Schema> schemas = null;
    InputStream input;
    JavaType type;
    ObjectMapper mapper = new ObjectMapper();

    try {
        Schema schema = (Schema) Schema.class.getClassLoader().loadClass(className).newInstance();

        input = schema.getClass().getClassLoader().getResourceAsStream(file);

        type = mapper.getTypeFactory().constructCollectionType(List.class, schema.getClass());
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    } catch (InstantiationException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }

    try {
        JsonParser jParser = jfactory.createJsonParser(input);
        schemas = mapper.readValue(jParser, type);
    } catch (IOException e) {
        System.err.println("Specified file could not be found.");
    } finally {
        try {
            if (input != null) {
                input.close();
            }
        } catch (IOException e) {
            System.err.println("Could not close filestream");
        }
    }
    if (schemas != null) {
        for (Schema schemaInstance : schemas) {
            try {
                Method getKey = schemaInstance.getClass().getMethod(key);
                map.put(getKey.invoke(schemaInstance).toString(), schemaInstance);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            } catch (NoSuchMethodException e) {
                System.err.println("Specified key is not a valid Getter for " + className);
            }
        }
    }
    return map;
}

From source file:org.seedstack.seed.core.internal.data.DataManagerImpl.java

DataManagerImpl() {
    this.jsonFactory = new JsonFactory();
    this.objectMapper = new ObjectMapper();
    this.jsonFactory.setCodec(this.objectMapper);
}

From source file:io.syndesis.jsondb.impl.JsonRecordSupport.java

public static void jsonStreamToRecords(String dbPath, InputStream is, Consumer<JsonRecord> consumer)
        throws IOException {
    try (JsonParser jp = new JsonFactory().createParser(is)) {
        jsonStreamToRecords(jp, dbPath, consumer);

        JsonToken jsonToken = jp.nextToken();
        if (jsonToken != null) {
            throw new JsonParseException(jp, "Document did not terminate as expected.");
        }/*ww  w.  j  a v a  2s .  c  o m*/
    }
}

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

@Test
public void testArray2() throws IOException {
    StringWriter writer = new StringWriter();
    JacksonStreamTarget target = new JacksonStreamTarget(new JsonFactory().createGenerator(writer));

    target.startObject();/*from w ww. j  av  a2s .  c o  m*/
    target.name("alice");
    target.startObject();
    target.name("bob");
    target.startArray();
    target.value("edgar");
    target.value("charlie");
    target.endArray();
    target.endObject();
    target.endObject();

    target.close();

    Assert.assertEquals("{\"alice\":{\"bob\":[\"edgar\",\"charlie\"]}}", writer.toString());
}

From source file:com.cedarsoft.couchdb.DesignDocumentsUpdater.java

/**
 * Creates the json content for the design document
 *
 * @return a string containing the json content for this design document
 *
 * @throws IOException//  ww  w .  j  av  a 2  s.  c o  m
 */
public static String createJson(@Nonnull DesignDocument designDocument, @Nullable Revision revision)
        throws IOException {
    //noinspection TypeMayBeWeakened
    StringWriter writer = new StringWriter();
    JsonGenerator generator = new JsonFactory().createJsonGenerator(writer);
    generator.writeStartObject();

    generator.writeStringField("_id", designDocument.getId());
    if (revision != null) {
        generator.writeStringField("_rev", revision.asString());
    }
    generator.writeStringField("language", "javascript");

    generator.writeObjectFieldStart("views");

    for (View view : designDocument.getViews()) {
        generator.writeObjectFieldStart(view.getName());

        generator.writeStringField("map", view.getMappingFunction());
        @Nullable
        String reduceFunction = view.getReduceFunction();
        if (reduceFunction != null) {
            generator.writeStringField("reduce", reduceFunction);
        }
        generator.writeEndObject();
    }

    generator.writeEndObject();
    generator.writeEndObject();
    generator.flush();
    return writer.toString();
}

From source file:com.ibm.ws.lars.rest.FrontPage.java

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setContentType(MediaType.APPLICATION_JSON);
    resp.setCharacterEncoding(StandardCharsets.UTF_8.name());
    PrintWriter printWriter = resp.getWriter();

    List<AssetFilter> filters = new ArrayList<>();
    filters.add(new AssetFilter("state",
            Arrays.asList(new Condition[] { new Condition(Operation.EQUALS, "published") })));
    int assetCount = serviceLayer.countAllAssets(filters, null);

    JsonGenerator frontPageJsonGenerator = new JsonFactory().createGenerator(printWriter);
    frontPageJsonGenerator.setPrettyPrinter(new DefaultPrettyPrinter());

    frontPageJsonGenerator.writeStartObject();
    frontPageJsonGenerator.writeStringField("serverName", "LARS");
    frontPageJsonGenerator.writeNumberField("assetCount", assetCount);
    frontPageJsonGenerator.writeEndObject();

    frontPageJsonGenerator.flush();/*from  w ww. j  a  v a 2 s . c  o  m*/
    frontPageJsonGenerator.close();
}