Search code examples
amazon-ion

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


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?


Solution

  • 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
      ]
    }