Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transit-js cannot encode doubles which happen to be integers #55

Open
samdesota opened this issue Aug 31, 2020 · 0 comments
Open

transit-js cannot encode doubles which happen to be integers #55

samdesota opened this issue Aug 31, 2020 · 0 comments

Comments

@samdesota
Copy link

samdesota commented Aug 31, 2020

I'm sending data from a node js client encoded with transit and persisting in Datomic, however since transit-js appears to not have support for a explicit transit.double type, Datomic will error any time I try persist data with a float that happens to be an integer, because it is decoded in transit-clj as an integer.

I've worked around this by creating a new class, and creating a custom handler that returns the encoded double via the rep method on the handler interface:

class Decimal {
  constructor(value: number) {
    this.value = value
  }
}

const handlers = t.map();

  handlers.set(Decimal, {
    tag: () => "d",
    rep: (dec: any) => `~d${dec.value}`,
  });

  return t.writer("json", { handlers });

This seems to be the only way I can get transit-js to correctly encode float types. It seems like there should be a transit.double method that will force the long-form encoding of doubles so the type information isn't lost in transit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant