lossless-json.js 7.1 KB

1
  1. !function(a,b){"object"==typeof exports&&"undefined"!=typeof module?b(exports):"function"==typeof define&&define.amd?define(["exports"],b):b((a=a||self).LosslessJSON={})}(this,function(a){"use strict";function b(a){return a&&void 0!==a.circularRefs&&null!==a.circularRefs&&(v=!0===a.circularRefs),{circularRefs:v}}function c(a){return(c="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a})(a)}function d(a,b){for(var c=0;c<b.length;c++){var d=b[c];d.enumerable=d.enumerable||!1,d.configurable=!0,"value"in d&&(d.writable=!0),Object.defineProperty(a,d.key,d)}}function e(a){return("string"!=typeof a?a+"":a).replace(/^-/,"").replace(/e.*$/,"").replace(/^0\.?0*|\./,"")}function f(a,b,d,e){return Array.isArray(d)?e.call(a,b,function(a,b){for(var c=[],d=0;d<a.length;d++)c[d]=f(a,d+"",a[d],b);return c}(d,e)):d&&"object"===c(d)&&!d.isLosslessNumber?e.call(a,b,function(a,b){var c={};for(var d in a)a.hasOwnProperty(d)&&(c[d]=f(a,d,a[d],b));return c}(d,e)):e.call(a,b,d)}function g(a){return encodeURIComponent(a.replace(/\//g,"~1").replace(/~/g,"~0"))}function h(a){return decodeURIComponent(a).replace(/~1/g,"/").replace(/~0/g,"~")}function i(a){return"#/"+a.map(g).join("/")}function j(){B++,C=A.charAt(B)}function k(){for(E=x.NULL,D="";" "===C||"\t"===C||"\n"===C||"\r"===C;)j();if(y[C])return E=x.DELIMITER,D=C,void j();if(m(C)||"-"===C){if(E=x.NUMBER,"-"===C){if(D+=C,j(),!m(C))throw n("Invalid number, digit expected",B)}else"0"===C&&(D+=C,j());for(;m(C);)D+=C,j();if("."===C){if(D+=C,j(),!m(C))throw n("Invalid number, digit expected",B);for(;m(C);)D+=C,j()}if("e"===C||"E"===C){if(D+=C,j(),"+"!==C&&"-"!==C||(D+=C,j()),!m(C))throw n("Invalid number, digit expected",B);for(;m(C);)D+=C,j()}}else if('"'!==C){if(!l(C)){for(E=x.UNKNOWN;""!==C;)D+=C,j();throw n('Syntax error in part "'+D+'"')}for(E=x.SYMBOL;l(C);)D+=C,j()}else{for(E=x.STRING,j();""!==C&&'"'!==C;)if("\\"===C){j();var a=z[C];if(void 0!==a)D+=a,j();else{if("u"!==C)throw n('Invalid escape character "\\'+C+'"',B);j();for(var b="",c=0;c<4;c++){if(!/^[0-9a-fA-F]/.test(C))throw n("Invalid unicode character");b+=C,j()}D+=String.fromCharCode(parseInt(b,16))}}else D+=C,j();if('"'!==C)throw n("End of string expected");j()}}function l(a){return/^[a-zA-Z_]/.test(a)}function m(a){return"0"<=a&&a<="9"}function n(a,b){void 0===b&&(b=B-D.length);var c=new SyntaxError(a+" (char "+b+")");return c.char=b,c}function o(){if(E!==x.DELIMITER||"{"!==D)return function(){if(E!==x.DELIMITER||"["!==D)return function(){if(E!==x.STRING)return function(){if(E!==x.NUMBER)return function(){if(E!==x.SYMBOL)return function(){throw n(""===D?"Unexpected end of json string":"Value expected")}();if("true"===D)return k(),!0;if("false"===D)return k(),!1;if("null"!==D)throw n('Unknown symbol "'+D+'"');return k(),null}();var a=new w(D);return k(),a}();var a=D;return k(),a}();k();var a=[];if(E===x.DELIMITER&&"]"===D)return k(),a;var b=G.length;for(G[b]=a;F[b]=a.length+"",a.push(o()),E===x.DELIMITER&&","===D;)k();if(E===x.DELIMITER&&"]"===D)return k(),G.length=b,F.length=b,a;throw n('Comma or end of array "]" expected')}();var a,c;k();var d={};if(E===x.DELIMITER&&"}"===D)return k(),d;var e=G.length;for(G[e]=d;;){if(E!==x.STRING)throw n("Object key expected");if(c=D,k(),E!==x.DELIMITER||":"!==D)throw n("Colon expected");if(k(),d[F[e]=c]=o(),E!==x.DELIMITER||","!==D)break;k()}if(E!==x.DELIMITER||"}"!==D)throw n('Comma or end of object "}" expected');return k(),"string"==typeof(a=d).$ref&&1===Object.keys(a).length?function(a){if(!b().circularRefs)return a;for(var c=function(a){var b=a.split("/").map(h);if("#"!==b.shift())throw SyntaxError("Cannot parse JSON Pointer: no valid URI fragment");return""===b[b.length-1]&&b.pop(),b}(a.$ref),d=0;d<c.length;d++)if(c[d]!==F[d])throw new Error('Invalid circular reference "'+a.$ref+'"');return G[c.length]}(d):(G.length=e,F.length=e,d)}function p(a,b,c){I=[],H=[];var d,e="function"==typeof b?b.call({"":a},"",a):a;return"number"==typeof c?10<c?d=u(" ",10):1<=c&&(d=u(" ",c)):"string"==typeof c&&""!==c&&(d=c),q(e,b,d,"")}function q(a,b,d,e){return"boolean"==typeof a||a instanceof Boolean||null===a||"number"==typeof a||a instanceof Number||"string"==typeof a||a instanceof String||a instanceof Date?JSON.stringify(a):a&&a.isLosslessNumber?a.value:Array.isArray(a)?function(a,b,c,d){var e=c?d+c:void 0,f=c?"[\n":"[";if(s(a))return t(a,b,c,d);var g=I.length;I[g]=a;for(var h=0;h<a.length;h++){var i=h+"",j="function"==typeof b?b.call(a,i,a[h]):a[h];c&&(f+=e),void 0!==j&&"function"!=typeof j?(H[g]=i,f+=q(j,b,c,e)):f+="null",h<a.length-1&&(f+=c?",\n":",")}return I.length=g,H.length=g,f+=c?"\n"+d+"]":"]"}(a,b,d,e):a&&"object"===c(a)?r(a,b,d,e):void 0}function r(a,b,c,d){var e=c?d+c:void 0,f=!0,g=c?"{\n":"{";if("function"==typeof a.toJSON)return p(a.toJSON(),b,c);if(s(a))return t(a,b,c,d);var h,i,j,k=I.length;for(var l in I[k]=a)if(a.hasOwnProperty(l)){var m="function"==typeof b?b.call(a,l,a[l]):a[l];h=l,j=b,void 0===(i=m)||"function"==typeof i||Array.isArray(j)&&!function(a,b){for(var c=0;c<a.length;c++)if(a[c]==b)return!0;return!1}(j,h)||(f?f=!1:g+=c?",\n":",",g+=c?e+'"'+l+'": ':'"'+l+'":',H[k]=l,g+=q(m,b,c,e))}return I.length=k,H.length=k,g+=c?"\n"+d+"}":"}"}function s(a){return-1!==I.indexOf(a)}function t(a,c,d,e){if(!b().circularRefs)throw new Error('Circular reference at "'+i(H)+'"');var f=I.indexOf(a);return r({$ref:i(H.slice(0,f))},c,d,e)}function u(a,b){for(var c="";0<b--;)c+=a;return c}var v=!0,w=function(){function a(b){!function(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}(this,a),this.value=function a(b){if("string"==typeof b){if(!/^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/.test(b))throw new Error('Invalid number (value: "'+b+'")');return b}if("number"!=typeof b)return a(b&&b.valueOf());if(15<e(b+"").length)throw new Error("Invalid number: contains more than 15 digits (value: "+b+")");if(isNaN(b))throw new Error("Invalid number: NaN");if(!isFinite(b))throw new Error("Invalid number: Infinity");return b+""}(b),this.type="LosslessNumber",this.isLosslessNumber=!0}var b,c,f;return b=a,(c=[{key:"valueOf",value:function(){var a=parseFloat(this.value),b=e(this.value);if(15<b.length)throw new Error("Cannot convert to number: number would be truncated (value: "+this.value+")");if(!isFinite(a))throw new Error("Cannot convert to number: number would overflow (value: "+this.value+")");if(Math.abs(a)<Number.MIN_VALUE&&!/^0*$/.test(b))throw new Error("Cannot convert to number: number would underflow (value: "+this.value+")");return a}},{key:"toString",value:function(){return this.value}}])&&d(b.prototype,c),f&&d(b,f),a}(),x={NULL:0,DELIMITER:1,NUMBER:2,STRING:3,SYMBOL:4,UNKNOWN:5},y={"":!0,"{":!0,"}":!0,"[":!0,"]":!0,":":!0,",":!0},z={'"':'"',"\\":"\\","/":"/",b:"\b",f:"\f",n:"\n",r:"\r",t:"\t"},A="",B=0,C="",D="",E=x.NULL,F=[],G=[],H=[],I=[];a.LosslessNumber=w,a.config=b,a.parse=function(a,b){B=0,C=(A=a).charAt(0),D="",E=x.NULL,G=[],F=[],k();var c,d=o();if(""!==D)throw n("Unexpected characters");return b?f({"":c=d},"",c,b):d},a.stringify=p,Object.defineProperty(a,"__esModule",{value:!0})});