Skip to content
This repository was archived by the owner on Mar 5, 2023. It is now read-only.

Commit 1efadb4

Browse files
committed
Feature: single (non-object) values can be serlialized
1 parent b267398 commit 1efadb4

File tree

5 files changed

+285
-39
lines changed

5 files changed

+285
-39
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ jsonStringify(obj); // '{"foo":42,"hello":"world"}'
6666

6767
### stableJsonStringify(*value*,[ *compareFunction*[, *safe*]])
6868

69-
A deterministic version of `jsonStringify()`. It performs the same as `jsonStringify()` with the exception that object entries are sorted before being serialized. This ensures that consistent output is produced for the same input.
69+
A deterministic version of `jsonStringify()`. It performs the same as `jsonStringify()` with the exception that object entries are sorted before being serialized. This ensures that consistent output is produced for the same input, at the cost of reduced performance.
7070

7171
#### Parameters
7272

__tests__/json-stringify.test.ts

Lines changed: 101 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,78 @@
11
import { jsonStringify } from '../src/index';
22

3+
describe('Serializing a single value returns the same result as JSON.stringfy()', () => {
4+
test('undefined', () => {
5+
const expectedResult = JSON.stringify(undefined);
6+
const result = jsonStringify(undefined);
7+
8+
expect(result).toEqual(expectedResult);
9+
});
10+
11+
test('boolean', () => {
12+
const expectedResult = JSON.stringify(true);
13+
const result = jsonStringify(true);
14+
15+
expect(result).toEqual(expectedResult);
16+
});
17+
18+
test('number', () => {
19+
const expectedResult = JSON.stringify(42);
20+
const result = jsonStringify(42);
21+
22+
expect(result).toEqual(expectedResult);
23+
});
24+
25+
test('number (Infinity)', () => {
26+
const expectedResult = JSON.stringify(Infinity);
27+
const result = jsonStringify(Infinity);
28+
29+
expect(result).toEqual(expectedResult);
30+
});
31+
32+
test('number (NaN)', () => {
33+
const expectedResult = JSON.stringify(NaN);
34+
const result = jsonStringify(NaN);
35+
36+
expect(result).toEqual(expectedResult);
37+
});
38+
39+
test('string', () => {
40+
const expectedResult = JSON.stringify('abc');
41+
const result = jsonStringify('abc');
42+
43+
expect(result).toEqual(expectedResult);
44+
});
45+
46+
test('null', () => {
47+
const expectedResult = JSON.stringify(null);
48+
const result = jsonStringify(null);
49+
50+
expect(result).toEqual(expectedResult);
51+
});
52+
53+
test('buffer', () => {
54+
const buf = Buffer.from('abc');
55+
const expectedResult = JSON.stringify(buf);
56+
const result = jsonStringify(buf);
57+
58+
expect(result).toEqual(expectedResult);
59+
});
60+
61+
test('date', () => {
62+
const date = new Date();
63+
const expectedResult = JSON.stringify(date);
64+
const result = jsonStringify(date);
65+
66+
expect(result).toEqual(expectedResult);
67+
});
68+
});
69+
70+
describe('Passing a single BigInt', () => {
71+
test('throws an error', () => {
72+
expect(() => jsonStringify(BigInt(42))).toThrow('Cannot serialize a BigInt');
73+
});
74+
});
75+
376
describe('Calling JSON.parse() on a stringifed object with all possible values returns the same result as JSON.stringify()', () => {
477
const obj = {
578
a: true,
@@ -62,31 +135,47 @@ describe('Calling JSON.parse() on a stringifed object with all possible values r
62135
];
63136

64137
test('Standard object', () => {
65-
const expectedResult = JSON.parse(JSON.stringify(obj));
66-
const result = JSON.parse(jsonStringify(obj));
138+
const expectedResult = JSON.stringify(obj);
139+
const result = jsonStringify(obj);
67140

68-
expect(result).toEqual(expectedResult);
141+
expect(result).toBeDefined();
142+
143+
if (result !== undefined) {
144+
expect(JSON.parse(result)).toEqual(JSON.parse(expectedResult));
145+
}
69146
});
70147

71148
test('Array', () => {
72-
const expectedResult = JSON.parse(JSON.stringify(arr));
73-
const result = JSON.parse(jsonStringify(arr));
149+
const expectedResult = JSON.stringify(arr);
150+
const result = jsonStringify(arr);
74151

75-
expect(result).toEqual(expectedResult);
152+
expect(result).toBeDefined();
153+
154+
if (result !== undefined) {
155+
expect(JSON.parse(result)).toEqual(JSON.parse(expectedResult));
156+
}
76157
});
77158

78159
test('Standard object, safe set to false', () => {
79-
const expectedResult = JSON.parse(JSON.stringify(obj));
80-
const result = JSON.parse(jsonStringify(obj, false));
160+
const expectedResult = JSON.stringify(obj);
161+
const result = jsonStringify(obj, false);
81162

82-
expect(result).toEqual(expectedResult);
163+
expect(result).toBeDefined();
164+
165+
if (result !== undefined) {
166+
expect(JSON.parse(result)).toEqual(JSON.parse(expectedResult));
167+
}
83168
});
84169

85170
test('Array, safe set to false', () => {
86-
const expectedResult = JSON.parse(JSON.stringify(arr));
87-
const result = JSON.parse(jsonStringify(arr, false));
171+
const expectedResult = JSON.stringify(arr);
172+
const result = jsonStringify(arr, false);
88173

89-
expect(result).toEqual(expectedResult);
174+
expect(result).toBeDefined();
175+
176+
if (result !== undefined) {
177+
expect(JSON.parse(result)).toEqual(JSON.parse(expectedResult));
178+
}
90179
});
91180
});
92181

__tests__/stable-json-stringify.test.ts

Lines changed: 101 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,78 @@
11
import { stableJsonStringify } from '../src/index';
22

3+
describe('Serializing a single value returns the same result as JSON.stringfy()', () => {
4+
test('undefined', () => {
5+
const expectedResult = JSON.stringify(undefined);
6+
const result = stableJsonStringify(undefined);
7+
8+
expect(result).toEqual(expectedResult);
9+
});
10+
11+
test('boolean', () => {
12+
const expectedResult = JSON.stringify(true);
13+
const result = stableJsonStringify(true);
14+
15+
expect(result).toEqual(expectedResult);
16+
});
17+
18+
test('number', () => {
19+
const expectedResult = JSON.stringify(42);
20+
const result = stableJsonStringify(42);
21+
22+
expect(result).toEqual(expectedResult);
23+
});
24+
25+
test('number (Infinity)', () => {
26+
const expectedResult = JSON.stringify(Infinity);
27+
const result = stableJsonStringify(Infinity);
28+
29+
expect(result).toEqual(expectedResult);
30+
});
31+
32+
test('number (NaN)', () => {
33+
const expectedResult = JSON.stringify(NaN);
34+
const result = stableJsonStringify(NaN);
35+
36+
expect(result).toEqual(expectedResult);
37+
});
38+
39+
test('string', () => {
40+
const expectedResult = JSON.stringify('abc');
41+
const result = stableJsonStringify('abc');
42+
43+
expect(result).toEqual(expectedResult);
44+
});
45+
46+
test('null', () => {
47+
const expectedResult = JSON.stringify(null);
48+
const result = stableJsonStringify(null);
49+
50+
expect(result).toEqual(expectedResult);
51+
});
52+
53+
test('buffer', () => {
54+
const buf = Buffer.from('abc');
55+
const expectedResult = JSON.stringify(buf);
56+
const result = stableJsonStringify(buf);
57+
58+
expect(result).toEqual(expectedResult);
59+
});
60+
61+
test('date', () => {
62+
const date = new Date();
63+
const expectedResult = JSON.stringify(date);
64+
const result = stableJsonStringify(date);
65+
66+
expect(result).toEqual(expectedResult);
67+
});
68+
});
69+
70+
describe('Passing a single BigInt', () => {
71+
test('throws an error', () => {
72+
expect(() => stableJsonStringify(BigInt(42))).toThrow('Cannot serialize a BigInt');
73+
});
74+
});
75+
376
describe('Calling JSON.parse() on a stringifed object with all possible values returns the same result as JSON.stringify()', () => {
477
const obj = {
578
a: true,
@@ -62,31 +135,47 @@ describe('Calling JSON.parse() on a stringifed object with all possible values r
62135
];
63136

64137
test('Standard object', () => {
65-
const expectedResult = JSON.parse(JSON.stringify(obj));
66-
const result = JSON.parse(stableJsonStringify(obj));
138+
const expectedResult = JSON.stringify(obj);
139+
const result = stableJsonStringify(obj);
67140

68-
expect(result).toEqual(expectedResult);
141+
expect(result).toBeDefined();
142+
143+
if (result !== undefined) {
144+
expect(JSON.parse(result)).toEqual(JSON.parse(expectedResult));
145+
}
69146
});
70147

71148
test('Array', () => {
72-
const expectedResult = JSON.parse(JSON.stringify(arr));
73-
const result = JSON.parse(stableJsonStringify(arr));
149+
const expectedResult = JSON.stringify(arr);
150+
const result = stableJsonStringify(arr);
74151

75-
expect(result).toEqual(expectedResult);
152+
expect(result).toBeDefined();
153+
154+
if (result !== undefined) {
155+
expect(JSON.parse(result)).toEqual(JSON.parse(expectedResult));
156+
}
76157
});
77158

78159
test('Standard object, safe set to false', () => {
79-
const expectedResult = JSON.parse(JSON.stringify(obj));
80-
const result = JSON.parse(stableJsonStringify(obj, null, false));
160+
const expectedResult = JSON.stringify(obj);
161+
const result = stableJsonStringify(obj, null, false);
81162

82-
expect(result).toEqual(expectedResult);
163+
expect(result).toBeDefined();
164+
165+
if (result !== undefined) {
166+
expect(JSON.parse(result)).toEqual(JSON.parse(expectedResult));
167+
}
83168
});
84169

85170
test('Array, safe set to false', () => {
86-
const expectedResult = JSON.parse(JSON.stringify(arr));
87-
const result = JSON.parse(stableJsonStringify(arr, null, false));
171+
const expectedResult = JSON.stringify(arr);
172+
const result = stableJsonStringify(arr, null, false);
88173

89-
expect(result).toEqual(expectedResult);
174+
expect(result).toBeDefined();
175+
176+
if (result !== undefined) {
177+
expect(JSON.parse(result)).toEqual(JSON.parse(expectedResult));
178+
}
90179
});
91180
});
92181

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tuft/json-stringify",
3-
"version": "1.0.0-alpha9",
3+
"version": "1.0.0-alpha10",
44
"description": "Serialize JavaScript objects to JSON strings.",
55
"author": "Stuart Kennedy",
66
"license": "MIT",

0 commit comments

Comments
 (0)