nim-plotly icon indicating copy to clipboard operation
nim-plotly copied to clipboard

macro for `%` JSON serialization

Open brentp opened this issue 6 years ago • 2 comments

currently any time an attribute is added, the % procs must be updated to include that attribute.

a macro that iterated over the fields and values would simplify this a lot.

brentp avatar May 16 '18 16:05 brentp

@brentp not sure i understand, but *% from std/json can do that: it can serialize (or reverse: https://nim-lang.github.io/Nim/json.html#overview-unmarshalling) objects and tuples directly, no need to iterate over fields

specifically: this could be replaced by: let foo = %* a IIRC

func `%`*(a: Annotation): JsonNode =
  ## creates a JsonNode from an `Annotations` object depending on the object variant
  result = %[ ("x", %a.x)
            , ("xshift", %a.xshift)
            , ("y", %a.y)
            , ("yshift", %a.yshift)
            , ("text", %a.text)
            , ("showarrow", %a.showarrow)
            ]

timotheecour avatar Jan 26 '19 04:01 timotheecour

I mean, for example, the ones the use ordered fields, as in: https://github.com/brentp/nim-plotly/blob/master/src/plotly/api.nim#L175

brentp avatar Jan 26 '19 14:01 brentp