Search in sources :

Example 26 with JsonParser

use of org.apache.flink.shaded.jackson2.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 27 with JsonParser

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser in project airlift by airlift.

the class SmileMapper method readFrom.

@Override
public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream inputStream) throws IOException {
    Object object;
    try {
        JsonParser jsonParser = new SmileFactory().createParser(inputStream);
        // Important: we are NOT to close the underlying stream after
        // mapping, so we need to instruct parser:
        jsonParser.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE);
        object = objectMapper.readValue(jsonParser, objectMapper.getTypeFactory().constructType(genericType));
    } catch (Exception e) {
        // we want to return a 400 for bad JSON but not for a real IO exception
        if (e instanceof IOException && !(e instanceof JsonProcessingException) && !(e instanceof EOFException)) {
            throw (IOException) e;
        }
        // log the exception at debug so it can be viewed during development
        // Note: we are not logging at a higher level because this could cause a denial of service
        log.debug(e, "Invalid json for Java type %s", type);
        // Invalid json request. Throwing exception so the response code can be overridden using a mapper.
        throw new JsonMapperParsingException(type, e);
    }
    return object;
}
Also used : SmileFactory(com.fasterxml.jackson.dataformat.smile.SmileFactory) EOFException(java.io.EOFException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) EOFException(java.io.EOFException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 28 with JsonParser

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser in project nifi by apache.

the class DataPacketDto method getDataPacketStream.

public static Stream<DataPacket> getDataPacketStream(InputStream inputStream) throws IOException {
    JsonParser jsonParser = new JsonFactory().createParser(inputStream);
    if (jsonParser.nextToken() != JsonToken.START_ARRAY) {
        throw new IOException("Expecting start array token to begin object array.");
    }
    jsonParser.setCodec(new ObjectMapper());
    return StreamSupport.stream(Spliterators.spliteratorUnknownSize(new Iterator<DataPacket>() {

        DataPacket next = getNext();

        @Override
        public boolean hasNext() {
            return next != null;
        }

        @Override
        public DataPacket next() {
            DataPacket next = this.next;
            this.next = getNext();
            return next;
        }

        DataPacket getNext() throws RuntimeException {
            try {
                if (jsonParser.nextToken() == JsonToken.END_ARRAY) {
                    return null;
                }
                DataPacketDto dataPacketDto = jsonParser.readValueAs(DATA_PACKET_DTO_TYPE_REFERENCE);
                return dataPacketDto.toDataPacket();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }, Spliterator.ORDERED), false);
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) Iterator(java.util.Iterator) IOException(java.io.IOException) DataPacket(org.apache.nifi.remote.protocol.DataPacket) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 29 with JsonParser

use of org.apache.flink.shaded.jackson2.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 30 with JsonParser

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser in project spring-security-oauth by spring-projects.

the class JwkSetConverter method convert.

/**
 * Converts the supplied <code>InputStream</code> to a <code>Set</code> of {@link JwkDefinition}(s).
 *
 * @param jwkSetSource the source for the JWK Set
 * @return a <code>Set</code> of {@link JwkDefinition}(s)
 * @throws JwkException if the JWK Set JSON object is invalid
 */
@Override
public Set<JwkDefinition> convert(InputStream jwkSetSource) {
    Set<JwkDefinition> jwkDefinitions;
    JsonParser parser = null;
    try {
        parser = this.factory.createParser(jwkSetSource);
        if (parser.nextToken() != JsonToken.START_OBJECT) {
            throw new JwkException("Invalid JWK Set Object.");
        }
        if (parser.nextToken() != JsonToken.FIELD_NAME) {
            throw new JwkException("Invalid JWK Set Object.");
        }
        if (!parser.getCurrentName().equals(KEYS)) {
            throw new JwkException("Invalid JWK Set Object. The JWK Set MUST have a " + KEYS + " attribute.");
        }
        if (parser.nextToken() != JsonToken.START_ARRAY) {
            throw new JwkException("Invalid JWK Set Object. The JWK Set MUST have an array of JWK(s).");
        }
        jwkDefinitions = new LinkedHashSet<JwkDefinition>();
        Map<String, String> attributes = new HashMap<String, String>();
        while (parser.nextToken() == JsonToken.START_OBJECT) {
            while (parser.nextToken() == JsonToken.FIELD_NAME) {
                String attributeName = parser.getCurrentName();
                // gh-1082 - skip arrays such as x5c as we can't deal with them yet
                if (parser.nextToken() == JsonToken.START_ARRAY) {
                    while (parser.nextToken() != JsonToken.END_ARRAY) {
                    }
                } else {
                    attributes.put(attributeName, parser.getValueAsString());
                }
            }
            JwkDefinition jwkDefinition = null;
            JwkDefinition.KeyType keyType = JwkDefinition.KeyType.fromValue(attributes.get(KEY_TYPE));
            if (JwkDefinition.KeyType.RSA.equals(keyType)) {
                jwkDefinition = this.createRsaJwkDefinition(attributes);
            } else if (JwkDefinition.KeyType.EC.equals(keyType)) {
                jwkDefinition = this.createEllipticCurveJwkDefinition(attributes);
            }
            if (jwkDefinition != null) {
                if (!jwkDefinitions.add(jwkDefinition)) {
                    throw new JwkException("Duplicate JWK found in Set: " + jwkDefinition.getKeyId() + " (" + KEY_ID + ")");
                }
            }
            attributes.clear();
        }
    } catch (IOException ex) {
        throw new JwkException("An I/O error occurred while reading the JWK Set: " + ex.getMessage(), ex);
    } finally {
        try {
            if (parser != null)
                parser.close();
        } catch (IOException ex) {
        }
    }
    return jwkDefinitions;
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Aggregations

JsonParser (com.fasterxml.jackson.core.JsonParser)587 KriptonRuntimeException (com.abubusoft.kripton.exception.KriptonRuntimeException)258 JacksonWrapperParser (com.abubusoft.kripton.persistence.JacksonWrapperParser)258 KriptonJsonContext (com.abubusoft.kripton.KriptonJsonContext)257 ArrayList (java.util.ArrayList)171 IOException (java.io.IOException)126 JsonFactory (com.fasterxml.jackson.core.JsonFactory)76 Test (org.junit.Test)57 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)51 JsonNode (com.fasterxml.jackson.databind.JsonNode)46 HashSet (java.util.HashSet)43 JsonToken (com.fasterxml.jackson.core.JsonToken)41 LinkedHashSet (java.util.LinkedHashSet)38 HashMap (java.util.HashMap)35 LinkedList (java.util.LinkedList)26 JsonParseException (com.fasterxml.jackson.core.JsonParseException)23 JsonUtil.createJsonParser (com.facebook.presto.util.JsonUtil.createJsonParser)21 List (java.util.List)21 DeserializationContext (com.fasterxml.jackson.databind.DeserializationContext)20 InputStream (java.io.InputStream)20