简体   繁体   中英

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM