Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional metadata in classdesc #137

Closed
pdvrieze opened this issue May 21, 2018 · 3 comments
Closed

Additional metadata in classdesc #137

pdvrieze opened this issue May 21, 2018 · 3 comments

Comments

@pdvrieze
Copy link
Contributor

I've been writing a serializer for xml. It's been a bit of a struggle. In particular I found that the metadata provided in KSerialClassDesc is insufficient. If I want to support existing formats (rather than a serialization that is convenient to work with for Kotlin serialization). I need to be able to know more about the children of an element (note that this information is already available and embedded into the serializer/deserializer):

  • Whether or not it is nullable or optional - In XML nullable values should be omitted, but when reading
    this requires those missing values to be provided for reading as being null. Optional values can just be
    omitted. For list-like types I'd like to provide an empty list for non-optionals (which break in many other
    ways for non-trivial classes).
  • What its KSerialClassKind is helps with deciding how to serialize/deserialize it, in particular whether to
    serialize it as body text, attribute or element. (list elements are natural as body text - a single element
    could be an attribute)
  • Whether it will use a loader/saver (possibly which loader/saver). In that sense it would be very helpful if the loader/saver interfaces would actually export serialClassDesc (which KSerializer does).

Currently I work around this by "retrieving" the information myself. I've created CanaryInput and CanaryOuput classes that simulate reading/writing and record what the loader/saver actually does. For loading this involves throwing and catching exceptions (I don't have actual values). For saver it depends on the data being saved so it could be incomplete (if a value is null I don't get to see what type it is when not null). The input is more complete in what information it can retrieve, but this is still rather much a pain.

@sandwwraith
Copy link
Member

sandwwraith commented May 22, 2018

For first option of knowing nullability/optionality we have pending requests: #71 #58 will be resolved when it would be possible to pass this information, so we're considering it as a useful feature.

I don't understand points two and three, though. Class kind is available from descriptor inside writeBegin and usage of loaders/savers always go through write/read(Nullable)SerializableElementValue which also has a descriptor.

@pdvrieze
Copy link
Contributor Author

I know that class kind is available in writeBegin (or readBegin for the parameter). I've looked at my code and this is where I use it for:

  • To determine whether a child is a list/set/map to generate an empty list of them as I don't know whether it is optional.
  • To determine whether a child should be serialized as XML tag (using the child count in this case) or attribute.
  • In particular if I serialize a primitive (eg string) element, this will need to be wrapped in a tag but if the element is a tag it shouldn't be.

Not in the current code, but I would like to be able to generate a schema from the serializer. Overall this should be able to be done merely by adding a reference to the referred loader/saver (and having the loader/saver be required to provide child classdesc)..

@sandwwraith
Copy link
Member

sandwwraith commented Apr 16, 2019

Provided design for SerialDescriptor covers all cases. It supports child iterating (via elementsCount and getElementDescriptor) and determining the primitive/composite kind of an element.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants