Skip to content

JSON support #53

@jessealama

Description

@jessealama

Should Amount have a toJSON method? If so, perhaps the method could return something like

{ 
  "value": "123.456"
  "unit": "kilogram"
}

The value in unit could be undefined if the Amount doesn't have a unit.

Do we want to include the significant digits and/or fraction digits properties, too?

This issue touches on #35. If we round on the way in, then the string contained in the value property is precisely the value, so including significant digits and fraction digits is arguably unnecessary. However, if we round only on the way out (only), conversion to JSON might be like toNumber, toString in that toJSON would not just emit a JSON object, but also do rounding beforehand. Altneratively, one could include significant digits and fraction digits in the JSON:

let a = new Amount(123.456);
a.with({ fractionDigits: 2 }.toJSON();
// { "value": "123.46", "unit": undefined }

// or:

// { "value": "123.456", "fractionDigits": 2, "unit": undefined }

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