Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

lossless-json

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lossless-json - npm Package Compare versions

Comparing version 2.0.6 to 2.0.7

51

lib/esm/parse.js

@@ -0,1 +1,8 @@

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
import { parseLosslessNumber } from './numberParsers.js';

@@ -45,2 +52,3 @@ import { revive } from './revive.js';

}
var start = i;
var key = parseString();

@@ -50,10 +58,13 @@ if (key === undefined) {

}
if (Object.prototype.hasOwnProperty.call(object, key)) {
skipWhitespace();
eatColon();
var _value = parseValue();
// TODO: test deep equal instead of strict equal
if (Object.prototype.hasOwnProperty.call(object, key) && !isDeepEqual(_value, object[key])) {
// Note that we could also test `if(key in object) {...}`
// or `if (object[key] !== 'undefined') {...}`, but that is slower.
throwDuplicateKey(key);
throwDuplicateKey(key, start + 1);
}
skipWhitespace();
eatColon();
object[key] = parseValue();
object[key] = _value;
}

@@ -79,5 +90,5 @@ if (text.charCodeAt(i) !== codeClosingBrace) {

}
var _value = parseValue();
expectArrayItem(_value);
array.push(_value);
var _value2 = parseValue();
expectArrayItem(_value2);
array.push(_value2);
}

@@ -220,4 +231,4 @@ if (text.charCodeAt(i) !== codeClosingBracket) {

}
function throwDuplicateKey(key) {
throw new SyntaxError("Duplicate key '".concat(key, "' encountered at position ").concat(i - key.length - 1));
function throwDuplicateKey(key, pos) {
throw new SyntaxError("Duplicate key '".concat(key, "' encountered at position ").concat(pos));
}

@@ -272,2 +283,22 @@ function throwObjectKeyOrEndExpected() {

}
export function isDeepEqual(a, b) {
if (a === b) {
return true;
}
if (Array.isArray(a) && Array.isArray(b)) {
return a.length === b.length && a.every(function (item, index) {
return isDeepEqual(item, b[index]);
});
}
if (isObject(a) && isObject(b)) {
var keys = _toConsumableArray(new Set([].concat(_toConsumableArray(Object.keys(a)), _toConsumableArray(Object.keys(b)))));
return keys.every(function (key) {
return isDeepEqual(a[key], b[key]);
});
}
return false;
}
function isObject(value) {
return _typeof(value) === 'object' && value !== null;
}

@@ -274,0 +305,0 @@ // map with all escape characters

3

lib/types/parse.d.ts

@@ -0,3 +1,3 @@

import type { NumberParser, Reviver } from './types';
import { JavaScriptValue } from './types';
import type { NumberParser, Reviver } from './types';
/**

@@ -27,2 +27,3 @@ * The LosslessJSON.parse() method parses a string as JSON, optionally transforming

export declare function isValidStringCharacter(code: number): boolean;
export declare function isDeepEqual(a: unknown, b: unknown): boolean;
export declare const codeUppercaseA = 65;

@@ -29,0 +30,0 @@ export declare const codeLowercaseA = 97;

@@ -1,1 +0,1 @@

!function(t,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r((t="undefined"!=typeof globalThis?globalThis:t||self).LosslessJSON={})}(this,(function(t){"use strict";function r(t){return n.test(t)}var n=/^-?[0-9]+$/;function e(t){return a.test(t)}var o,a=/^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/;function i(t,n){var e=parseFloat(t),o=String(e),a=u(t),i=u(o);if(a===i)return!0;if(!0===(null==n?void 0:n.approx)){if(!r(t)&&i.length>=14&&a.startsWith(i.substring(0,14)))return!0}return!1}function c(n){if(!i(n,{approx:!1})){if(r(n))return t.UnsafeNumberReason.truncate_integer;var e=parseFloat(n);return isFinite(e)?0===e?t.UnsafeNumberReason.underflow:t.UnsafeNumberReason.truncate_float:t.UnsafeNumberReason.overflow}}function u(t){return t.replace(f,"").replace(s,"").replace(d,"").replace(l,"")}t.UnsafeNumberReason=void 0,(o=t.UnsafeNumberReason||(t.UnsafeNumberReason={})).underflow="underflow",o.overflow="overflow",o.truncate_integer="truncate_integer",o.truncate_float="truncate_float";var f=/[eE][+-]?\d+$/,l=/^-?(0*)?/,s=/\./,d=/0+$/;function y(t){return y="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},y(t)}function v(t,r){for(var n=0;n<r.length;n++){var e=r[n];e.enumerable=e.enumerable||!1,e.configurable=!0,"value"in e&&(e.writable=!0),Object.defineProperty(t,p(e.key),e)}}function p(t){var r=function(t,r){if("object"!==y(t)||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var e=n.call(t,r||"default");if("object"!==y(e))return e;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===r?String:Number)(t)}(t,"string");return"symbol"===y(r)?r:String(r)}var h=function(){function n(t){if(function(t,r){if(!(t instanceof r))throw new TypeError("Cannot call a class as a function")}(this,n),function(t,r,n){(r=p(r))in t?Object.defineProperty(t,r,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[r]=n}(this,"isLosslessNumber",!0),!e(t))throw new Error('Invalid number (value: "'+t+'")');this.value=t}var o,a,i;return o=n,(a=[{key:"valueOf",value:function(){var n=c(this.value);if(void 0===n||n===t.UnsafeNumberReason.truncate_float)return parseFloat(this.value);if(r(this.value))return BigInt(this.value);throw new Error("Cannot safely convert to number: "+"the value '".concat(this.value,"' would ").concat(n," and become ").concat(parseFloat(this.value)))}},{key:"toString",value:function(){return this.value}}])&&v(o.prototype,a),i&&v(o,i),Object.defineProperty(o,"prototype",{writable:!1}),n}();function b(t){return t&&"object"===y(t)&&!0===t.isLosslessNumber||!1}function m(t){return new h(t)}function w(t){return w="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},w(t)}function g(t,r){return S({"":t},"",t,r)}function S(t,r,n,e){return Array.isArray(n)?e.call(t,r,function(t,r){for(var n=0;n<t.length;n++)t[n]=S(t,n+"",t[n],r);return t}(n,e)):n&&"object"===w(n)&&!b(n)?e.call(t,r,function(t,r){return Object.keys(t).forEach((function(n){var e=S(t,n,t[n],r);void 0!==e?t[n]=e:delete t[n]})),t}(n,e)):e.call(t,r,n)}function A(t){return t===_||t===F||t===J||t===L}function C(t){return t>=B&&t<=Q||t>=z&&t<=M||t>=G&&t<=V}function N(t){return t>=B&&t<=Q}function x(t){return t>=D&&t<=Q}function E(t){return t>=32&&t<=1114111}var O={'"':'"',"\\":"\\","/":"/",b:"\b",f:"\f",n:"\n",r:"\r",t:"\t"},j=92,I=123,R=125,k=91,U=93,_=32,F=10,J=9,L=13,P=34,T=43,$=45,B=48,D=49,Q=57,W=44,Z=46,q=58,z=65,G=97,H=69,K=101,M=70,V=102;function X(t){return X="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},X(t)}var Y=/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/;t.LosslessNumber=h,t.config=function(t){throw new Error("config is deprecated, support for circularRefs is removed from the library. If you encounter circular references in your data structures, please rethink your datastructures: better prevent circular references in the first place.")},t.getUnsafeNumberReason=c,t.isInteger=r,t.isLosslessNumber=b,t.isNumber=e,t.isSafeNumber=i,t.parse=function(t,r){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:m,e=0,o=c();return v(o),h(),r?g(o,r):o;function a(){if(t.charCodeAt(e)===I){e++,f();for(var r={},n=!0;e<t.length&&t.charCodeAt(e)!==R;){n?n=!1:(d(),f());var o=l();void 0===o&&S(),Object.prototype.hasOwnProperty.call(r,o)&&_(o),f(),y(),r[o]=c()}return t.charCodeAt(e)!==R&&F(),e++,r}}function i(){if(t.charCodeAt(e)===k){e++,f();for(var r=[],n=!0;e<t.length&&t.charCodeAt(e)!==U;){n?n=!1:d();var o=c();p(o),r.push(o)}return t.charCodeAt(e)!==U&&J(),e++,r}}function c(){var t,r,n,e,o,c;f();var d=null!==(t=null!==(r=null!==(n=null!==(e=null!==(o=null!==(c=l())&&void 0!==c?c:s())&&void 0!==o?o:a())&&void 0!==e?e:i())&&void 0!==n?n:u("true",!0))&&void 0!==r?r:u("false",!1))&&void 0!==t?t:u("null",null);return f(),d}function u(r,n){if(t.slice(e,e+r.length)===r)return e+=r.length,n}function f(){for(;A(t.charCodeAt(e));)e++}function l(){if(t.charCodeAt(e)===P){e++;for(var r="";e<t.length&&t.charCodeAt(e)!==P;){if(t.charCodeAt(e)===j){var n=t[e+1],o=O[n];void 0!==o?(r+=o,e++):"u"===n?C(t.charCodeAt(e+2))&&C(t.charCodeAt(e+3))&&C(t.charCodeAt(e+4))&&C(t.charCodeAt(e+5))?(r+=String.fromCharCode(parseInt(t.slice(e+2,e+6),16)),e+=5):Q(e):D(e)}else E(t.charCodeAt(e))?r+=t[e]:L(t[e]);e++}return w(),e++,r}}function s(){var r=e;if(t.charCodeAt(e)===$&&(e++,b(r)),t.charCodeAt(e)===B)e++;else if(x(t.charCodeAt(e)))for(e++;N(t.charCodeAt(e));)e++;if(t.charCodeAt(e)===Z)for(e++,b(r);N(t.charCodeAt(e));)e++;if(t.charCodeAt(e)===K||t.charCodeAt(e)===H)for(e++,t.charCodeAt(e)!==$&&t.charCodeAt(e)!==T||e++,b(r);N(t.charCodeAt(e));)e++;if(e>r)return n(t.slice(r,e))}function d(){if(t.charCodeAt(e)!==W)throw new SyntaxError("Comma ',' expected after value ".concat(M()));e++}function y(){if(t.charCodeAt(e)!==q)throw new SyntaxError("Colon ':' expected after property name ".concat(M()));e++}function v(t){if(void 0===t)throw new SyntaxError("JSON value expected ".concat(M()))}function p(t){if(void 0===t)throw new SyntaxError("Array item expected ".concat(M()))}function h(){if(e<t.length)throw new SyntaxError("Expected end of input ".concat(M()))}function b(r){if(!N(t.charCodeAt(e))){var n=t.slice(r,e);throw new SyntaxError("Invalid number '".concat(n,"', expecting a digit ").concat(M()))}}function w(){if(t.charCodeAt(e)!==P)throw new SyntaxError("End of string '\"' expected ".concat(M()))}function S(){throw new SyntaxError("Quoted object key expected ".concat(M()))}function _(t){throw new SyntaxError("Duplicate key '".concat(t,"' encountered at position ").concat(e-t.length-1))}function F(){throw new SyntaxError("Quoted object key or end of object '}' expected ".concat(M()))}function J(){throw new SyntaxError("Array item or end of array ']' expected ".concat(M()))}function L(t){throw new SyntaxError("Invalid character '".concat(t,"' ").concat(z()))}function D(r){var n=t.slice(r,r+2);throw new SyntaxError("Invalid escape character '".concat(n,"' ").concat(z()))}function Q(r){for(var n=r+2;/\w/.test(t[n]);)n++;var e=t.slice(r,n);throw new SyntaxError("Invalid unicode character '".concat(e,"' ").concat(z()))}function z(){return"at position ".concat(e)}function G(){return e<t.length?"but got '".concat(t[e],"'"):"but reached end of input"}function M(){return G()+" "+z()}},t.parseLosslessNumber=m,t.parseNumberAndBigInt=function(t){return r(t)?BigInt(t):parseFloat(t)},t.reviveDate=function(t,r){return"string"==typeof r&&Y.test(r)?new Date(r):r},t.stringify=function t(r,n,o,a){var i=function(t){if("number"==typeof t)return" ".repeat(t);if("string"==typeof t&&""!==t)return t;return}(o);return c("function"==typeof n?n.call({"":r},"",r):r,"");function c(r,u){if(Array.isArray(a)){var f=a.find((function(t){return t.test(r)}));if(f){var l=f.stringify(r);if("string"!=typeof l||!e(l))throw new Error("Invalid JSON number: output of a number stringifier must be a string containing a JSON number "+"(output: ".concat(l,")"));return l}}return"boolean"==typeof r||"number"==typeof r||"string"==typeof r||null===r||r instanceof Date||r instanceof Boolean||r instanceof Number||r instanceof String?JSON.stringify(r):r&&r.isLosslessNumber||"bigint"==typeof r?r.toString():Array.isArray(r)?function(t,r){if(0===t.length)return"[]";for(var e=i?r+i:void 0,o=i?"[\n":"[",a=0;a<t.length;a++){var u="function"==typeof n?n.call(t,String(a),t[a]):t[a];i&&(o+=e),o+=void 0!==u&&"function"!=typeof u?c(u,e):"null",a<t.length-1&&(o+=i?",\n":",")}return o+=i?"\n"+r+"]":"]"}(r,u):r&&"object"===X(r)?function(r,e){if("function"==typeof r.toJSON)return t(r.toJSON(),n,o,void 0);var a=Array.isArray(n)?n.map(String):Object.keys(r);if(0===a.length)return"{}";var u=i?e+i:void 0,f=!0,l=i?"{\n":"{";return a.forEach((function(t){var e="function"==typeof n?n.call(r,t,r[t]):r[t];if(function(t,r){return void 0!==r&&"function"!=typeof r&&"symbol"!==X(r)}(0,e)){f?f=!1:l+=i?",\n":",";var o=JSON.stringify(t);l+=i?u+o+": ":o+":",l+=c(e,u)}})),l+=i?"\n"+e+"}":"}"}(r,u):void 0}},t.toLosslessNumber=function(t){if(u(t+"").length>15)throw new Error("Invalid number: contains more than 15 digits and is most likely truncated and unsafe by itself "+"(value: ".concat(t,")"));if(isNaN(t))throw new Error("Invalid number: NaN");if(!isFinite(t))throw new Error("Invalid number: "+t);return new h(String(t))},t.toSafeNumberOrThrow=function(r,n){var e=parseFloat(r),o=c(r);if(!0===(null==n?void 0:n.approx)?o&&o!==t.UnsafeNumberReason.truncate_float:o){var a=o.replace(/_\w+$/,"");throw new Error("Cannot safely convert to number: "+"the value '".concat(r,"' would ").concat(a," and become ").concat(e))}return e}}));//# sourceMappingURL=lossless-json.js.map
!function(t,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r((t="undefined"!=typeof globalThis?globalThis:t||self).LosslessJSON={})}(this,(function(t){"use strict";function r(t){return n.test(t)}var n=/^-?[0-9]+$/;function e(t){return i.test(t)}var o,i=/^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/;function a(t,n){var e=parseFloat(t),o=String(e),i=c(t),a=c(o);if(i===a)return!0;if(!0===(null==n?void 0:n.approx)){if(!r(t)&&a.length>=14&&i.startsWith(a.substring(0,14)))return!0}return!1}function u(n){if(!a(n,{approx:!1})){if(r(n))return t.UnsafeNumberReason.truncate_integer;var e=parseFloat(n);return isFinite(e)?0===e?t.UnsafeNumberReason.underflow:t.UnsafeNumberReason.truncate_float:t.UnsafeNumberReason.overflow}}function c(t){return t.replace(f,"").replace(s,"").replace(y,"").replace(l,"")}t.UnsafeNumberReason=void 0,(o=t.UnsafeNumberReason||(t.UnsafeNumberReason={})).underflow="underflow",o.overflow="overflow",o.truncate_integer="truncate_integer",o.truncate_float="truncate_float";var f=/[eE][+-]?\d+$/,l=/^-?(0*)?/,s=/\./,y=/0+$/;function d(t){return d="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},d(t)}function p(t,r){for(var n=0;n<r.length;n++){var e=r[n];e.enumerable=e.enumerable||!1,e.configurable=!0,"value"in e&&(e.writable=!0),Object.defineProperty(t,b(e.key),e)}}function b(t){var r=function(t,r){if("object"!==d(t)||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var e=n.call(t,r||"default");if("object"!==d(e))return e;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===r?String:Number)(t)}(t,"string");return"symbol"===d(r)?r:String(r)}var v=function(){function n(t){if(function(t,r){if(!(t instanceof r))throw new TypeError("Cannot call a class as a function")}(this,n),function(t,r,n){(r=b(r))in t?Object.defineProperty(t,r,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[r]=n}(this,"isLosslessNumber",!0),!e(t))throw new Error('Invalid number (value: "'+t+'")');this.value=t}var o,i,a;return o=n,(i=[{key:"valueOf",value:function(){var n=u(this.value);if(void 0===n||n===t.UnsafeNumberReason.truncate_float)return parseFloat(this.value);if(r(this.value))return BigInt(this.value);throw new Error("Cannot safely convert to number: "+"the value '".concat(this.value,"' would ").concat(n," and become ").concat(parseFloat(this.value)))}},{key:"toString",value:function(){return this.value}}])&&p(o.prototype,i),a&&p(o,a),Object.defineProperty(o,"prototype",{writable:!1}),n}();function h(t){return t&&"object"===d(t)&&!0===t.isLosslessNumber||!1}function m(t){return new v(t)}function w(t){return w="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},w(t)}function g(t,r){return S({"":t},"",t,r)}function S(t,r,n,e){return Array.isArray(n)?e.call(t,r,function(t,r){for(var n=0;n<t.length;n++)t[n]=S(t,n+"",t[n],r);return t}(n,e)):n&&"object"===w(n)&&!h(n)?e.call(t,r,function(t,r){return Object.keys(t).forEach((function(n){var e=S(t,n,t[n],r);void 0!==e?t[n]=e:delete t[n]})),t}(n,e)):e.call(t,r,n)}function A(t){return A="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},A(t)}function C(t){return function(t){if(Array.isArray(t))return N(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||function(t,r){if(!t)return;if("string"==typeof t)return N(t,r);var n=Object.prototype.toString.call(t).slice(8,-1);"Object"===n&&t.constructor&&(n=t.constructor.name);if("Map"===n||"Set"===n)return Array.from(t);if("Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return N(t,r)}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function N(t,r){(null==r||r>t.length)&&(r=t.length);for(var n=0,e=new Array(r);n<r;n++)e[n]=t[n];return e}function E(t){return t===$||t===P||t===B||t===D}function x(t){return t>=Z&&t<=z||t>=V&&t<=rt||t>=X&&t<=nt}function j(t){return t>=Z&&t<=z}function O(t){return t>=q&&t<=z}function I(t){return t>=32&&t<=1114111}function k(t,r){return t===r||(Array.isArray(t)&&Array.isArray(r)?t.length===r.length&&t.every((function(t,n){return k(t,r[n])})):!(!R(t)||!R(r))&&C(new Set([].concat(C(Object.keys(t)),C(Object.keys(r))))).every((function(n){return k(t[n],r[n])})))}function R(t){return"object"===A(t)&&null!==t}var U={'"':'"',"\\":"\\","/":"/",b:"\b",f:"\f",n:"\n",r:"\r",t:"\t"},_=92,F=123,J=125,L=91,T=93,$=32,P=10,B=9,D=13,Q=34,M=43,W=45,Z=48,q=49,z=57,G=44,H=46,K=58,V=65,X=97,Y=69,tt=101,rt=70,nt=102;function et(t){return et="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},et(t)}var ot=/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/;t.LosslessNumber=v,t.config=function(t){throw new Error("config is deprecated, support for circularRefs is removed from the library. If you encounter circular references in your data structures, please rethink your datastructures: better prevent circular references in the first place.")},t.getUnsafeNumberReason=u,t.isInteger=r,t.isLosslessNumber=h,t.isNumber=e,t.isSafeNumber=a,t.parse=function(t,r){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:m,e=0,o=u();return p(o),v(),r?g(o,r):o;function i(){if(t.charCodeAt(e)===F){e++,f();for(var r={},n=!0;e<t.length&&t.charCodeAt(e)!==J;){n?n=!1:(y(),f());var o=e,i=l();void 0===i&&S(),f(),d();var a=u();Object.prototype.hasOwnProperty.call(r,i)&&!k(a,r[i])&&A(i,o+1),r[i]=a}return t.charCodeAt(e)!==J&&C(),e++,r}}function a(){if(t.charCodeAt(e)===L){e++,f();for(var r=[],n=!0;e<t.length&&t.charCodeAt(e)!==T;){n?n=!1:y();var o=u();b(o),r.push(o)}return t.charCodeAt(e)!==T&&N(),e++,r}}function u(){var t,r,n,e,o,u;f();var y=null!==(t=null!==(r=null!==(n=null!==(e=null!==(o=null!==(u=l())&&void 0!==u?u:s())&&void 0!==o?o:i())&&void 0!==e?e:a())&&void 0!==n?n:c("true",!0))&&void 0!==r?r:c("false",!1))&&void 0!==t?t:c("null",null);return f(),y}function c(r,n){if(t.slice(e,e+r.length)===r)return e+=r.length,n}function f(){for(;E(t.charCodeAt(e));)e++}function l(){if(t.charCodeAt(e)===Q){e++;for(var r="";e<t.length&&t.charCodeAt(e)!==Q;){if(t.charCodeAt(e)===_){var n=t[e+1],o=U[n];void 0!==o?(r+=o,e++):"u"===n?x(t.charCodeAt(e+2))&&x(t.charCodeAt(e+3))&&x(t.charCodeAt(e+4))&&x(t.charCodeAt(e+5))?(r+=String.fromCharCode(parseInt(t.slice(e+2,e+6),16)),e+=5):P(e):$(e)}else I(t.charCodeAt(e))?r+=t[e]:R(t[e]);e++}return w(),e++,r}}function s(){var r=e;if(t.charCodeAt(e)===W&&(e++,h(r)),t.charCodeAt(e)===Z)e++;else if(O(t.charCodeAt(e)))for(e++;j(t.charCodeAt(e));)e++;if(t.charCodeAt(e)===H)for(e++,h(r);j(t.charCodeAt(e));)e++;if(t.charCodeAt(e)===tt||t.charCodeAt(e)===Y)for(e++,t.charCodeAt(e)!==W&&t.charCodeAt(e)!==M||e++,h(r);j(t.charCodeAt(e));)e++;if(e>r)return n(t.slice(r,e))}function y(){if(t.charCodeAt(e)!==G)throw new SyntaxError("Comma ',' expected after value ".concat(q()));e++}function d(){if(t.charCodeAt(e)!==K)throw new SyntaxError("Colon ':' expected after property name ".concat(q()));e++}function p(t){if(void 0===t)throw new SyntaxError("JSON value expected ".concat(q()))}function b(t){if(void 0===t)throw new SyntaxError("Array item expected ".concat(q()))}function v(){if(e<t.length)throw new SyntaxError("Expected end of input ".concat(q()))}function h(r){if(!j(t.charCodeAt(e))){var n=t.slice(r,e);throw new SyntaxError("Invalid number '".concat(n,"', expecting a digit ").concat(q()))}}function w(){if(t.charCodeAt(e)!==Q)throw new SyntaxError("End of string '\"' expected ".concat(q()))}function S(){throw new SyntaxError("Quoted object key expected ".concat(q()))}function A(t,r){throw new SyntaxError("Duplicate key '".concat(t,"' encountered at position ").concat(r))}function C(){throw new SyntaxError("Quoted object key or end of object '}' expected ".concat(q()))}function N(){throw new SyntaxError("Array item or end of array ']' expected ".concat(q()))}function R(t){throw new SyntaxError("Invalid character '".concat(t,"' ").concat(B()))}function $(r){var n=t.slice(r,r+2);throw new SyntaxError("Invalid escape character '".concat(n,"' ").concat(B()))}function P(r){for(var n=r+2;/\w/.test(t[n]);)n++;var e=t.slice(r,n);throw new SyntaxError("Invalid unicode character '".concat(e,"' ").concat(B()))}function B(){return"at position ".concat(e)}function D(){return e<t.length?"but got '".concat(t[e],"'"):"but reached end of input"}function q(){return D()+" "+B()}},t.parseLosslessNumber=m,t.parseNumberAndBigInt=function(t){return r(t)?BigInt(t):parseFloat(t)},t.reviveDate=function(t,r){return"string"==typeof r&&ot.test(r)?new Date(r):r},t.stringify=function t(r,n,o,i){var a=function(t){if("number"==typeof t)return" ".repeat(t);if("string"==typeof t&&""!==t)return t;return}(o);return u("function"==typeof n?n.call({"":r},"",r):r,"");function u(r,c){if(Array.isArray(i)){var f=i.find((function(t){return t.test(r)}));if(f){var l=f.stringify(r);if("string"!=typeof l||!e(l))throw new Error("Invalid JSON number: output of a number stringifier must be a string containing a JSON number "+"(output: ".concat(l,")"));return l}}return"boolean"==typeof r||"number"==typeof r||"string"==typeof r||null===r||r instanceof Date||r instanceof Boolean||r instanceof Number||r instanceof String?JSON.stringify(r):r&&r.isLosslessNumber||"bigint"==typeof r?r.toString():Array.isArray(r)?function(t,r){if(0===t.length)return"[]";for(var e=a?r+a:void 0,o=a?"[\n":"[",i=0;i<t.length;i++){var c="function"==typeof n?n.call(t,String(i),t[i]):t[i];a&&(o+=e),o+=void 0!==c&&"function"!=typeof c?u(c,e):"null",i<t.length-1&&(o+=a?",\n":",")}return o+=a?"\n"+r+"]":"]"}(r,c):r&&"object"===et(r)?function(r,e){if("function"==typeof r.toJSON)return t(r.toJSON(),n,o,void 0);var i=Array.isArray(n)?n.map(String):Object.keys(r);if(0===i.length)return"{}";var c=a?e+a:void 0,f=!0,l=a?"{\n":"{";return i.forEach((function(t){var e="function"==typeof n?n.call(r,t,r[t]):r[t];if(function(t,r){return void 0!==r&&"function"!=typeof r&&"symbol"!==et(r)}(0,e)){f?f=!1:l+=a?",\n":",";var o=JSON.stringify(t);l+=a?c+o+": ":o+":",l+=u(e,c)}})),l+=a?"\n"+e+"}":"}"}(r,c):void 0}},t.toLosslessNumber=function(t){if(c(t+"").length>15)throw new Error("Invalid number: contains more than 15 digits and is most likely truncated and unsafe by itself "+"(value: ".concat(t,")"));if(isNaN(t))throw new Error("Invalid number: NaN");if(!isFinite(t))throw new Error("Invalid number: "+t);return new v(String(t))},t.toSafeNumberOrThrow=function(r,n){var e=parseFloat(r),o=u(r);if(!0===(null==n?void 0:n.approx)?o&&o!==t.UnsafeNumberReason.truncate_float:o){var i=o.replace(/_\w+$/,"");throw new Error("Cannot safely convert to number: "+"the value '".concat(r,"' would ").concat(i," and become ").concat(e))}return e}}));//# sourceMappingURL=lossless-json.js.map
The MIT License (MIT)
Copyright (c) 2016-2022 Jos de Jong
Copyright (c) 2016-2023 Jos de Jong

@@ -5,0 +5,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

{
"name": "lossless-json",
"version": "2.0.6",
"version": "2.0.7",
"description": "Parse JSON without risk of losing numeric information",

@@ -24,3 +24,4 @@ "type": "module",

"test": "jest --rootDir=test --runInBand",
"test:lib": "npm run build && jest --rootDir='test-lib'",
"lint": "prettier --ignore-path .gitignore lib --check . && eslint src/**/*.ts test/**/*.ts test-lib/**/*.mjs tools/**/*.mjs",
"format": "prettier --ignore-path .gitignore lib --write . && npm run lint -- --fix",
"build": "npm-run-all build:**",

@@ -31,7 +32,12 @@ "build:clean": "del-cli lib",

"build:types": "tsc --project tsconfig-types.json",
"lint": "prettier --ignore-path .gitignore lib --check . && eslint src/**/*.ts test/**/*.ts test-lib/**/*.mjs tools/**/*.mjs",
"format": "prettier --ignore-path .gitignore lib --write . && npm run lint -- --fix",
"build-and-test": "npm run build && jest && npm run lint",
"prepublishOnly": "npm test && npm run build",
"benchmark": "npm run build:esm && node tools/benchmark/run.mjs"
"build:validate": "jest --rootDir='test-lib' --runInBand",
"build-and-test": "npm test && npm run lint && npm run build",
"release": "npm-run-all release:**",
"release:build-and-test": "npm run build-and-test",
"release:version": "standard-version",
"release:push": "git push && git push --tag",
"release:publish": "npm publish",
"release-dry-run": "npm run build-and-test && standard-version --dry-run",
"benchmark": "npm run build:esm && node tools/benchmark/run.mjs",
"prepare": "husky install"
},

@@ -58,2 +64,4 @@ "keywords": [

"@babel/preset-typescript": "7.18.6",
"@commitlint/cli": "17.4.4",
"@commitlint/config-conventional": "17.4.4",
"@rollup/plugin-terser": "0.4.0",

@@ -75,2 +83,3 @@ "@types/benchmark": "2.1.2",

"eslint-plugin-promise": "6.1.1",
"husky": "8.0.3",
"jest": "29.4.2",

@@ -80,2 +89,3 @@ "npm-run-all": "4.1.5",

"rollup": "3.15.0",
"standard-version": "9.5.0",
"ts-jest": "29.0.5",

@@ -82,0 +92,0 @@ "typescript": "4.9.5"

@@ -163,4 +163,6 @@ # lossless-json

The library is compatible with the native `JSON.parse` and `JSON.stringify`, and also comes with the optional `reviver` and `replacer` arguments that allow you to serialize for example data classes in a custom way. Here is an example demonstrating how you can stringify a `Date` as an object with a `$date` key instead of a string, so it is uniquely recognizable when parsing the structure:
The library is compatible with the native `JSON.parse` and `JSON.stringify`, and also comes with the optional `reviver` and `replacer` arguments that allow you to serialize for example data classes in a custom way. Here is an example demonstrating how you can stringify a `Date` in a different way than the built-in `reviveDate` utility function.
The following example stringifies a `Date` as an object with a `$date` key instead of a string, so it is uniquely recognizable when parsing the structure:
```js

@@ -170,3 +172,3 @@ import { parse, stringify } from 'lossless-json'

// stringify a Date as a unique object with a key '$date', so it is recognizable
function dateReplacer(key, value) {
function customDateReplacer(key, value) {
if (value instanceof Date) {

@@ -185,3 +187,3 @@ return {

function dateReviver(key, value) {
function customDateReviver(key, value) {
if (isJSONDateObject(value)) {

@@ -199,3 +201,3 @@ return new Date(value.$date)

const text = stringify(record, dateReplacer)
const text = stringify(record, customDateReplacer)
console.log(text)

@@ -205,3 +207,3 @@ // output:

const parsed = parse(text, dateReviver)
const parsed = parse(text, customDateReviver)
console.log(parsed)

@@ -415,14 +417,27 @@ // output:

## Publish
### Release
- Update version number in `package.json`, and run `npm install` to update the version number in `package-lock.json` too.
- Describe changes is `HISTORY.md`
- run `build-and-test` to see whether everything works correctly.
- Commit changes.
- merge changes from `develop` into `master`
- create git tag and push it: `git tag v1.0.2 && git push --tags`
- publish via `npm publish` (this will first build, test, and lint before actually publishing the library).
To release a new version:
```
$ npm run release
```
This will:
- lint
- test
- build
- increment the version number
- push the changes to git, add a git version tag
- publish the npm package
To try the build and see the change list without actually publishing:
```
$ npm run release-dry-run
```
## License
Released under the [MIT license](LICENSE.md).

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc