reasonml / reason

Simple, fast & type safe code that leverages the JavaScript & OCaml ecosystems
http://reasonml.github.io
MIT License
10.14k stars 428 forks source link

Variant serialization #2560

Closed dmtrKovalenko closed 4 years ago

dmtrKovalenko commented 4 years ago

Consider changing runtime value representation of variants, because for now there is a pain in the ass to serialize and deserialize them manually. Variants are super-duper cool for making strongly typed event buses, but it doesn't work out of the box :(

Workaround:

let serializeVariant: 'a => string = [%bs.raw
  {|
    function (variant) {
      return JSON.stringify({ tag: variant.tag, variant };
    }
  |}
];

let deserializeVariant: string => 'a = [%bs.raw
  {|
    function (serializedString) {
      const { tag, variant } = JSON.parse(serializedString);
      variant.tag = tag;

      return variant;
    }
  |}
];
hhugo commented 4 years ago

This does not belong here. Bucklescript is responsible for the compilation to javascript https://github.com/BuckleScript/bucklescript/.

dmtrKovalenko commented 4 years ago

Yeah, my bad