Skip to content

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

@samdesota

Description

@samdesota

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions