Search in sources :

Example 66 with JsonParser

use of com.fasterxml.jackson.core.JsonParser in project atlasmap by atlasmap.

the class JsonModule method getCollectionSize.

@Override
public int getCollectionSize(AtlasInternalSession session, Field field) throws AtlasException {
    // TODO could this use FieldReader?
    Object document = session.getSourceDocument(getDocId());
    // make this a JSON document
    JsonFactory jsonFactory = new JsonFactory();
    ObjectMapper objectMapper = new ObjectMapper();
    try {
        JsonParser parser = jsonFactory.createParser(document.toString());
        JsonNode rootNode = objectMapper.readTree(parser);
        ObjectNode parentNode = (ObjectNode) rootNode;
        String parentSegment = "[root node]";
        for (SegmentContext sc : new AtlasPath(field.getPath()).getSegmentContexts(false)) {
            JsonNode currentNode = JsonFieldWriter.getChildNode(parentNode, parentSegment, sc.getSegment());
            if (currentNode == null) {
                return 0;
            }
            if (AtlasPath.isCollectionSegment(sc.getSegment())) {
                if (currentNode != null && currentNode.isArray()) {
                    return currentNode.size();
                }
                return 0;
            }
            parentNode = (ObjectNode) currentNode;
        }
    } catch (IOException e) {
        throw new AtlasException(e.getMessage(), e);
    }
    return 0;
}
Also used : SegmentContext(io.atlasmap.core.AtlasPath.SegmentContext) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonFactory(com.fasterxml.jackson.core.JsonFactory) AtlasPath(io.atlasmap.core.AtlasPath) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) AtlasException(io.atlasmap.api.AtlasException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 67 with JsonParser

use of com.fasterxml.jackson.core.JsonParser in project atlasmap by atlasmap.

the class JsonFieldReader method setDocument.

public void setDocument(String document) throws AtlasException {
    if (document == null || document.isEmpty()) {
        throw new AtlasException(new IllegalArgumentException("document cannot be null nor empty"));
    }
    try {
        JsonFactory factory = new JsonFactory();
        ObjectMapper mapper = new ObjectMapper();
        JsonParser parser = factory.createParser(document);
        this.rootNode = mapper.readTree(parser);
    } catch (Exception e) {
        throw new AtlasException(e);
    }
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) AtlasException(io.atlasmap.api.AtlasException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) AtlasException(io.atlasmap.api.AtlasException) AtlasConversionException(io.atlasmap.api.AtlasConversionException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 68 with JsonParser

use of com.fasterxml.jackson.core.JsonParser in project uPortal by Jasig.

the class LayoutPortlet method isValidJSON.

private boolean isValidJSON(final String json) {
    boolean valid = false;
    try {
        final JsonParser parser = new ObjectMapper().getFactory().createParser(json);
        while (parser.nextToken() != null) {
        }
        valid = true;
    } catch (Exception jpe) {
        // eat error
        valid = false;
    }
    return valid;
}
Also used : ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 69 with JsonParser

use of com.fasterxml.jackson.core.JsonParser in project Payara by payara.

the class ProgressStatusDTOJsonProprietaryReader method readFrom.

@Override
public ProgressStatusDTO readFrom(final InputStream is, final String contentType) throws IOException {
    try (JsonParser jp = factory.createJsonParser(is)) {
        // sorounding object
        JsonToken token = jp.nextToken();
        // Name progress-status
        jp.nextToken();
        JsonToken token2 = jp.nextToken();
        if (token != JsonToken.START_OBJECT || token2 != JsonToken.START_OBJECT || !"progress-status".equals(jp.getCurrentName())) {
            throw new IOException("Not expected type (progress-status) but (" + jp.getCurrentName() + ")");
        }
        return readProgressStatus(jp);
    }
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 70 with JsonParser

use of com.fasterxml.jackson.core.JsonParser in project Payara by payara.

the class ProgressStatusEventJsonProprietaryReader method readFrom.

@Override
public ProgressStatusEvent readFrom(final InputStream is, final String contentType) throws IOException {
    try (JsonParser jp = factory.createJsonParser(is)) {
        // sorounding object
        JsonToken token = jp.nextToken();
        // Name progress-status-event
        jp.nextToken();
        JsonToken token2 = jp.nextToken();
        if (token != JsonToken.START_OBJECT || token2 != JsonToken.START_OBJECT || !"progress-status-event".equals(jp.getCurrentName())) {
            throw new IOException("Not expected type (progress-status-event) but (" + jp.getCurrentName() + ")");
        }
        return readProgressStatusEvent(jp);
    }
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Aggregations

JsonParser (com.fasterxml.jackson.core.JsonParser)144 IOException (java.io.IOException)43 Test (org.junit.Test)35 JsonFactory (com.fasterxml.jackson.core.JsonFactory)26 StringWriter (java.io.StringWriter)17 JsonNode (com.fasterxml.jackson.databind.JsonNode)15 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)15 ExtensibleJSONWriter (com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter)15 JsonToken (com.fasterxml.jackson.core.JsonToken)14 JsonUtil.createJsonParser (com.facebook.presto.util.JsonUtil.createJsonParser)12 SqlNullable (com.facebook.presto.spi.function.SqlNullable)11 SqlType (com.facebook.presto.spi.function.SqlType)11 BaseTest (com.fasterxml.jackson.core.BaseTest)11 UTF8DataInputJsonParser (com.fasterxml.jackson.core.json.UTF8DataInputJsonParser)11 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)10 JsonParseException (com.fasterxml.jackson.core.JsonParseException)9 SimpleParseUUT (com.instagram.common.json.annotation.processor.uut.SimpleParseUUT)7 PrestoException (com.facebook.presto.spi.PrestoException)6 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)6 ScalarOperator (com.facebook.presto.spi.function.ScalarOperator)5