@stryke/json 0.2.1 → 0.3.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.
@@ -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 _stringifyMin = require("./utils/stringify-min.cjs");
13
+ var _stringifyJson = require("./utils/stringify-json.cjs");
14
14
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
15
15
  class StormJSON extends _superjson.default {
16
- static #r;
16
+ static #e;
17
17
  static get instance() {
18
- return StormJSON.#r || (StormJSON.#r = new StormJSON()), StormJSON.#r;
18
+ return StormJSON.#e || (StormJSON.#e = new StormJSON()), StormJSON.#e;
19
19
  }
20
- static deserialize(r) {
21
- return StormJSON.instance.deserialize(r);
20
+ static deserialize(e) {
21
+ return StormJSON.instance.deserialize(e);
22
22
  }
23
- static serialize(r) {
24
- return StormJSON.instance.serialize(r);
23
+ static serialize(e) {
24
+ return StormJSON.instance.serialize(e);
25
25
  }
26
- static parse(r) {
27
- return StormJSON.instance.parse(r);
26
+ static parse(e) {
27
+ return StormJSON.instance.parse(e);
28
28
  }
29
- static stringifyMin(r) {
30
- const e = StormJSON.instance.customTransformerRegistry.findApplicable(r);
31
- let s = r;
32
- return e && (s = e.serialize(s)), (0, _stringifyMin.stringifyMin)(s);
29
+ static stringifyJson(e, r) {
30
+ const s = StormJSON.instance.customTransformerRegistry.findApplicable(e);
31
+ let t = e;
32
+ return s && (t = s.serialize(t)), (0, _stringifyJson.stringifyJson)(t?.json ? t?.json : t, r?.spaces);
33
33
  }
34
- static stringify(r) {
35
- return StormJSON.instance.stringify(r);
34
+ static stringify(e) {
35
+ return StormJSON.instance.stringify(e);
36
36
  }
37
- static stringifyJson(r) {
38
- return StormJSON.instance.stringify(r);
39
- }
40
- static parseJson(r, e) {
37
+ static parseJson(e, r) {
41
38
  try {
42
- if (e?.expectComments === !1) return StormJSON.instance.parse(r);
39
+ if (r?.expectComments === !1) return StormJSON.instance.parse(e);
43
40
  } catch {}
44
41
  const s = [],
45
- i = {
42
+ t = {
46
43
  allowTrailingComma: !0,
47
- ...e
44
+ ...r
48
45
  },
49
- n = (0, _jsoncParser.parse)(r, s, i);
50
- if (s.length > 0 && s[0]) throw new Error((0, _parseError.formatParseError)(r, s[0]));
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(r, e, s, i) {
50
+ static register(e, r, s, t) {
54
51
  StormJSON.instance.registerCustom({
55
- isApplicable: i,
56
- serialize: e,
52
+ isApplicable: t,
53
+ serialize: r,
57
54
  deserialize: s
58
- }, r);
55
+ }, e);
59
56
  }
60
- static registerClass(r, e) {
61
- StormJSON.instance.registerClass(r, {
62
- identifier: (0, _isString.isString)(e) ? e : e?.identifier || r.name,
63
- allowProps: e && (0, _isObject.isObject)(e) && e?.allowProps && Array.isArray(e.allowProps) ? e.allowProps : ["__typename"]
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: t => _buffer.Buffer.isBuffer(t),
75
- serialize: t => t.toString("base64"),
76
- deserialize: t => _buffer.Buffer.from(t, "base64")
71
+ isApplicable: i => _buffer.Buffer.isBuffer(i),
72
+ serialize: i => i.toString("base64"),
73
+ deserialize: i => _buffer.Buffer.from(i, "base64")
77
74
  }, "Bytes");
@@ -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,10 +27,14 @@ export declare class StormJSON extends SuperJSON {
27
27
  */
28
28
  static parse<TData = unknown>(value: string): TData;
29
29
  /**
30
- * Stringify the given value with superjson
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 json - 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 stringifyMin(obj: any): string;
37
+ static stringifyJson<TJson>(json: TJson, options?: JsonSerializeOptions): string;
34
38
  /**
35
39
  * Stringify the given value with superjson
36
40
  *
@@ -38,14 +42,6 @@ export declare class StormJSON extends SuperJSON {
38
42
  * @returns The stringified object
39
43
  */
40
44
  static stringify(obj: any): string;
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;
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.
@@ -1 +1 @@
1
- import{isObject as o}from"@stryke/types/type-checks/is-object";import{isString as c}from"@stryke/types/type-checks/is-string";import{Buffer as a}from"buffer/";import{parse as l}from"jsonc-parser";import u from"superjson";import{formatParseError as p}from"./utils/parse-error";import{stringifyMin as f}from"./utils/stringify-min";export class StormJSON extends u{static#r;static get instance(){return StormJSON.#r||(StormJSON.#r=new StormJSON),StormJSON.#r}static deserialize(r){return StormJSON.instance.deserialize(r)}static serialize(r){return StormJSON.instance.serialize(r)}static parse(r){return StormJSON.instance.parse(r)}static stringifyMin(r){const e=StormJSON.instance.customTransformerRegistry.findApplicable(r);let s=r;return e&&(s=e.serialize(s)),f(s)}static stringify(r){return StormJSON.instance.stringify(r)}static stringifyJson(r){return StormJSON.instance.stringify(r)}static parseJson(r,e){try{if(e?.expectComments===!1)return StormJSON.instance.parse(r)}catch{}const s=[],i={allowTrailingComma:!0,...e},n=l(r,s,i);if(s.length>0&&s[0])throw new Error(p(r,s[0]));return n}static register(r,e,s,i){StormJSON.instance.registerCustom({isApplicable:i,serialize:e,deserialize:s},r)}static registerClass(r,e){StormJSON.instance.registerClass(r,{identifier:c(e)?e:e?.identifier||r.name,allowProps:e&&o(e)&&e?.allowProps&&Array.isArray(e.allowProps)?e.allowProps:["__typename"]})}constructor(){super({dedupe:!0})}}StormJSON.instance.registerCustom({isApplicable:t=>a.isBuffer(t),serialize:t=>t.toString("base64"),deserialize:t=>a.from(t,"base64")},"Bytes");
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{stringifyJson as f}from"./utils/stringify-json";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 stringifyJson(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 stringify(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 type Class = {
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 {
@@ -25,14 +25,14 @@ Object.keys(_parseError).forEach(function (key) {
25
25
  }
26
26
  });
27
27
  });
28
- var _stringifyMin = require("./stringify-min.cjs");
29
- Object.keys(_stringifyMin).forEach(function (key) {
28
+ var _stringifyJson = require("./stringify-json.cjs");
29
+ Object.keys(_stringifyJson).forEach(function (key) {
30
30
  if (key === "default" || key === "__esModule") return;
31
- if (key in exports && exports[key] === _stringifyMin[key]) return;
31
+ if (key in exports && exports[key] === _stringifyJson[key]) return;
32
32
  Object.defineProperty(exports, key, {
33
33
  enumerable: true,
34
34
  get: function () {
35
- return _stringifyMin[key];
35
+ return _stringifyJson[key];
36
36
  }
37
37
  });
38
38
  });
@@ -1,4 +1,4 @@
1
1
  export * from "./code-frames";
2
2
  export * from "./parse-error";
3
- export * from "./stringify-min";
3
+ export * from "./stringify-json";
4
4
  export * from "./strip-comments";
@@ -1 +1 @@
1
- export*from"./code-frames";export*from"./parse-error";export*from"./stringify-min";export*from"./strip-comments";
1
+ export*from"./code-frames";export*from"./parse-error";export*from"./stringify-json";export*from"./strip-comments";
@@ -3,21 +3,21 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.stringifyMin = void 0;
6
+ exports.stringifyJson = void 0;
7
7
  var _isNumber = require("@stryke/types/type-checks/is-number");
8
- const stringifyMin = (r, n = " ") => {
9
- const t = (0, _isNumber.isNumber)(n) ? " ".repeat(n) : n;
8
+ const stringifyJson = (r, t = 2) => {
9
+ const n = (0, _isNumber.isNumber)(t) ? " ".repeat(t) : t;
10
10
  switch (r) {
11
11
  case null:
12
- return "!n";
12
+ return "null";
13
13
  case void 0:
14
- return "!u";
14
+ return "undefined";
15
15
  case !0:
16
- return "!t";
16
+ return "true";
17
17
  case !1:
18
- return "!f";
18
+ return "false";
19
19
  }
20
- if (Array.isArray(r)) return `[${t}${r.map(e => stringifyMin(e, t)).join(`,${t}`)}${t}]`;
20
+ if (Array.isArray(r)) return `[${n}${r.map(e => stringifyJson(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 `{${t}${e.map(s => `${s}${t}=${t}${stringifyMin(r[s], t)}`).join(`,${t}`)}${t}}`;
30
+ return `{${n}${e.map(s => `${s}${n}=${n}${stringifyJson(r[s], n)}`).join(`,${n}`)}${n}}`;
31
31
  }
32
32
  default:
33
- return "?";
33
+ return "null";
34
34
  }
35
35
  };
36
- exports.stringifyMin = stringifyMin;
36
+ exports.stringifyJson = stringifyJson;
@@ -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 stringifyMin: (value: unknown, spacing?: string | number) => string;
8
+ export declare const stringifyJson: (value: unknown, spacing?: string | number) => string;
@@ -0,0 +1 @@
1
+ import{isNumber as i}from"@stryke/types/type-checks/is-number";export const stringifyJson=(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=>stringifyJson(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}=${n}${stringifyJson(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.2.1",
3
+ "version": "0.3.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-min": {
109
+ "./utils/stringify-json": {
110
110
  "import": {
111
- "types": "./dist/utils/stringify-min.d.ts",
112
- "default": "./dist/utils/stringify-min.mjs"
111
+ "types": "./dist/utils/stringify-json.d.ts",
112
+ "default": "./dist/utils/stringify-json.mjs"
113
113
  },
114
114
  "require": {
115
- "types": "./dist/utils/stringify-min.d.ts",
116
- "default": "./dist/utils/stringify-min.cjs"
115
+ "types": "./dist/utils/stringify-json.d.ts",
116
+ "default": "./dist/utils/stringify-json.cjs"
117
117
  },
118
118
  "default": {
119
- "types": "./dist/utils/stringify-min.d.ts",
120
- "default": "./dist/utils/stringify-min.mjs"
119
+ "types": "./dist/utils/stringify-json.d.ts",
120
+ "default": "./dist/utils/stringify-json.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"?"}};