Skip to content

Commit

Permalink
Merge pull request #2 from cyberphone/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
cyberphone committed Feb 5, 2019
2 parents d8bbdd4 + ccd57f5 commit 5cf6072
Show file tree
Hide file tree
Showing 3 changed files with 315 additions and 37 deletions.
72 changes: 72 additions & 0 deletions broken.js
@@ -0,0 +1,72 @@
// Sample showing how a naive use of JCS will fail
'use strict';
var canonicalize = function(object) {

var buffer = '';
serialize(object);
return buffer;

function serialize(object) {
if (object === null || typeof object !== 'object' ||
object.toJSON != null) {
/////////////////////////////////////////////////
// Primitive type or toJSON - Use ES6/JSON //
/////////////////////////////////////////////////
buffer += JSON.stringify(object);

} else if (Array.isArray(object)) {
/////////////////////////////////////////////////
// Array - Maintain element order //
/////////////////////////////////////////////////
buffer += '[';
let next = false;
object.forEach((element) => {
if (next) {
buffer += ',';
}
next = true;
/////////////////////////////////////////
// Array element - Recursive expansion //
/////////////////////////////////////////
serialize(element);
});
buffer += ']';

} else {
/////////////////////////////////////////////////
// Object - Sort properties before serializing //
/////////////////////////////////////////////////
buffer += '{';
let next = false;
Object.keys(object).sort().forEach((property) => {
if (next) {
buffer += ',';
}
next = true;
///////////////////////////////////////////////
// Property names are strings - Use ES6/JSON //
///////////////////////////////////////////////
buffer += JSON.stringify(property);
buffer += ':';
//////////////////////////////////////////
// Property value - Recursive expansion //
//////////////////////////////////////////
serialize(object[property]);
});
buffer += '}';
}
}
};

const jstring =
'{"time": "2019-01-28T07:45:10Z", "big": "055", "val": 3.5}';

BigInt.prototype.toJSON = function () {
return this.toString();
};

var object = JSON.parse(jstring,
(k,v) => k == 'time' ? new Date(v) : k == 'big' ? BigInt(v) : v
);

console.log(canonicalize(object));

0 comments on commit 5cf6072

Please sign in to comment.