Search in sources :

Example 56 with JsonParser

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

the class NumberCoercionTest method testToBigIntegerCoercion.

public void testToBigIntegerCoercion() throws Exception {
    for (int mode : ALL_STREAMING_MODES) {
        JsonParser p;
        p = createParser(mode, "1");
        assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
        // int to BigInteger
        assertEquals(1, p.getIntValue());
        assertEquals(BigInteger.ONE, p.getBigIntegerValue());
        p.close();
        p = createParser(mode, "2.0");
        assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
        // double to BigInteger
        assertEquals(2.0, p.getDoubleValue());
        assertEquals(BigInteger.valueOf(2L), p.getBigIntegerValue());
        p.close();
        p = createParser(mode, String.valueOf(Long.MAX_VALUE));
        assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
        // long to BigInteger
        assertEquals(Long.MAX_VALUE, p.getLongValue());
        assertEquals(BigInteger.valueOf(Long.MAX_VALUE), p.getBigIntegerValue());
        p.close();
        p = createParser(mode, " 200.0");
        assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
        // BigDecimal to BigInteger
        assertEquals(new BigDecimal("200.0"), p.getDecimalValue());
        assertEquals(BigInteger.valueOf(200L), p.getBigIntegerValue());
        p.close();
    }
}
Also used : BigDecimal(java.math.BigDecimal) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 57 with JsonParser

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

the class CandlepinAttributeDeserializer method deserialize.

@Override
public Map<String, String> deserialize(JsonParser parser, DeserializationContext context) throws IOException, JsonProcessingException {
    Map<String, String> output = new HashMap<>();
    TreeNode node = parser.readValueAsTree();
    if (node.isObject()) {
        log.debug("Processing attributes as a mapping of key/value pairs");
        // This is what we want, key/value pairs (hopefully).
        for (Iterator<String> fieldNames = node.fieldNames(); fieldNames.hasNext(); ) {
            String field = fieldNames.next();
            TreeNode valueNode = node.get(field);
            if (!valueNode.isValueNode()) {
                throw new CandlepinJsonProcessingException("Unexpected value type in map: " + valueNode.asToken(), parser.getCurrentLocation());
            }
            JsonParser subparser = valueNode.traverse();
            subparser.nextValue();
            String value = subparser.getValueAsString();
            subparser.close();
            log.debug("Found key/value pair: {} = {}", field, value);
            output.put(field, value);
        }
    } else if (node.isArray()) {
        log.debug("Processing attributes as an array of attribute objects");
        // Probably old collection of objects containing name/value attribute.
        // Iterate over the objects, adding the values to the map.
        int size = node.size();
        for (int i = 0; i < size; ++i) {
            TreeNode obj = node.get(i);
            if (!obj.isObject()) {
                throw new CandlepinJsonProcessingException("Unexpected value type in array: " + obj.asToken(), parser.getCurrentLocation());
            }
            TreeNode fieldNode = obj.get("name");
            if (fieldNode == null) {
                throw new CandlepinJsonProcessingException("No attribute name defined in attribute object", parser.getCurrentLocation());
            }
            if (!fieldNode.isValueNode()) {
                throw new CandlepinJsonProcessingException("Unexpected value type for attribute name: " + fieldNode.asToken(), parser.getCurrentLocation());
            }
            JsonParser subparser = fieldNode.traverse();
            subparser.nextValue();
            String field = subparser.getValueAsString();
            subparser.close();
            TreeNode valueNode = obj.get("value");
            if (valueNode != null) {
                if (!valueNode.isValueNode()) {
                    throw new CandlepinJsonProcessingException("Unexpected value type for attribute value: " + valueNode.asToken(), parser.getCurrentLocation());
                }
                subparser = valueNode.traverse();
                subparser.nextValue();
                String value = subparser.getValueAsString();
                subparser.close();
                log.debug("Found key/value pair: {} = {}", field, value);
                output.put(field, value);
            } else {
                log.debug("Found key/value pair: {} = {}", field, null);
                output.put(field, null);
            }
        }
    } else {
        log.debug("Processing attributes as an array of attribute objects");
        // Uh oh.
        throw new CandlepinJsonProcessingException("Unexpected attribute value type: " + node.asToken(), parser.getCurrentLocation());
    }
    return output;
}
Also used : CandlepinJsonProcessingException(org.candlepin.common.exceptions.CandlepinJsonProcessingException) HashMap(java.util.HashMap) TreeNode(com.fasterxml.jackson.core.TreeNode) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 58 with JsonParser

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser in project jackson-jaxrs-propertyfiltering by HubSpot.

the class PropertyFilteringMessageBodyWriter method valueToTree.

private JsonNode valueToTree(ObjectMapper mapper, ObjectWriter writer, Object o) {
    if (o == null) {
        return null;
    }
    TokenBuffer buf = new TokenBuffer(mapper, false);
    JsonNode result;
    try {
        writer.writeValue(buf, o);
        JsonParser jp = buf.asParser();
        result = mapper.readTree(jp);
        jp.close();
    } catch (IOException e) {
        // should not occur, no real i/o...
        throw new IllegalArgumentException(e.getMessage(), e);
    }
    return result;
}
Also used : TokenBuffer(com.fasterxml.jackson.databind.util.TokenBuffer) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 59 with JsonParser

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

the class JSONLDPersistenceKit method doReadModel.

protected IEntity doReadModel(IPersistenceProvider pp) throws Exception {
    JsonFactory factory = new JsonFactory();
    JsonParser parser = factory.createParser(pp.getInputStream());
    parser.disable(Feature.AUTO_CLOSE_SOURCE);
    try {
        return new JSONLDEntityDecoder().clone(new JSONParserTemplateFactory(parser).create());
    } finally {
        parser.close();
    }
}
Also used : JSONLDEntityDecoder(org.whole.lang.json.util.JSONLDEntityDecoder) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JSONParserTemplateFactory(org.whole.lang.json.util.JSONParserTemplateFactory) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 60 with JsonParser

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

the class EditableDeserializerTest method deserialise.

/**
 * @param stream the input stream containing JSON
 * @return map of the deserialised JSON
 * @see <a href="https://stackoverflow.com/a/37001410/379565">this Stack Overflow post</a>
 */
@SneakyThrows({ JsonParseException.class, IOException.class })
private Map<String, Object> deserialise(final InputStream stream) {
    JsonParser parser = mapper.getFactory().createParser(stream);
    DeserializationContext ctxt = mapper.getDeserializationContext();
    parser.nextToken();
    parser.nextToken();
    parser.nextToken();
    return deserializer.deserialize(parser, ctxt);
}
Also used : DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) JsonParser(com.fasterxml.jackson.core.JsonParser) SneakyThrows(lombok.SneakyThrows)

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)39 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