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

[Question] Lost units in JSON #10

Closed
AlexandreRoba opened this issue Jun 18, 2023 · 2 comments
Closed

[Question] Lost units in JSON #10

AlexandreRoba opened this issue Jun 18, 2023 · 2 comments

Comments

@AlexandreRoba
Copy link

Hi all, Thanks for this amazing libraries.

I'm trying to serialize a length in JSON to store it in a JSON Document but I can seem to find the unit in the serialized object. Am I correct. Ths object seem to be serialized as
{"amount":12.5,"dimension":{"length":1}}

it serialize the same way no matter if its a km or a meter. Any advice on how to proceed? I do not want to loose the unit in the process.

Thanks.

@Lukinoh
Copy link
Contributor

Lukinoh commented Jun 20, 2023

Hello,

I am not sure that the library was thought to have the quantity serializable.
As you mention the following quantities get serialized the same way:

const quantity1 = grams(1);
const quantity2 = kilograms(1);

// Result: {"amount":"1","dimension":{"mass":1}}

If you console.log the quantity1 and quantity2 you can see that the unit is stored in a function.
Hence the JSON.stringify strips it away.

QuantityImpl {
amount: 1,
unit: [Function: makeQuantity] UnitImpl {
  symbol: ' g',
  dimension: { mass: 1 },
  scale: 0.001,
  offset: 0,
  arithmetic: {
    from: [Function: from],
    toNative: [Function: toNative],
    add: [Function: add],
    sub: [Function: sub],
    mul: [Function: mul],
    div: [Function: div],
    pow: [Function: pow],
    abs: [Function: abs],
    compare: [Function: compare]
  }
},
dimension: { mass: 1 }
}

QuantityImpl {
amount: 1,
unit: [Function: makeQuantity] UnitImpl {
  symbol: ' kg',
  dimension: { mass: 1 },
  scale: 1,
  offset: 0,
  arithmetic: {
    from: [Function: from],
    toNative: [Function: toNative],
    add: [Function: add],
    sub: [Function: sub],
    mul: [Function: mul],
    div: [Function: div],
    pow: [Function: pow],
    abs: [Function: abs],
    compare: [Function: compare]
  }
},
dimension: { mass: 1 }
}

I don't know if the library will be update with a serialization feature.
Probably, the best solution you have without modifying the library is to write your own serializer.

The serialize function transform your quantity to:

{
  amount: number;
  symbol: string
}

And the deserialize calls the function that corresponds to the symbol with the amount.

// Trivial implementation
if (symbol === 'g') {
  return grams(amount)
} else if () {
}

@AlexandreRoba
Copy link
Author

Hi @Lukinoh thanks for your reply, indeed I ended up with the same conclusion. I'm using trpc and superjson and some data cross the wire so I need to serialize it. Again for the library. I'm implementing my own serialization.

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

2 participants