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