use of com.amazon.ion.IonSystem in project jackson-dataformats-binary by FasterXML.
the class IonParserTest method testParserCapabilities.
@Test
public void testParserCapabilities() throws Exception {
IonSystem ion = IonSystemBuilder.standard().build();
Integer intValue = Integer.MAX_VALUE;
IonValue ionInt = ion.newInt(intValue);
try (IonParser p = new IonFactory().createParser(ObjectReadContext.empty(), ionInt)) {
// 15-Jan-2021, tatu: 2.14 added this setting, not enabled in
// default set
Assert.assertTrue(p.streamReadCapabilities().isEnabled(StreamReadCapability.EXACT_FLOATS));
}
}
use of com.amazon.ion.IonSystem in project jackson-dataformats-binary by FasterXML.
the class DataBindReadTest method testFromIon.
/**
* Test reading an IonValue, which also happens to not be at the top level.
*/
@Test
public void testFromIon() throws IOException {
IonObjectMapper m = new IonObjectMapper();
IonSystem ion = IonSystemBuilder.standard().build();
IonValue value = ion.singleValue("{payload: {'a': bc, b : '14' }}");
MyBean bean = m.readValue(((IonStruct) value).get("payload"), MyBean.class);
assertEquals("bc", bean.a);
assertEquals(14, bean.b);
}
use of com.amazon.ion.IonSystem in project ion-hive-serde by amzn.
the class IonHiveSerDe method deserialize.
@Override
public Object deserialize(final Writable blob) throws SerDeException {
final byte[] bytes;
final int length;
// TextFormat which produces Text
if (blob instanceof Text) {
Text text = (Text) blob;
// getBytes returns a reference to the current buffer which is only valid up to length
bytes = text.getBytes();
length = text.getLength();
} else if (blob instanceof BytesWritable) {
BytesWritable bytesWritable = (BytesWritable) blob;
// getBytes returns a reference to the current buffer which is only valid up to length
bytes = bytesWritable.getBytes();
length = bytesWritable.getLength();
} else {
throw new SerDeException("Invalid Writable instance, must be either Text or BytesWritable, was " + blob.getClass());
}
final IonSystem domFactory = ionFactory.getDomFactory();
try (final IonReader reader = ionFactory.newReader(bytes, 0, length)) {
/*
We're using an IonStruct here because:
1. We need a key-value store to carry column values
2. The top-level IonStruct as a context object carries the IonSystem which we use as a ValueFactory in
the callbacks created in PathExtractionConfig
Refer to https://github.com/amzn/ion-hive-serde/issues/61.
*/
IonStruct struct = domFactory.newEmptyStruct();
if (!serDeProperties.pathExtractorCaseSensitivity()) {
struct = new IonStructCaseInsensitiveDecorator(struct);
}
final PathExtractor<IonStruct> pathExtractor = serDeProperties.pathExtractor();
pathExtractor.match(reader, struct);
return struct;
} catch (IonException e) {
// skips if ignoring malformed
if (serDeProperties.getIgnoreMalformed()) {
return null;
}
throw e;
} catch (IOException e) {
throw new SerDeException(e);
}
}
use of com.amazon.ion.IonSystem in project jackson-dataformats-binary by FasterXML.
the class IonParserTest method testGetNumberTypeAndValue.
@Test
public void testGetNumberTypeAndValue() throws Exception {
IonSystem ion = IonSystemBuilder.standard().build();
Integer intValue = Integer.MAX_VALUE;
IonValue ionInt = ion.newInt(intValue);
IonParser intParser = ionFactory.createParser(EMPTY_READ_CTXT, ionInt);
Assert.assertEquals(JsonToken.VALUE_NUMBER_INT, intParser.nextToken());
Assert.assertEquals(JsonParser.NumberType.INT, intParser.getNumberType());
Assert.assertEquals(intValue, intParser.getNumberValue());
Long longValue = Long.MAX_VALUE;
IonValue ionLong = ion.newInt(longValue);
IonParser longParser = ionFactory.createParser(EMPTY_READ_CTXT, ionLong);
Assert.assertEquals(JsonToken.VALUE_NUMBER_INT, longParser.nextToken());
Assert.assertEquals(JsonParser.NumberType.LONG, longParser.getNumberType());
Assert.assertEquals(longValue, longParser.getNumberValue());
BigInteger bigIntValue = new BigInteger(Long.MAX_VALUE + "1");
IonValue ionBigInt = ion.newInt(bigIntValue);
IonParser bigIntParser = ionFactory.createParser(EMPTY_READ_CTXT, ionBigInt);
Assert.assertEquals(JsonToken.VALUE_NUMBER_INT, bigIntParser.nextToken());
Assert.assertEquals(JsonParser.NumberType.BIG_INTEGER, bigIntParser.getNumberType());
Assert.assertEquals(bigIntValue, bigIntParser.getNumberValue());
Double decimalValue = Double.MAX_VALUE;
IonValue ionDecimal = ion.newDecimal(decimalValue);
IonParser decimalParser = ionFactory.createParser(EMPTY_READ_CTXT, ionDecimal);
Assert.assertEquals(JsonToken.VALUE_NUMBER_FLOAT, decimalParser.nextToken());
Assert.assertEquals(JsonParser.NumberType.BIG_DECIMAL, decimalParser.getNumberType());
Assert.assertTrue(new BigDecimal("" + decimalValue).compareTo((BigDecimal) decimalParser.getNumberValue()) == 0);
Double floatValue = Double.MAX_VALUE;
IonValue ionFloat = ion.newFloat(floatValue);
IonParser floatParser = ionFactory.createParser(EMPTY_READ_CTXT, ionFloat);
Assert.assertEquals(JsonToken.VALUE_NUMBER_FLOAT, floatParser.nextToken());
Assert.assertEquals(JsonParser.NumberType.DOUBLE, floatParser.getNumberType());
Assert.assertEquals(floatValue, floatParser.getNumberValue());
BigDecimal bigDecimalValue = new BigDecimal(Double.MAX_VALUE + "1");
IonValue ionBigDecimal = ion.newDecimal(bigDecimalValue);
IonParser bigDecimalParser = ionFactory.createParser(EMPTY_READ_CTXT, ionBigDecimal);
Assert.assertEquals(JsonToken.VALUE_NUMBER_FLOAT, bigDecimalParser.nextToken());
Assert.assertEquals(JsonParser.NumberType.BIG_DECIMAL, bigDecimalParser.getNumberType());
Assert.assertTrue(bigDecimalValue.compareTo((BigDecimal) bigDecimalParser.getNumberValue()) == 0);
}
use of com.amazon.ion.IonSystem in project jackson-dataformats-binary by FasterXML.
the class IonValueDeserializer method deserialize.
@Override
public IonValue deserialize(JsonParser jp, DeserializationContext ctxt) throws JacksonException {
Object embeddedObject = jp.getEmbeddedObject();
if (embeddedObject instanceof IonValue) {
return (IonValue) embeddedObject;
}
// We rely on the IonParser's IonSystem to wrap supported types into an IonValue
if (!(jp instanceof IonParser)) {
throw DatabindException.from(jp, "Unsupported parser for deserializing " + embeddedObject.getClass().getCanonicalName() + " into IonValue");
}
IonSystem ionSystem = ((IonParser) jp).getIonSystem();
if (embeddedObject instanceof Timestamp) {
return ionSystem.newTimestamp((Timestamp) embeddedObject);
}
if (embeddedObject instanceof byte[]) {
// The parser provides no distinction between BLOB and CLOB, deserializing to a BLOB is the safest choice.
return ionSystem.newBlob((byte[]) embeddedObject);
}
throw DatabindException.from(jp, "Cannot deserialize embedded object type " + embeddedObject.getClass().getCanonicalName() + " into IonValue");
}
Aggregations