@stryke/json 0.2.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/storm-json.cjs +32 -35
- package/dist/storm-json.d.ts +8 -12
- package/dist/storm-json.mjs +1 -1
- package/dist/types.d.ts +2 -2
- package/dist/utils/index.cjs +4 -4
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.mjs +1 -1
- package/dist/utils/{stringify-min.cjs → stringify.cjs} +11 -11
- package/dist/utils/{stringify-min.d.ts → stringify.d.ts} +1 -1
- package/dist/utils/stringify.mjs +1 -0
- package/package.json +8 -8
- package/dist/utils/stringify-min.mjs +0 -1
package/dist/storm-json.cjs
CHANGED
|
@@ -10,57 +10,54 @@ var _buffer = require("buffer/");
|
|
|
10
10
|
var _jsoncParser = require("jsonc-parser");
|
|
11
11
|
var _superjson = _interopRequireDefault(require("superjson"));
|
|
12
12
|
var _parseError = require("./utils/parse-error.cjs");
|
|
13
|
-
var
|
|
13
|
+
var _stringify = require("./utils/stringify.cjs");
|
|
14
14
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
15
15
|
class StormJSON extends _superjson.default {
|
|
16
|
-
static #
|
|
16
|
+
static #e;
|
|
17
17
|
static get instance() {
|
|
18
|
-
return StormJSON.#
|
|
18
|
+
return StormJSON.#e || (StormJSON.#e = new StormJSON()), StormJSON.#e;
|
|
19
19
|
}
|
|
20
|
-
static deserialize(
|
|
21
|
-
return StormJSON.instance.deserialize(
|
|
20
|
+
static deserialize(e) {
|
|
21
|
+
return StormJSON.instance.deserialize(e);
|
|
22
22
|
}
|
|
23
|
-
static serialize(
|
|
24
|
-
return StormJSON.instance.serialize(
|
|
23
|
+
static serialize(e) {
|
|
24
|
+
return StormJSON.instance.serialize(e);
|
|
25
25
|
}
|
|
26
|
-
static parse(
|
|
27
|
-
return StormJSON.instance.parse(
|
|
26
|
+
static parse(e) {
|
|
27
|
+
return StormJSON.instance.parse(e);
|
|
28
28
|
}
|
|
29
|
-
static
|
|
30
|
-
const
|
|
31
|
-
let
|
|
32
|
-
return
|
|
29
|
+
static stringify(e, r) {
|
|
30
|
+
const s = StormJSON.instance.customTransformerRegistry.findApplicable(e);
|
|
31
|
+
let t = e;
|
|
32
|
+
return s && (t = s.serialize(t)), (0, _stringify.stringify)(t?.json ? t?.json : t, r?.spaces);
|
|
33
33
|
}
|
|
34
|
-
static
|
|
35
|
-
return StormJSON.instance.stringify(
|
|
34
|
+
static stringifyBase(e) {
|
|
35
|
+
return StormJSON.instance.stringify(e);
|
|
36
36
|
}
|
|
37
|
-
static
|
|
38
|
-
return StormJSON.instance.stringify(r);
|
|
39
|
-
}
|
|
40
|
-
static parseJson(r, e) {
|
|
37
|
+
static parseJson(e, r) {
|
|
41
38
|
try {
|
|
42
|
-
if (
|
|
39
|
+
if (r?.expectComments === !1) return StormJSON.instance.parse(e);
|
|
43
40
|
} catch {}
|
|
44
41
|
const s = [],
|
|
45
|
-
|
|
42
|
+
t = {
|
|
46
43
|
allowTrailingComma: !0,
|
|
47
|
-
...
|
|
44
|
+
...r
|
|
48
45
|
},
|
|
49
|
-
n = (0, _jsoncParser.parse)(
|
|
50
|
-
if (s.length > 0 && s[0]) throw new Error((0, _parseError.formatParseError)(
|
|
46
|
+
n = (0, _jsoncParser.parse)(e, s, t);
|
|
47
|
+
if (s.length > 0 && s[0]) throw new Error((0, _parseError.formatParseError)(e, s[0]));
|
|
51
48
|
return n;
|
|
52
49
|
}
|
|
53
|
-
static register(
|
|
50
|
+
static register(e, r, s, t) {
|
|
54
51
|
StormJSON.instance.registerCustom({
|
|
55
|
-
isApplicable:
|
|
56
|
-
serialize:
|
|
52
|
+
isApplicable: t,
|
|
53
|
+
serialize: r,
|
|
57
54
|
deserialize: s
|
|
58
|
-
},
|
|
55
|
+
}, e);
|
|
59
56
|
}
|
|
60
|
-
static registerClass(
|
|
61
|
-
StormJSON.instance.registerClass(
|
|
62
|
-
identifier: (0, _isString.isString)(
|
|
63
|
-
allowProps:
|
|
57
|
+
static registerClass(e, r) {
|
|
58
|
+
StormJSON.instance.registerClass(e, {
|
|
59
|
+
identifier: (0, _isString.isString)(r) ? r : r?.identifier || e.name,
|
|
60
|
+
allowProps: r && (0, _isObject.isObject)(r) && r?.allowProps && Array.isArray(r.allowProps) ? r.allowProps : ["__typename"]
|
|
64
61
|
});
|
|
65
62
|
}
|
|
66
63
|
constructor() {
|
|
@@ -71,7 +68,7 @@ class StormJSON extends _superjson.default {
|
|
|
71
68
|
}
|
|
72
69
|
exports.StormJSON = StormJSON;
|
|
73
70
|
StormJSON.instance.registerCustom({
|
|
74
|
-
isApplicable:
|
|
75
|
-
serialize:
|
|
76
|
-
deserialize:
|
|
71
|
+
isApplicable: i => _buffer.Buffer.isBuffer(i),
|
|
72
|
+
serialize: i => i.toString("base64"),
|
|
73
|
+
deserialize: i => _buffer.Buffer.from(i, "base64")
|
|
77
74
|
}, "Bytes");
|
package/dist/storm-json.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Class, JsonParseOptions, JsonParserResult, JsonValue } from "./types";
|
|
1
|
+
import type { Class, JsonParseOptions, JsonParserResult, JsonSerializeOptions, JsonValue } from "./types";
|
|
2
2
|
import SuperJSON from "superjson";
|
|
3
3
|
/**
|
|
4
4
|
* A static JSON parser class used by Storm Software to serialize and deserialize JSON data
|
|
@@ -27,25 +27,21 @@ export declare class StormJSON extends SuperJSON {
|
|
|
27
27
|
*/
|
|
28
28
|
static parse<TData = unknown>(value: string): TData;
|
|
29
29
|
/**
|
|
30
|
-
*
|
|
30
|
+
* Serializes the given data to a JSON string.
|
|
31
|
+
* By default the JSON string is formatted with a 2 space indentation to be easy readable.
|
|
31
32
|
*
|
|
33
|
+
* @param value - Object which should be serialized to JSON
|
|
34
|
+
* @param options - JSON serialize options
|
|
35
|
+
* @returns the formatted JSON representation of the object
|
|
32
36
|
*/
|
|
33
|
-
static
|
|
37
|
+
static stringify<T>(value: T, options?: JsonSerializeOptions): string;
|
|
34
38
|
/**
|
|
35
39
|
* Stringify the given value with superjson
|
|
36
40
|
*
|
|
37
41
|
* @param obj - The object to stringify
|
|
38
42
|
* @returns The stringified object
|
|
39
43
|
*/
|
|
40
|
-
static
|
|
41
|
-
/**
|
|
42
|
-
* Serializes the given data to a JSON string.
|
|
43
|
-
* By default the JSON string is formatted with a 2 space indentation to be easy readable.
|
|
44
|
-
*
|
|
45
|
-
* @param json - Object which should be serialized to JSON
|
|
46
|
-
* @returns the formatted JSON representation of the object
|
|
47
|
-
*/
|
|
48
|
-
static stringifyJson(json: any): string;
|
|
44
|
+
static stringifyBase(obj: any): string;
|
|
49
45
|
/**
|
|
50
46
|
* Parses the given JSON string and returns the object the JSON content represents.
|
|
51
47
|
* By default javascript-style comments and trailing commas are allowed.
|
package/dist/storm-json.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{isObject as o}from"@stryke/types/type-checks/is-object";import{isString as
|
|
1
|
+
import{isObject as o}from"@stryke/types/type-checks/is-object";import{isString as l}from"@stryke/types/type-checks/is-string";import{Buffer as a}from"buffer/";import{parse as c}from"jsonc-parser";import u from"superjson";import{formatParseError as p}from"./utils/parse-error";import{stringify as f}from"./utils/stringify";export class StormJSON extends u{static#e;static get instance(){return StormJSON.#e||(StormJSON.#e=new StormJSON),StormJSON.#e}static deserialize(e){return StormJSON.instance.deserialize(e)}static serialize(e){return StormJSON.instance.serialize(e)}static parse(e){return StormJSON.instance.parse(e)}static stringify(e,r){const s=StormJSON.instance.customTransformerRegistry.findApplicable(e);let t=e;return s&&(t=s.serialize(t)),f(t?.json?t?.json:t,r?.spaces)}static stringifyBase(e){return StormJSON.instance.stringify(e)}static parseJson(e,r){try{if(r?.expectComments===!1)return StormJSON.instance.parse(e)}catch{}const s=[],t={allowTrailingComma:!0,...r},n=c(e,s,t);if(s.length>0&&s[0])throw new Error(p(e,s[0]));return n}static register(e,r,s,t){StormJSON.instance.registerCustom({isApplicable:t,serialize:r,deserialize:s},e)}static registerClass(e,r){StormJSON.instance.registerClass(e,{identifier:l(r)?r:r?.identifier||e.name,allowProps:r&&o(r)&&r?.allowProps&&Array.isArray(r.allowProps)?r.allowProps:["__typename"]})}constructor(){super({dedupe:!0})}}StormJSON.instance.registerCustom({isApplicable:i=>a.isBuffer(i),serialize:i=>i.toString("base64"),deserialize:i=>a.from(i,"base64")},"Bytes");
|
package/dist/types.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { ParseOptions } from "jsonc-parser";
|
|
2
2
|
export type PrimitiveJsonValue = string | number | boolean | undefined | null;
|
|
3
|
-
export
|
|
3
|
+
export interface Class {
|
|
4
4
|
new (...args: any[]): any;
|
|
5
|
-
}
|
|
5
|
+
}
|
|
6
6
|
export type JsonValue = PrimitiveJsonValue | JsonArray | JsonObject;
|
|
7
7
|
export type JsonArray = Array<JsonValue>;
|
|
8
8
|
export interface JsonObject {
|
package/dist/utils/index.cjs
CHANGED
|
@@ -25,14 +25,14 @@ Object.keys(_parseError).forEach(function (key) {
|
|
|
25
25
|
}
|
|
26
26
|
});
|
|
27
27
|
});
|
|
28
|
-
var
|
|
29
|
-
Object.keys(
|
|
28
|
+
var _stringify = require("./stringify.cjs");
|
|
29
|
+
Object.keys(_stringify).forEach(function (key) {
|
|
30
30
|
if (key === "default" || key === "__esModule") return;
|
|
31
|
-
if (key in exports && exports[key] ===
|
|
31
|
+
if (key in exports && exports[key] === _stringify[key]) return;
|
|
32
32
|
Object.defineProperty(exports, key, {
|
|
33
33
|
enumerable: true,
|
|
34
34
|
get: function () {
|
|
35
|
-
return
|
|
35
|
+
return _stringify[key];
|
|
36
36
|
}
|
|
37
37
|
});
|
|
38
38
|
});
|
package/dist/utils/index.d.ts
CHANGED
package/dist/utils/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export*from"./code-frames";export*from"./parse-error";export*from"./stringify
|
|
1
|
+
export*from"./code-frames";export*from"./parse-error";export*from"./stringify";export*from"./strip-comments";
|
|
@@ -3,21 +3,21 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.
|
|
6
|
+
exports.stringify = void 0;
|
|
7
7
|
var _isNumber = require("@stryke/types/type-checks/is-number");
|
|
8
|
-
const
|
|
9
|
-
const
|
|
8
|
+
const stringify = (r, t = 2) => {
|
|
9
|
+
const n = (0, _isNumber.isNumber)(t) ? " ".repeat(t) : t;
|
|
10
10
|
switch (r) {
|
|
11
11
|
case null:
|
|
12
|
-
return "
|
|
12
|
+
return "null";
|
|
13
13
|
case void 0:
|
|
14
|
-
return "
|
|
14
|
+
return "undefined";
|
|
15
15
|
case !0:
|
|
16
|
-
return "
|
|
16
|
+
return "true";
|
|
17
17
|
case !1:
|
|
18
|
-
return "
|
|
18
|
+
return "false";
|
|
19
19
|
}
|
|
20
|
-
if (Array.isArray(r)) return `[${
|
|
20
|
+
if (Array.isArray(r)) return `[${n}${r.map(e => stringify(e, n)).join(`,${n}`)}${n}]`;
|
|
21
21
|
if (r instanceof Uint8Array) return r.toString();
|
|
22
22
|
switch (typeof r) {
|
|
23
23
|
case "number":
|
|
@@ -27,10 +27,10 @@ const stringifyMin = (r, n = " ") => {
|
|
|
27
27
|
case "object":
|
|
28
28
|
{
|
|
29
29
|
const e = Object.keys(r);
|
|
30
|
-
return `{${
|
|
30
|
+
return `{${n}${e.map(s => `${s}: ${n}${stringify(r[s], n)}`).join(`,${n}`)}${n}}`;
|
|
31
31
|
}
|
|
32
32
|
default:
|
|
33
|
-
return "
|
|
33
|
+
return "null";
|
|
34
34
|
}
|
|
35
35
|
};
|
|
36
|
-
exports.
|
|
36
|
+
exports.stringify = stringify;
|
|
@@ -5,4 +5,4 @@
|
|
|
5
5
|
* @param spacing - The spacing to use for the stringification
|
|
6
6
|
* @returns The stringified value
|
|
7
7
|
*/
|
|
8
|
-
export declare const
|
|
8
|
+
export declare const stringify: (value: unknown, spacing?: string | number) => string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{isNumber as i}from"@stryke/types/type-checks/is-number";export const stringify=(r,t=2)=>{const n=i(t)?" ".repeat(t):t;switch(r){case null:return"null";case void 0:return"undefined";case!0:return"true";case!1:return"false"}if(Array.isArray(r))return`[${n}${r.map(e=>stringify(e,n)).join(`,${n}`)}${n}]`;if(r instanceof Uint8Array)return r.toString();switch(typeof r){case"number":return`${r}`;case"string":return JSON.stringify(r);case"object":{const e=Object.keys(r);return`{${n}${e.map(s=>`${s}: ${n}${stringify(r[s],n)}`).join(`,${n}`)}${n}}`}default:return"null"}};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stryke/json",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A package containing JSON parsing/stringify utilities used by Storm Software.",
|
|
6
6
|
"repository": {
|
|
@@ -106,18 +106,18 @@
|
|
|
106
106
|
"default": "./dist/utils/strip-comments.mjs"
|
|
107
107
|
}
|
|
108
108
|
},
|
|
109
|
-
"./utils/stringify
|
|
109
|
+
"./utils/stringify": {
|
|
110
110
|
"import": {
|
|
111
|
-
"types": "./dist/utils/stringify
|
|
112
|
-
"default": "./dist/utils/stringify
|
|
111
|
+
"types": "./dist/utils/stringify.d.ts",
|
|
112
|
+
"default": "./dist/utils/stringify.mjs"
|
|
113
113
|
},
|
|
114
114
|
"require": {
|
|
115
|
-
"types": "./dist/utils/stringify
|
|
116
|
-
"default": "./dist/utils/stringify
|
|
115
|
+
"types": "./dist/utils/stringify.d.ts",
|
|
116
|
+
"default": "./dist/utils/stringify.cjs"
|
|
117
117
|
},
|
|
118
118
|
"default": {
|
|
119
|
-
"types": "./dist/utils/stringify
|
|
120
|
-
"default": "./dist/utils/stringify
|
|
119
|
+
"types": "./dist/utils/stringify.d.ts",
|
|
120
|
+
"default": "./dist/utils/stringify.mjs"
|
|
121
121
|
}
|
|
122
122
|
},
|
|
123
123
|
"./utils/parse-error": {
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{isNumber as i}from"@stryke/types/type-checks/is-number";export const stringifyMin=(r,n=" ")=>{const t=i(n)?" ".repeat(n):n;switch(r){case null:return"!n";case void 0:return"!u";case!0:return"!t";case!1:return"!f"}if(Array.isArray(r))return`[${t}${r.map(e=>stringifyMin(e,t)).join(`,${t}`)}${t}]`;if(r instanceof Uint8Array)return r.toString();switch(typeof r){case"number":return`${r}`;case"string":return JSON.stringify(r);case"object":{const e=Object.keys(r);return`{${t}${e.map(s=>`${s}${t}=${t}${stringifyMin(r[s],t)}`).join(`,${t}`)}${t}}`}default:return"?"}};
|