Skip to content

Commit

Permalink
fix: serialization of Units without a value, see #1240
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed Apr 29, 2024
1 parent d97f338 commit da0c70e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# History

# unpublished changes since 12.4.2

- Fix: serialization of Units without a value, see #1240.


# 2024-04-24, 12.4.2

- Fix #3192: function `isNaN` returns `false` for `NaN` units in a matrix or
Expand Down
4 changes: 2 additions & 2 deletions src/type/unit/Unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ export const createUnitClass = /* #__PURE__ */ factory(name, dependencies, ({
return {
mathjs: 'Unit',
value: this._denormalize(this.value),
unit: this.formatUnits(),
unit: this.units.length > 0 ? this.formatUnits() : null,
fixPrefix: this.fixPrefix
}
}
Expand All @@ -915,7 +915,7 @@ export const createUnitClass = /* #__PURE__ */ factory(name, dependencies, ({
* @return {Unit}
*/
Unit.fromJSON = function (json) {
const unit = new Unit(json.value, json.unit)
const unit = new Unit(json.value, json.unit ?? undefined)
unit.fixPrefix = json.fixPrefix || false
return unit
}
Expand Down
14 changes: 14 additions & 0 deletions test/unit-tests/json/replacer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ describe('replacer', function () {
assert.deepStrictEqual(JSON.stringify(u, replacer), json)
})

it('should stringify a Unit with a value only', function () {
const u = new math.Unit(5)
const json = '{"mathjs":"Unit","value":5,"unit":null,"fixPrefix":false}'
assert.deepStrictEqual(JSON.stringify(u), json)
assert.deepStrictEqual(JSON.stringify(u, replacer), json)
})

it('should stringify a Unit without a value', function () {
const u = new math.Unit(null, 'cm')
const json = '{"mathjs":"Unit","value":null,"unit":"cm","fixPrefix":false}'
assert.deepStrictEqual(JSON.stringify(u), json)
assert.deepStrictEqual(JSON.stringify(u, replacer), json)
})

it('should stringify a Matrix, dense', function () {
const m = math.matrix([[1, 2], [3, 4]], 'dense')
const json = '{"mathjs":"DenseMatrix","data":[[1,2],[3,4]],"size":[2,2]}'
Expand Down
20 changes: 20 additions & 0 deletions test/unit-tests/json/reviver.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,26 @@ describe('reviver', function () {
assert.deepStrictEqual(obj, u)
})

it('should parse a stringified Unit with a value only', function () {
const json = '{"mathjs":"Unit","value":5,"unit":null,"fixPrefix":false}'
const u = new math.Unit(5)

const obj = JSON.parse(json, reviver)

assert(obj instanceof math.Unit)
assert.deepStrictEqual(obj, u)
})

it('should parse a stringified Unit without a value', function () {
const json = '{"mathjs":"Unit","value":null,"unit":"cm","fixPrefix":false}'
const u = new math.Unit(null, 'cm')

const obj = JSON.parse(json, reviver)

assert(obj instanceof math.Unit)
assert.deepStrictEqual(obj, u)
})

it('should parse a stringified Range (2)', function () {
const json = '{"mathjs":"Range","start":2,"end":10,"step":2}'
const r = new math.Range(2, 10, 2)
Expand Down

0 comments on commit da0c70e

Please sign in to comment.