-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace dumb JSON encoding by more sophisticated encoding.
The new encoding can remember types and saves space when storing Uint8Arrays. Also increases the version number and simplifies access to JDB objects in tests.
- Loading branch information
Showing
32 changed files
with
385 additions
and
276 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class BufferUtils { | ||
/** | ||
* @param {*} buffer | ||
* @return {string} | ||
*/ | ||
static toBase64(buffer) { | ||
return btoa(String.fromCharCode(...new Uint8Array(buffer))); | ||
} | ||
|
||
/** | ||
* @param {string} base64 | ||
* @return {SerialBuffer} | ||
*/ | ||
static fromBase64(base64) { | ||
return Uint8Array.from(atob(base64), c => c.charCodeAt(0)); | ||
} | ||
} | ||
|
||
Class.register(BufferUtils); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
class JSONUtils { | ||
static stringify(value) { | ||
return JSON.stringify(value, JSONUtils.jsonifyType); | ||
} | ||
|
||
static parse(value) { | ||
return JSON.parse(value, JSONUtils.parseType); | ||
} | ||
|
||
static parseType(key, value) { | ||
if (value[JSONUtils.TYPE_SYMBOL]) { | ||
switch (value[JSONUtils.TYPE_SYMBOL]) { | ||
case 'Uint8Array': | ||
return BufferUtils.fromBase64(value[JSONUtils.VALUE_SYMBOL]); | ||
case 'Set': | ||
return Set.from(value[JSONUtils.VALUE_SYMBOL]); | ||
} | ||
} | ||
return value; | ||
} | ||
|
||
static jsonifyType(key, value) { | ||
if (value instanceof Uint8Array) { | ||
return JSONUtils.typedObject('Uint8Array', BufferUtils.toBase64(value)); | ||
} | ||
if (value instanceof Set) { | ||
return JSONUtils.typedObject('Set', Array.from(value)); | ||
} | ||
return value; | ||
} | ||
|
||
static typedObject(type, value) { | ||
const obj = {}; | ||
obj[JSONUtils.TYPE_SYMBOL] = type; | ||
obj[JSONUtils.VALUE_SYMBOL] = value; | ||
return obj; | ||
} | ||
} | ||
JSONUtils.TYPE_SYMBOL = '__'; | ||
JSONUtils.VALUE_SYMBOL = 'value'; | ||
|
||
Class.register(JSONUtils); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.