Skip to content

Commit

Permalink
JSON serialize NaN and Infinity
Browse files Browse the repository at this point in the history
ref #318
  • Loading branch information
frostburn committed May 13, 2024
1 parent 3861e78 commit 7409e6b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
16 changes: 13 additions & 3 deletions src/__tests__/monzo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,24 +461,26 @@ describe('JSON serialization', () => {
3.5,
TimeMonzo.fromFraction('81/80'),
null,
new TimeReal(0, NaN),
new TimeReal(1, Infinity),
];
const serialized = JSON.stringify(data);
expect(serialized).toBe(
'["Hello, world!",{"n":10,"d":7},{"type":"TimeReal","timeExponent":-1,"value":777},3.5,{"type":"TimeMonzo","timeExponent":{"n":0,"d":1},"primeExponents":[{"n":-4,"d":1},{"n":4,"d":1},{"n":-1,"d":1},{"n":0,"d":1},{"n":0,"d":1},{"n":0,"d":1},{"n":0,"d":1},{"n":0,"d":1},{"n":0,"d":1}],"residual":{"n":1,"d":1}},null]'
'["Hello, world!",{"n":10,"d":7},{"type":"TimeReal","timeExponent":-1,"value":777},3.5,{"type":"TimeMonzo","timeExponent":{"n":0,"d":1},"primeExponents":[{"n":-4,"d":1},{"n":4,"d":1},{"n":-1,"d":1},{"n":0,"d":1},{"n":0,"d":1},{"n":0,"d":1},{"n":0,"d":1},{"n":0,"d":1},{"n":0,"d":1}],"residual":{"n":1,"d":1}},null,{"type":"TimeReal","timeExponent":0,"value":"NaN"},{"type":"TimeReal","timeExponent":1,"value":"Infinity"}]'
);
});

it('can deserialize an array of primitives, fractions and monzos', () => {
const serialized =
'["Hello, world!",{"n":10,"d":7},{"type":"TimeReal","timeExponent":-1,"value":777},3.5,{"type":"TimeMonzo","timeExponent":{"n":0,"d":1},"primeExponents":[{"n":-4,"d":1},{"n":4,"d":1},{"n":-1,"d":1},{"n":0,"d":1},{"n":0,"d":1},{"n":0,"d":1},{"n":0,"d":1},{"n":0,"d":1},{"n":0,"d":1}],"residual":{"n":1,"d":1}},null]';
'["Hello, world!",{"n":10,"d":7},{"type":"TimeReal","timeExponent":-1,"value":777},3.5,{"type":"TimeMonzo","timeExponent":{"n":0,"d":1},"primeExponents":[{"n":-4,"d":1},{"n":4,"d":1},{"n":-1,"d":1},{"n":0,"d":1},{"n":0,"d":1},{"n":0,"d":1},{"n":0,"d":1},{"n":0,"d":1},{"n":0,"d":1}],"residual":{"n":1,"d":1}},null,{"type":"TimeReal","timeExponent":0,"value":"NaN"},{"type":"TimeReal","timeExponent":1,"value":"Infinity"}]';
function reviver(key: string, value: any) {
return TimeMonzo.reviver(
key,
TimeReal.reviver(key, Fraction.reviver(key, value))
);
}
const data = JSON.parse(serialized, reviver);
expect(data).toHaveLength(6);
expect(data).toHaveLength(8);

expect(data[0]).toBe('Hello, world!');

Expand All @@ -495,5 +497,13 @@ describe('JSON serialization', () => {
expect(data[4].toFraction().toFraction()).toBe('81/80');

expect(data[5]).toBeNull();

expect(data[6]).toBeInstanceOf(TimeReal);
expect(data[6].timeExponent).toBe(0);
expect(data[6].value).toBeNaN();

expect(data[7]).toBeInstanceOf(TimeReal);
expect(data[7].timeExponent).toBe(1);
expect(data[7].value).toBe(Infinity);
});
});
26 changes: 24 additions & 2 deletions src/monzo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ export class TimeReal {
if (!value) {
timeExponent = 0;
}
// Checks for Infinity and NaN
if (!isFinite(timeExponent)) {
throw new Error('Time exponent must be finite.');
}
this.timeExponent = timeExponent;
this.value = value;
}
Expand Down Expand Up @@ -233,7 +237,15 @@ export class TimeReal {
value !== null &&
value.type === 'TimeReal'
) {
return new TimeReal(value.timeExponent, value.value);
let v: number | string = value.value;
if (v === 'NaN') {
v = NaN;
} else if (v === 'Infinity') {
v = Infinity;
} else if (v === '-Infinity') {
v = -Infinity;
}
return new TimeReal(value.timeExponent, v as number);
}
return value;
}
Expand All @@ -243,10 +255,20 @@ export class TimeReal {
* @returns The serialized object with property `type` set to `'TimeReal'`.
*/
toJSON() {
let value: number | string = this.value;
// JSON sure is a standard
if (isNaN(value)) {
value = 'NaN';
} else if (value === Infinity) {
value = 'Infinity';
} else if (value === -Infinity) {
value = '-Infinity';
}

return {
type: 'TimeReal',
timeExponent: this.timeExponent,
value: this.value,
value,
};
}

Expand Down

0 comments on commit 7409e6b

Please sign in to comment.