-
Notifications
You must be signed in to change notification settings - Fork 12
/
rjson.js
255 lines (223 loc) · 7.81 KB
/
rjson.js
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
Copyright (c) 2012, Dmytro V. Dogadailo <[email protected]>
RJSON is Recursive JSON.
RJSON converts any JSON data collection into more compact recursive
form. Compressed data is still JSON and can be parsed with `JSON.parse`. RJSON
can compress not only homogeneous collections, but any data sets with free
structure.
RJSON is stream single-pass compressor, it extracts data schemes from a
document, assign each schema unique number and use this number instead of
repeating same property names again and again.
Bellow you can see same document in both forms.
JSON:
{
"id": 7,
"tags": ["programming", "javascript"],
"users": [
{"first": "Homer", "last": "Simpson"},
{"first": "Hank", "last": "Hill"},
{"first": "Peter", "last": "Griffin"}
],
"books": [
{"title": "JavaScript", "author": "Flanagan", "year": 2006},
{"title": "Cascading Style Sheets", "author": "Meyer", "year": 2004}
]
}
RJSON:
{
"id": 7,
"tags": ["programming", "javascript"],
"users": [
{"first": "Homer", "last": "Simpson"},
[2, "Hank", "Hill", "Peter", "Griffin"]
],
"books": [
{"title": "JavaScript", "author": "Flanagan", "year": 2006},
[3, "Cascading Style Sheets", "Meyer", 2004]
]
}
RJSON allows to:
* reduce JSON data size and network traffic when gzip isn't available. For
example, in-browser 3D-modeling tools like [Mydeco
3D-planner](http://mydeco.com/3d-planner/) may process and send to server
megabytes of JSON-data;
* analyze large collections of JSON-data without
unpacking of whole dataset. RJSON-data is still JSON-data, so it can be
traversed and analyzed after parsing and fully unpacked only if a document meets
some conditions.
*/
var RJSON = (function() {
'use strict';
var hasOwnProperty = Object.prototype.hasOwnProperty,
toString = Object.prototype.toString,
getKeys = Object.keys || _keys,
isArray = Array.isArray || _isArray;
/**
* @param {*} Any valid for JSON javascript data.
* @return {*} Packed javascript data, usually a dictionary.
*/
function pack(data) {
var schemas = {}, maxSchemaIndex = 0;
function encodeArray(value) {
var len = value.length, encoded = [];
if (len === 0) return [];
if (typeof value[0] === 'number') {
encoded.push(0); // 0 is schema index for Array
}
for (var i = 0; i < len; i++) {
var v = value[i],
current = encode(v),
last = encoded[encoded.length - 1];
if (isEncodedObject(current) &&
isArray(last) && current[0] === last[0]) {
// current and previous object have same schema,
// so merge their values into one array
encoded[encoded.length - 1] =
last.concat(current.slice(1));
} else {
encoded.push(current);
}
}
return encoded;
}
function encodeObject(value) {
var schemaKeys = getKeys(value).sort();
if (schemaKeys.length === 0) {
return {};
}
var encoded,
schema = schemaKeys.length + ':' + schemaKeys.join('|'),
schemaIndex = schemas[schema];
if (schemaIndex) { // known schema
encoded = [schemaIndex];
for (var i = 0, k; k = schemaKeys[i++]; ) {
encoded[i] = encode(value[k]);
}
} else { // new schema
schemas[schema] = ++maxSchemaIndex;
encoded = {};
for (var i = 0, k; k = schemaKeys[i++]; ) {
encoded[k] = encode(value[k]);
}
}
return encoded;
}
function encode(value) {
if (typeof value !== 'object' || !value) {
// non-objects or null return as is
return value;
} else if (isArray(value)) {
return encodeArray(value);
} else {
return encodeObject(value);
}
}
return encode(data);
}
/**
* @param {*} data Packed javascript data.
* @return {*} Original data.
*/
function unpack(data) {
var schemas = {}, maxSchemaIndex = 0;
function parseArray(value) {
if (value.length === 0) {
return [];
} else if (value[0] === 0 || typeof value[0] !== 'number') {
return decodeArray(value);
} else {
return decodeObject(value);
}
}
function decodeArray(value) {
var len = value.length, decoded = []; // decode array of something
for (var i = (value[0] === 0 ? 1 : 0); i < len; i++) {
var v = value[i], obj = decode(v);
if (isEncodedObject(v) && isArray(obj)) {
// several objects was encoded into single array
decoded = decoded.concat(obj);
} else {
decoded.push(obj);
}
}
return decoded;
}
function decodeObject(value) {
var schemaKeys = schemas[value[0]],
schemaLen = schemaKeys.length,
total = (value.length - 1) / schemaLen,
decoded;
if (total > 1) {
decoded = []; // array of objects with same schema
for (var i = 0; i < total; i++) {
var obj = {};
for (var j = 0, k; k = schemaKeys[j++]; ) {
obj[k] = decode(value[i * schemaLen + j]);
}
decoded.push(obj);
}
} else {
decoded = {};
for (var j = 0, k; k = schemaKeys[j++]; ) {
decoded[k] = decode(value[j]);
}
}
return decoded;
}
function decodeNewObject(value) {
var schemaKeys = getKeys(value).sort();
if (schemaKeys.length === 0) {
return {};
}
schemas[++maxSchemaIndex] = schemaKeys;
var decoded = {};
for (var i = 0, k; k = schemaKeys[i++]; ) {
decoded[k] = decode(value[k]);
}
return decoded;
}
function decode(value) {
if (typeof value !== 'object' || !value) {
// non-objects or null return as is
return value;
} else if (isArray(value)) {
return parseArray(value);
} else { // object with new schema
return decodeNewObject(value);
}
}
return decode(data);
}
/**
* Object is encoded as array and object schema index is stored as
* first item of the array. Valid schema index should be greater than 0,
* because 0 is reserved for Array schema.
* Several objects with same schema can be stored in the one array.
* @param {*} value encoded value to check.
* @return {boolean} true if value contains an encoded object or several
* objects with same schema.
*/
function isEncodedObject(value) {
return isArray(value) && typeof value[0] === 'number' && value[0] !== 0;
}
function _keys(obj) {
var keys = [], k;
for (k in obj) {
if (hasOwnProperty.call(obj, k)) {
keys.push(k);
}
}
return keys;
}
function _isArray(obj) {
return toString.apply(obj) === '[object Array]';
}
return {
pack: pack,
unpack: unpack
};
}());
// export for node.js
if (typeof module != 'undefined' && module.exports) {
module.exports = RJSON;
}