333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
'use strict';
 
const debug = require('debug')('serialize-json#JSONEncoder');
const is = require('is-type-of');
const utility = require('utility');
 
const REG_STR_REPLACER = /[\+ \|\^\%]/g;
const ENCODER_REPLACER = {
  ' ': '+',
  '+': '%2B',
  '|': '%7C',
  '^': '%5E',
  '%': '%25',
};
 
const TOKEN_TRUE = -1;
const TOKEN_FALSE = -2;
const TOKEN_NULL = -3;
const TOKEN_EMPTY_STRING = -4;
const TOKEN_UNDEFINED = -5;
 
class JSONEncoder {
  constructor() {
    this.dictionary = null;
  }
 
  encode(json) {
    this.dictionary = {
      strings: [],
      integers: [],
      floats: [],
      dates: [],
    };
    const ast = this._buildAst(json);
 
    let packed = this.dictionary.strings.join('|');
    packed += `^${this.dictionary.integers.join('|')}`;
    packed += `^${this.dictionary.floats.join('|')}`;
    packed += `^${this.dictionary.dates.join('|')}`;
    packed += `^${this._pack(ast)}`;
 
    debug('pack the json => %s', packed);
    return Buffer.from(packed);
  }
 
  _pack(ast) {
    if (is.array(ast)) {
      let packed = ast.shift();
      for (const item of ast) {
        packed += `${this._pack(item)}|`;
      }
      return (packed[packed.length - 1] === '|' ? packed.slice(0, -1) : packed) + ']';
    }
    const type = ast.type;
    const index = ast.index;
    const dictionary = this.dictionary;
    const strLen = dictionary.strings.length;
    const intLen = dictionary.integers.length;
    const floatLen = dictionary.floats.length;
 
    switch (type) {
      case 'string':
        return this._base10To36(index);
      case 'integer':
        return this._base10To36(strLen + index);
      case 'float':
        return this._base10To36(strLen + intLen + index);
      case 'date':
        return this._base10To36(strLen + intLen + floatLen + index);
      default:
        return this._base10To36(index);
    }
  }
 
  _encodeString(str) {
    return str.replace(REG_STR_REPLACER, a => ENCODER_REPLACER[a]);
  }
 
  _base10To36(num) {
    return num.toString(36).toUpperCase();
  }
 
  _dateTo36(date) {
    return this._base10To36(date.getTime());
  }
 
  _buildStringAst(str) {
    const dictionary = this.dictionary;
    if (str === '') {
      return {
        type: 'empty',
        index: TOKEN_EMPTY_STRING,
      };
    }
    const data = this._encodeString(str);
    return {
      type: 'string',
      index: dictionary.strings.push(data) - 1,
    };
  }
 
  _buildNumberAst(num) {
    const dictionary = this.dictionary;
    // integer
    if (num % 1 === 0) {
      const data = this._base10To36(num);
      return {
        type: 'integer',
        index: dictionary.integers.push(data) - 1,
      };
    }
    // float
    return {
      type: 'float',
      index: dictionary.floats.push(num) - 1,
    };
  }
 
  _buildObjectAst(obj) {
    if (obj === null) {
      return {
        type: 'null',
        index: TOKEN_NULL,
      };
    }
    if (is.date(obj)) {
      const dictionary = this.dictionary;
      const data = this._dateTo36(obj);
      return {
        type: 'date',
        index: dictionary.dates.push(data) - 1,
      };
    }
    let ast;
    if (is.array(obj)) {
      ast = [ '@' ];
      for (const item of obj) {
        ast.push(this._buildAst(item));
      }
      return ast;
    }
    if (is.buffer(obj)) {
      ast = [ '*' ];
      for (const item of obj.values()) {
        ast.push(this._buildAst(item));
      }
      return ast;
    }
    if (is.error(obj)) {
      ast = [ '#' ];
      ast.push(this._buildAst('message'));
      ast.push(this._buildAst(obj.message));
      ast.push(this._buildAst('stack'));
      ast.push(this._buildAst(obj.stack));
    } else {
      ast = [ '$' ];
    }
    for (const key in obj) {
      // support object without prototype, like: Object.create(null)
      if (!utility.has(obj, key)) {
        continue;
      }
      ast.push(this._buildAst(key));
      ast.push(this._buildAst(obj[key]));
    }
    return ast;
  }
 
  _buildAst(item) {
    const type = typeof item;
    debug('calling buildAst with type: %s and data: %j', type, item);
 
    switch (type) {
      case 'string':
        return this._buildStringAst(item);
      case 'number':
        return this._buildNumberAst(item);
      case 'boolean':
        return {
          type: 'boolean',
          index: item ? TOKEN_TRUE : TOKEN_FALSE,
        };
      case 'undefined':
        return {
          type: 'undefined',
          index: TOKEN_UNDEFINED,
        };
      case 'object':
        return this._buildObjectAst(item);
      default:
        debug('unsupported type: %s, return null', type);
        return {
          type: 'null',
          index: TOKEN_NULL,
        };
    }
  }
}
 
module.exports = JSONEncoder;