1 | 'use strict'
|
2 |
|
3 | import {jsonStableSerialize} from '../util';
|
4 | import test from 'tape';
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | function objeq(a, b) {
|
12 | if( a == b ) {
|
13 | return true;
|
14 | }
|
15 |
|
16 | var aProps = Object.getOwnPropertyNames(a);
|
17 | var bProps = Object.getOwnPropertyNames(b);
|
18 |
|
19 | if (aProps.length != bProps.length) {
|
20 | return false;
|
21 | }
|
22 |
|
23 | for (var i = 0; i < aProps.length; i++) {
|
24 | var propName = aProps[i];
|
25 | if( !objeq(a[propName], b[propName]) ) {
|
26 | return false;
|
27 | }
|
28 | }
|
29 |
|
30 | return true;
|
31 | }
|
32 |
|
33 |
|
34 | export function json_stable_serialize_tests() {
|
35 |
|
36 | const serializable_objects = [
|
37 | '',
|
38 | 1,
|
39 | true,
|
40 | 'hello',
|
41 | [],
|
42 | [1, 2],
|
43 | {},
|
44 | {'a': 'b'},
|
45 | {'a': 1, 'b': '2'},
|
46 | ['1', '2', '3'],
|
47 | [1, 2, 3],
|
48 | [{'a': 'b'}, {'c': 'd'}],
|
49 | [{'a': [1, 2, 3], 'b': [4, 5, 6]}],
|
50 | {'c': [{'a': 1, 'c': 2, 'b': 3}, {'f': 4, 'e': 5, 'd': 6}], 'a': [4, 1, 2, 3, '4', '5', '6'], 'd': true, 'b': 'false'}
|
51 | ];
|
52 |
|
53 | test('serialize and deserialize JSON', (t) => {
|
54 | t.plan(serializable_objects.length);
|
55 |
|
56 |
|
57 | for (var serializable_obj of serializable_objects) {
|
58 | console.log(`object: ${serializable_obj}`);
|
59 |
|
60 | const jsonstr = jsonStableSerialize(serializable_obj);
|
61 | console.log(`jsonstr: ${jsonstr}`);
|
62 |
|
63 | const reloaded_obj = JSON.parse(jsonstr);
|
64 | console.log(`reload: ${reloaded_obj}`);
|
65 |
|
66 | t.ok(objeq(reloaded_obj, serializable_obj), 'deserialized serialized object must equal the original object')
|
67 | }
|
68 | });
|
69 | }
|
70 |
|