How do I write a Java list to Amazon Ion writer?

403 Views Asked by At

Has anyone worked with Amazon Ion? (https://amzn.github.io/ion-docs/guides/cookbook.html)

I have to write a doc with IonWriter, the problem being that the schema needs something like this:

{
 name: "schemaName",
 valid: [a, b, c]
}

but I am unable to find a way to write [a,b,c] without quotes.

Things I have tried:

  • used writeString() by converting list to string
  • used writeByte() that resulted in byte data which is not required
  • used writeSymbol() that resulted in same as string.

Is there a way to do this?

2

There are 2 best solutions below

0
On BEST ANSWER

It looks like the use case of data type symbol based on Amazon ION specification https://amzn.github.io/ion-docs/docs/spec.html#symbol, you can try code below.

import com.amazon.ion.IonStruct;
import com.amazon.ion.IonSystem;
import com.amazon.ion.system.IonSystemBuilder;

public class IonSample {
    public static void main(String[] args) {
        IonSystem ionSystem = IonSystemBuilder.standard().build();

        IonStruct ionStruct = ionSystem.newEmptyStruct();
        ionStruct.add("name", ionSystem.newString("schemaName"));
        ionStruct.add("valid", ionSystem.newList(
            ionSystem.newSymbol("a"),
            ionSystem.newSymbol("b"),
            ionSystem.newSymbol("c")));

        System.out.println(ionStruct.toPrettyString());
    }
}

Output

{
  name:"schemaName",
  valid:[
    a,
    b,
    c
  ]
}
0
On

So I was able to find a way out we could simple use IonType.LIST and stepIn() and stepOut to achieve something like this.