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:nl.esciencecenter.xnatclient.data.XnatParser.java

public static int parseJsonResult(XnatObjectType type, String jsonStr, List list) throws XnatParseException {
    if (StringUtil.isEmpty(jsonStr))
        return 0;
    try {/*from   w  w w.  j  a v a  2  s  .c o m*/
        JsonFactory jsonFac = new JsonFactory();
        ObjectMapper mapper = new ObjectMapper();

        // use dom like parsing:
        JsonNode tree = mapper.readTree(jsonStr);
        JsonNode rootNode = null;

        JsonNode resultSet = tree.get("ResultSet");
        if (resultSet == null) {
            logger.warnPrintf("Couldn't find 'ResultSet' in jsonTree\n");
            // return 0;
        }

        JsonNode result = resultSet.get("Result");
        if (result == null) {
            logger.warnPrintf("Couldn't find 'Result' in jsonTree\n");
            return 0;
        }

        if (result.isArray() == false) {
            // logger.warnPrintf("Couldn't find 'Result' in jsonTree\n");
            return 0;
        }
        rootNode = result;

        // parse objects:
        Iterator<JsonNode> els = rootNode.elements();
        while (els.hasNext()) {
            JsonNode el = els.next();
            list.add(parseXnatObject(type, el));
        }
    }
    // wrap exception:
    catch (JsonParseException e) {
        throw new XnatParseException("Couldn't parse result:\n" + jsonStr + "\n---\n" + e.getMessage(), e);
    } catch (IOException e) {
        throw new XnatParseException("IOException:" + e.getMessage(), e);
    }

    return list.size();
}

From source file:com.mozilla.bagheera.validation.Validator.java

public Validator(final String[] validNamespaces) {
    if (validNamespaces == null || validNamespaces.length == 0) {
        throw new IllegalArgumentException("No valid namespace was specified");
    }/*from  ww  w  .jav a 2  s.com*/
    StringBuilder nsPatternBuilder = new StringBuilder("(");
    int i = 0, size = validNamespaces.length;
    for (String name : validNamespaces) {
        nsPatternBuilder.append(name.replaceAll("\\*", ".+"));
        if ((i + 1) < size) {
            nsPatternBuilder.append("|");
        }
        i++;
    }
    nsPatternBuilder.append(")");
    LOG.info("Namespace pattern: " + nsPatternBuilder.toString());
    validNamespacePattern = Pattern.compile(nsPatternBuilder.toString());

    jsonFactory = new JsonFactory();
}

From source file:nl.knaw.huygens.timbuctoo.rest.providers.HTMLProviderHelper.java

public HTMLProviderHelper(TypeRegistry registry, String stylesheetLink, String publicURL) {
    this.registry = registry;
    writers = Maps.newHashMap();//from  w ww  . j a va 2s .  c o m
    factory = new JsonFactory();
    preamble = createPreamble(publicURL, stylesheetLink);
}

From source file:com.baidubce.util.JsonUtils.java

public static JsonGenerator jsonGeneratorOf(Writer writer) throws IOException {
    return new JsonFactory().createGenerator(writer);
}

From source file:org.eclipse.winery.repository.resources.entitytypes.nodetypes.ImplementationsOfOneNodeTypeResource.java

/**
 * required by implementations.jsp/*w ww .ja  va 2s  .com*/
 *
 * @return for each node type implementation implementing the associated
 *         node type
 */
@Override
public String getImplementationsTableData() {
    String res;
    JsonFactory jsonFactory = new JsonFactory();
    StringWriter tableDataSW = new StringWriter();
    try {
        JsonGenerator jGenerator = jsonFactory.createGenerator(tableDataSW);
        jGenerator.writeStartArray();

        Collection<NodeTypeImplementationId> allNodeTypeImplementations = BackendUtils
                .getAllElementsRelatedWithATypeAttribute(NodeTypeImplementationId.class,
                        this.getTypeId().getQName());
        for (NodeTypeImplementationId ntiID : allNodeTypeImplementations) {
            jGenerator.writeStartArray();
            jGenerator.writeString(ntiID.getNamespace().getDecoded());
            jGenerator.writeString(ntiID.getXmlId().getDecoded());
            jGenerator.writeEndArray();
        }
        jGenerator.writeEndArray();
        jGenerator.close();
        tableDataSW.close();
        res = tableDataSW.toString();
    } catch (Exception e) {
        ImplementationsOfOneNodeTypeResource.LOGGER.error(e.getMessage(), e);
        res = "[]";
    }
    return res;
}

From source file:com.buildria.mocking.serializer.JacksonJsonSerializer.java

@Override
public byte[] serialize(@Nonnull Object obj) throws IOException {
    Objects.requireNonNull(obj);/*from  w  w  w  .  ja va 2  s.  c  o  m*/

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    JsonEncoding encoding = mappingFrom(ctx.getCharset());
    try (JsonGenerator g = new JsonFactory().createGenerator(out, encoding)) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.writeValue(g, obj);
    }

    return out.toByteArray();
}

From source file:edu.usd.btl.REST.CategoriesResource.java

@Path("/test")
@GET/*  w  w  w  . jav a  2s . c o m*/
@Produces("application/json")
public String getTestJson() throws Exception {
    // Create the node factory that gives us nodes.
    JsonNodeFactory factory = new JsonNodeFactory(false);

    // create a json factory to write the treenode as json. for the example
    // we just write to console
    JsonFactory jsonFactory = new JsonFactory();
    JsonGenerator generator = jsonFactory.createGenerator(System.out);
    ObjectMapper mapper = new ObjectMapper();

    // the root node - album
    JsonNode album = factory.objectNode();
    mapper.writeTree(generator, album);

    return "null";
}

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

@Test
public void testArrayValue() throws IOException {
    StringReader reader = new StringReader("[\"bob\"]");
    JacksonStreamSource source = new JacksonStreamSource(new JsonFactory().createParser(reader));

    Assert.assertEquals(JsonStreamToken.START_ARRAY, source.peek());
    source.startArray();//from  w w w  .j  a  va 2  s.co m

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

    Assert.assertEquals(JsonStreamToken.END_ARRAY, source.peek());
    source.endArray();

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

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

@Test(expected = NullPointerException.class)
public void testConstructorJsonFactoryNullDefaultServerUrl() {
    new EnsemblRestClientFactory(null, new JsonFactory());
}