How would one go about using kotlinx to serialize ...
# serialization
r
How would one go about using kotlinx to serialize what is essentially a union type? For example, the swagger 2.0 spec allows for many types of objects to be specified, and most conform quite nicely to kotlinx polymorphic serialization, providing a 'type' key to hint at the payload structure. However, almost all of these fields could also be a
$ref
payload, which is a simple single value data class with that one parameter. short snippet example
Copy code
BlkioWeightDevice:
        description: |
          Block IO weight (relative device weight) in the form:
[{"Path": "device_path", "Weight": weight}]
Copy code
type: "array"
        items:
          type: "object"
          properties:
            Path:
              type: "string"
            Weight:
              type: "integer"
              minimum: 0
      BlkioDeviceReadBps:
        description: |
          Limit read rate (bytes per second) from a device, in the form:
[{"Path": "device_path", "Rate": rate}]
Copy code
type: "array"
        items:
          $ref: "#/definitions/ThrottleDevice"
b
Have you looked into the
JsonContentPolymorphicSerializer
, which lets you manually determine the type to deserialize as from the shape of the JSON, instead of having an object with a type field?
r
That looks very promising, thank you!