Search in sources :

Example 41 with Timestamp

use of com.amazon.ion.Timestamp 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)

Example 42 with Timestamp

use of com.amazon.ion.Timestamp in project ion-java by amzn.

the class IonReaderTextSystemX method load_scalar_value.

private final void load_scalar_value() throws IOException {
    // make sure we're trying to load a scalar value here
    switch(_value_type) {
        case NULL:
            _v.setValueToNull(_null_type);
            _v.setAuthoritativeType(AS_TYPE.null_value);
            return;
        case BOOL:
        case INT:
        case FLOAT:
        case DECIMAL:
        case TIMESTAMP:
        case SYMBOL:
        case STRING:
            break;
        default:
            return;
    }
    StringBuilder cs = token_contents_load(_scanner.getToken());
    int token_type = _scanner.getToken();
    if (_value_type == IonType.DECIMAL) {
        // a string, but still in the StringBuilder
        for (int ii = 0; ii < cs.length(); ii++) {
            int c = cs.charAt(ii);
            if (c == 'd' || c == 'D') {
                cs.setCharAt(ii, 'e');
                break;
            }
        }
    } else if (token_type == IonTokenConstsX.TOKEN_HEX || token_type == IonTokenConstsX.TOKEN_BINARY) {
        boolean isNegative = (cs.charAt(0) == '-');
        // prefix = is_negative ? "-0x" : "0x";
        int pos = isNegative ? 1 : 0;
        char caseChar = token_type == IonTokenConstsX.TOKEN_HEX ? 'x' : 'b';
        if (cs.length() <= (isNegative ? 3 : 2) || Character.toLowerCase(cs.charAt(pos + 1)) != caseChar) {
            parse_error("Invalid " + (caseChar == 'x' ? "hexadecimal" : "binary") + " int value.");
        }
        cs.deleteCharAt(pos);
        cs.deleteCharAt(pos);
    }
    int len = cs.length();
    String s = cs.toString();
    clear_current_value_buffer();
    switch(token_type) {
        case IonTokenConstsX.TOKEN_UNKNOWN_NUMERIC:
            switch(_value_type) {
                case INT:
                    if (Radix.DECIMAL.isInt(s, len)) {
                        _v.setValue(Integer.parseInt(s));
                    } else if (Radix.DECIMAL.isLong(s, len)) {
                        _v.setValue(Long.parseLong(s));
                    } else {
                        _v.setValue(new BigInteger(s));
                    }
                    break;
                case DECIMAL:
                    // note that the string was modified above when it was a charsequence
                    try {
                        _v.setValue(Decimal.valueOf(s));
                    } catch (NumberFormatException e) {
                        parse_error(e);
                    }
                    break;
                case FLOAT:
                    try {
                        _v.setValue(Double.parseDouble(s));
                    } catch (NumberFormatException e) {
                        parse_error(e);
                    }
                    break;
                case TIMESTAMP:
                    _v.setValue(Timestamp.valueOf(s));
                    break;
                default:
                    String message = "unexpected prefectched value type " + getType().toString() + " encountered handling an unquoted symbol";
                    parse_error(message);
            }
            break;
        case IonTokenConstsX.TOKEN_INT:
            if (Radix.DECIMAL.isInt(s, len)) {
                _v.setValue(Integer.parseInt(s));
            } else if (Radix.DECIMAL.isLong(s, len)) {
                _v.setValue(Long.parseLong(s));
            } else {
                _v.setValue(new BigInteger(s));
            }
            break;
        case IonTokenConstsX.TOKEN_BINARY:
            if (Radix.BINARY.isInt(s, len)) {
                _v.setValue(Integer.parseInt(s, 2));
            } else if (Radix.BINARY.isLong(s, len)) {
                _v.setValue(Long.parseLong(s, 2));
            } else {
                _v.setValue(new BigInteger(s, 2));
            }
            break;
        case IonTokenConstsX.TOKEN_HEX:
            if (Radix.HEX.isInt(s, len)) {
                int v_int = Integer.parseInt(s, 16);
                _v.setValue(v_int);
            } else if (Radix.HEX.isLong(s, len)) {
                long v_long = Long.parseLong(s, 16);
                _v.setValue(v_long);
            } else {
                BigInteger v_big_int = new BigInteger(s, 16);
                _v.setValue(v_big_int);
            }
            break;
        case IonTokenConstsX.TOKEN_DECIMAL:
            try {
                _v.setValue(Decimal.valueOf(s));
            } catch (NumberFormatException e) {
                parse_error(e);
            }
            break;
        case IonTokenConstsX.TOKEN_FLOAT:
            try {
                _v.setValue(Double.parseDouble(s));
            } catch (NumberFormatException e) {
                parse_error(e);
            }
            break;
        case IonTokenConstsX.TOKEN_TIMESTAMP:
            Timestamp t = null;
            try {
                t = Timestamp.valueOf(s);
            } catch (IllegalArgumentException e) {
                parse_error(e);
            }
            _v.setValue(t);
            break;
        case IonTokenConstsX.TOKEN_SYMBOL_IDENTIFIER:
            // us as to what the saved type is)
            if (isNullValue()) {
                // the raw parser set _null_type when it
                // detected the unquoted symbol null in the
                // input (which is what isNullValue looks at)
                _v.setValueToNull(_null_type);
            } else {
                switch(getType()) {
                    case SYMBOL:
                        // TODO this is catching SIDs too, using wrong text.
                        _v.setValue(s);
                        break;
                    case FLOAT:
                        switch(_value_keyword) {
                            case IonTokenConstsX.KEYWORD_NAN:
                                _v.setValue(Double.NaN);
                                break;
                            default:
                                String message = "unexpected keyword " + s + " identified as a FLOAT";
                                parse_error(message);
                        }
                        break;
                    case BOOL:
                        switch(_value_keyword) {
                            case IonTokenConstsX.KEYWORD_TRUE:
                                _v.setValue(true);
                                break;
                            case IonTokenConstsX.KEYWORD_FALSE:
                                _v.setValue(false);
                                break;
                            default:
                                String message = "unexpected keyword " + s + " identified as a BOOL";
                                parse_error(message);
                        }
                        break;
                    default:
                        String message = "unexpected prefectched value type " + getType().toString() + " encountered handling an unquoted symbol";
                        parse_error(message);
                }
            }
            break;
        case IonTokenConstsX.TOKEN_SYMBOL_QUOTED:
        case IonTokenConstsX.TOKEN_SYMBOL_OPERATOR:
        case IonTokenConstsX.TOKEN_STRING_DOUBLE_QUOTE:
            _v.setValue(s);
            break;
        case IonTokenConstsX.TOKEN_STRING_TRIPLE_QUOTE:
            // long strings (triple quoted strings) are never
            // finished by the raw parser.  At most it reads
            // the first triple quoted string.
            _v.setValue(s);
            break;
        default:
            parse_error("scalar token " + IonTokenConstsX.getTokenName(_scanner.getToken()) + "isn't a recognized type");
    }
}
Also used : BigInteger(java.math.BigInteger) Timestamp(com.amazon.ion.Timestamp) IonTimestamp(com.amazon.ion.IonTimestamp)

Example 43 with Timestamp

use of com.amazon.ion.Timestamp in project ion-java by amzn.

the class IonRawBinaryWriterTest method testTimestampInvalidFractionalSeconds.

@Test
public void testTimestampInvalidFractionalSeconds() throws IOException {
    Timestamp ts = Timestamp.createFromUtcFields(Timestamp.Precision.FRACTION, 2000, 1, 1, 0, 0, 0, new BigDecimal(new BigInteger("0"), -1), 0);
    writer.writeTimestamp(ts);
    writer.finish();
    assertEquals(TestUtils.hexDump(new byte[] { (byte) 0xe0, 0x01, 0x00, (byte) 0xea, 0x68, (byte) 0x80, 0x0f, (byte) 0xd0, (byte) 0x81, (byte) 0x81, (byte) 0x80, (byte) 0x80, (byte) 0x80 }), TestUtils.hexDump(writer.getBytes()));
}
Also used : BigInteger(java.math.BigInteger) Timestamp(com.amazon.ion.Timestamp) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 44 with Timestamp

use of com.amazon.ion.Timestamp in project ion-java by amzn.

the class SymbolTableTest method testSymtabBinaryInjection.

@Test
public void testSymtabBinaryInjection() throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    IonWriter writer = system().newBinaryWriter(baos);
    writer.stepIn(IonType.LIST);
    writer.writeNull();
    writer.writeInt(10);
    writer.writeFloat(10.0);
    writer.writeTimestamp(new Timestamp(2013, 1, 1));
    // this is where symbol table injection happens
    writer.writeSymbol("abc");
    writer.writeString("abc");
    writer.stepOut();
    writer.finish();
    IonReader reader = system().newReader(baos.toByteArray());
    assertEquals(IonType.LIST, reader.next());
    reader.stepIn();
    assertEquals(IonType.NULL, reader.next());
    assertEquals(IonType.INT, reader.next());
    assertEquals(IonType.FLOAT, reader.next());
    assertEquals(IonType.TIMESTAMP, reader.next());
    assertEquals(IonType.SYMBOL, reader.next());
    assertEquals(IonType.STRING, reader.next());
    assertEquals(null, reader.next());
    reader.stepOut();
    assertEquals(null, reader.next());
}
Also used : IonReader(com.amazon.ion.IonReader) IonWriter(com.amazon.ion.IonWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Timestamp(com.amazon.ion.Timestamp) Test(org.junit.Test)

Example 45 with Timestamp

use of com.amazon.ion.Timestamp in project ion-java by amzn.

the class AbstractIonWriter method writeValueRecursive.

public final void writeValueRecursive(final IonReader reader) throws IOException {
    final IonType type = reader.getType();
    final SymbolToken fieldName = reader.getFieldNameSymbol();
    if (fieldName != null && !isFieldNameSet() && isInStruct()) {
        setFieldNameSymbol(fieldName);
    }
    final SymbolToken[] annotations = reader.getTypeAnnotationSymbols();
    if (annotations.length > 0) {
        setTypeAnnotationSymbols(annotations);
    }
    if (reader.isNullValue()) {
        writeNull(type);
        return;
    }
    switch(type) {
        case BOOL:
            final boolean booleanValue = reader.booleanValue();
            writeBool(booleanValue);
            break;
        case INT:
            switch(reader.getIntegerSize()) {
                case INT:
                    final int intValue = reader.intValue();
                    writeInt(intValue);
                    break;
                case LONG:
                    final long longValue = reader.longValue();
                    writeInt(longValue);
                    break;
                case BIG_INTEGER:
                    final BigInteger bigIntegerValue = reader.bigIntegerValue();
                    writeInt(bigIntegerValue);
                    break;
                default:
                    throw new IllegalStateException();
            }
            break;
        case FLOAT:
            final double doubleValue = reader.doubleValue();
            writeFloat(doubleValue);
            break;
        case DECIMAL:
            final Decimal decimalValue = reader.decimalValue();
            writeDecimal(decimalValue);
            break;
        case TIMESTAMP:
            final Timestamp timestampValue = reader.timestampValue();
            writeTimestamp(timestampValue);
            break;
        case SYMBOL:
            final SymbolToken symbolToken = reader.symbolValue();
            writeSymbolToken(symbolToken);
            break;
        case STRING:
            final String stringValue = reader.stringValue();
            writeString(stringValue);
            break;
        case CLOB:
            final byte[] clobValue = reader.newBytes();
            writeClob(clobValue);
            break;
        case BLOB:
            final byte[] blobValue = reader.newBytes();
            writeBlob(blobValue);
            break;
        case LIST:
        case SEXP:
        case STRUCT:
            reader.stepIn();
            stepIn(type);
            while (reader.next() != null) {
                writeValue(reader);
            }
            stepOut();
            reader.stepOut();
            break;
        default:
            throw new IllegalStateException("Unexpected type: " + type);
    }
}
Also used : IonType(com.amazon.ion.IonType) SymbolToken(com.amazon.ion.SymbolToken) Timestamp(com.amazon.ion.Timestamp) Decimal(com.amazon.ion.Decimal) BigInteger(java.math.BigInteger)

Aggregations

Timestamp (com.amazon.ion.Timestamp)51 Test (org.junit.Test)38 Instant (java.time.Instant)13 OffsetDateTime (java.time.OffsetDateTime)10 ZonedDateTime (java.time.ZonedDateTime)10 IonReader (com.amazon.ion.IonReader)8 IonObjectMapper (com.fasterxml.jackson.dataformat.ion.IonObjectMapper)8 RepeatInputStream (com.amazon.ion.util.RepeatInputStream)7 ByteArrayInputStream (java.io.ByteArrayInputStream)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 InputStream (java.io.InputStream)7 SequenceInputStream (java.io.SequenceInputStream)7 BigInteger (java.math.BigInteger)7 IonTimestamp (com.amazon.ion.IonTimestamp)6 BigDecimal (java.math.BigDecimal)5 Temporal (java.time.temporal.Temporal)5 Decimal (com.amazon.ion.Decimal)2 IonException (com.amazon.ion.IonException)2 IonWriter (com.amazon.ion.IonWriter)2 IonBlob (com.amazon.ion.IonBlob)1