Skip to content

Instantly share code, notes, and snippets.

@lalongooo
Created September 14, 2023 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lalongooo/7317a45c7d90b6ad23b8760c3398e12c to your computer and use it in GitHub Desktop.
Save lalongooo/7317a45c7d90b6ad23b8760c3398e12c to your computer and use it in GitHub Desktop.
Amazon Ion Serialization Example
// 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