@decco Variant encoding

Is there an easy way to skip the array on encode?

@decco
type order = {
  name: string,
}

@decco
type rule =
  | Order(order)

Default:

//rule_encode
["Order", {name: "John Smith"}]

Needed:

//rule_encode
{name: "John Smith"}

Not exactly sure how to accomplish this with a custom codec or if that is the best way to achieve this.

Perhaps use order_encode? Note it depends on the full semantics of your actual type and how it needs to be encoded and decoded in the real codebase. From the simplified example it’s not really possible to tell.

Maybe it helps to parse into variant. https://github.com/green-labs/ppx_spice

2 Likes

Giving it a try, but having some issues, any idea?

Thanks,

I am trying @spice in replace for @decco and I am receiving this error but I am not able to diagnose where the issue is coming from:

There is no error shown in the above issue.

The only way I can get rid of the error above is by manually declaring this:

let json_arr = []

image

Please post the full error message including code snippet, file name, line number, etc. in text form, screenshots are not accessible.

That is the problem I am having, there is no additional information from the compiler, no line number, etc.

rescript: [3/15] src/T.cmj
FAILED: src/T.cmj

  We've found a bug for you!
  ...../app/client/src/T.res

  The value json_arr can't be found

FAILED: cannot make progress due to previous errors.
>>>> Finish compiling(exit: 1)

That’s useful information in itself. Looks like a PPX issue, let’s see what the the spice people say.

1 Like

Hi, I’ve already left my comment to your issue in the repo. The motivation of the spice was exactly what you’ve encountered regarding the use case of the variant. I forked and change how to serialize and deserialize the json string to the variant or polymorphic variant, vice and versa. Therefore, the spice can not handle the variant with value like your example.
There was another motivation. Spice can handle the unicode string for the variant.

1 Like