amazon-ion / ion-element-kotlin

IonElement is an immutable in-memory representation of the Ion data format. IonElement's API is idiomatic to Kotlin.
Apache License 2.0
8 stars 7 forks source link

Create IonElement with annotation & meta from String #10

Open Zhouenze opened 4 years ago

Zhouenze commented 4 years ago

Currently IonElement doesn't support creation from String that contain annotation & meta, it has to be created like this:

ionSexpOf(
  ionSymbolOf("sexp"),
  ionSexpOf(
    ionSymbolOf("data").withAnnotation(...).withMeta(...)))

Instead of above, something like this would be much simpler: parseFromString("(sexp (annotation::data meta={line:1,column:23}))")

This ability is present for IonValue, it would be great if we could have the same for IonElement.

Thanks!

popematt commented 2 years ago

It seems like there are two issues here. The first is that you would like a way to get an IonElement tree from a string, and the second is that you want to be able to serialize/deserialize the "metas".

Creating an IonElement tree from a string

This is already possible using the loadSingleElement function, found here. For example:

loadSingleElement("(sexp (annotation::data))")

This is the equivalent to calling IonSystem::singleValue(...) using ion-java to get an IonValue.

Deserializing Metas

The "metas" are not actually part of the Ion data format, and there is no support for metas in IonValue. To get the location metadata for IonElement, you can do this:

import com.amazon.ionelement.util.INCLUDE_LOCATION_META

// ...

loadSingleElement("(sexp (annotation::data))", INCLUDE_LOCATION_META)

That will include the actual location in the metas, so the symbol data will have metas with a location of line:1,column:8 (because the annotation:: starts at column 8).