gabbhack / deser

Serde-like de/serialization library for Nim.
https://gabbhack.github.io/deser/
MIT License
23 stars 0 forks source link

Object variants support #6

Closed gabbhack closed 2 years ago

gabbhack commented 3 years ago

Currently, deser does not support deserialization in object variants. I think to implement it is like a serde, but without externally tagged (because it can be done without object variants.) and may be adjacently tagged - https://serde.rs/enum-representations.html

How I see tagged:

type
  MessageEnum = enum
    Request, Response
  Message = object
    id: string
    case kind {.rename("type").}: MessageEnum
    of Request:
      `method`: string
      params: Params
    of Response:
      result: Value

const js = """
{
  "type": "Request",
  "id": "123",
  "method": "321",
  "params": {...}
}
"""

let msg = js.parse().to(Message)
assert msg.kind == Request

How i see untagged:

##[
`untagged` is not for the entire object, because an object can have multiple "cases" with different behaviors.
`deser` tries to match data with each case, the first successfully matched one will be returned, and the `kind` field will be assigned the corresponding value.
]##
type
  MessageEnum = enum
    Request, Response
  Message = object
    id: string
    case kind {.untagged.}: MessageEnum
    of Request:
      `method`: string
      params: Params
    of Response:
      result: Value

const js = """
{
  "id": "123",
  "method": "321",
  "params": {...}
}
"""

let msg = js.parse().to(Message)
assert msg.kind == Request

Blocked by: