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;
}
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;
}
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);
}
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;
}
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;
}
Aggregations