Created
September 14, 2023 16:51
-
-
Save lalongooo/7317a45c7d90b6ad23b8760c3398e12c to your computer and use it in GitHub Desktop.
Amazon Ion Serialization Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Serialize data to Ion format | |
val out = ByteArrayOutputStream() | |
val ionWriter = IonTextWriterBuilder.standard().build(out) | |
ionWriter.stepIn(IonType.STRUCT) | |
ionWriter.setFieldName("name") | |
ionWriter.writeString("John Doe") | |
ionWriter.stepOut() | |
// Deserialize Ion data | |
val inputStream = ByteArrayInputStream(out.toByteArray()) | |
val readerBuilder = IonReaderBuilder.standard().build(inputStream) | |
readerBuilder.next() // position the reader at the first value, a struct | |
readerBuilder.stepIn() // step into the struct | |
readerBuilder.next() // position the reader at the first value in the struct | |
val fieldName: String = readerBuilder.fieldName // retrieve the current value's field name | |
val value: String = readerBuilder.stringValue() // retrieve the current value's String value | |
readerBuilder.stepOut() // step out of the struct | |
Log.d("TAG", "$fieldName $value") // prints "hello world" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment