Search in sources :

Example 16 with IonSystem

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));
    }
}
Also used : BigInteger(java.math.BigInteger) IonValue(com.amazon.ion.IonValue) IonSystem(com.amazon.ion.IonSystem) Test(org.junit.Test)

Example 17 with IonSystem

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);
}
Also used : IonValue(com.amazon.ion.IonValue) IonSystem(com.amazon.ion.IonSystem) Test(org.junit.Test)

Example 18 with IonSystem

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);
    }
}
Also used : IonSystem(com.amazon.ion.IonSystem) IonStruct(com.amazon.ion.IonStruct) IonException(com.amazon.ion.IonException) IonReader(com.amazon.ion.IonReader) IonStructCaseInsensitiveDecorator(com.amazon.ionhiveserde.caseinsensitivedecorator.IonStructCaseInsensitiveDecorator) Text(org.apache.hadoop.io.Text) BytesWritable(org.apache.hadoop.io.BytesWritable) IOException(java.io.IOException) SerDeException(org.apache.hadoop.hive.serde2.SerDeException)

Example 19 with IonSystem

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);
}
Also used : BigInteger(java.math.BigInteger) IonValue(com.amazon.ion.IonValue) IonSystem(com.amazon.ion.IonSystem) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 20 with IonSystem

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");
}
Also used : IonValue(com.amazon.ion.IonValue) IonSystem(com.amazon.ion.IonSystem) IonParser(com.fasterxml.jackson.dataformat.ion.IonParser) Timestamp(com.amazon.ion.Timestamp)

Aggregations

IonSystem (com.amazon.ion.IonSystem)39 Test (org.junit.Test)27 IonValue (com.amazon.ion.IonValue)12 SymbolTable (com.amazon.ion.SymbolTable)10 IonReader (com.amazon.ion.IonReader)8 IonWriter (com.amazon.ion.IonWriter)8 IonDatagram (com.amazon.ion.IonDatagram)6 IonStruct (com.amazon.ion.IonStruct)6 IonCatalog (com.amazon.ion.IonCatalog)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 IonString (com.amazon.ion.IonString)4 com.amazon.ion.impl._Private_IonBinaryWriterBuilder (com.amazon.ion.impl._Private_IonBinaryWriterBuilder)4 com.amazon.ion.impl._Private_IonWriter (com.amazon.ion.impl._Private_IonWriter)4 BinaryTest (com.amazon.ion.BinaryTest)3 IonBinaryWriter (com.amazon.ion.IonBinaryWriter)2 IonType (com.amazon.ion.IonType)2 SimpleCatalog (com.amazon.ion.system.SimpleCatalog)2 IOException (java.io.IOException)2 BigInteger (java.math.BigInteger)2 Date (java.util.Date)2