Giter VIP home page Giter VIP logo

strong-json-parse's Introduction

document

The readme also has a chinese version.查看中文

description

Based on json-bigint, added support for arbitrary precision decimals and bigInt numbers

What problem is strong-json-parse trying to solve?

json-bigint can convert large integers that exceed JS precision to strings, but the type of the number being converted is lost during this conversion. After processing the data, you may still stringify data to a string. Additionally, support for arbitrary-precision decimals has been added.

Guide

import { jsonParse } from "strong-json-parse";
const str = `{"id":123456989987654321,"name":"joey","interests":["football","video games"]}`;
// Use storeAsString options to store Bigint numbers as strings
const result = jsonParse(str, { storeAsString: true });
console.log(result);

output The original data type of id was number, but now it is stored as string

{
  "data": {
    "id": "123456989987654321",
    "name": "joey",
    "interests": ["football", "video games"]
  },
  "dataSchema": {
    "type": "object",
    "originType": "object",
    "value": {
      "id": {
        "type": "string",
        "originType": "number",
        "value": "123456989987654321"
      },
      "name": {
        "type": "string",
        "originType": "string",
        "value": "joey"
      },
      "interests": {
        "type": "array",
        "originType": "array",
        "value": [
          {
            "type": "string",
            "originType": "string",
            "value": "football"
          },
          {
            "type": "string",
            "originType": "string",
            "value": "video games"
          }
        ]
      }
    }
  }
}

Method jsonParse(str: string, options: IOptions)

Convert JSON string to object, return { data, dataSchema } object Where data represents the original JSON object, and dataSchema contains the pre-conversion and post-conversion data types of each value, please see the following example

Parameters

str: JSON string options:

interface IOptions {
  strict?: boolean; // not being strict means do not generate syntax errors for "duplicate key", default is false
  storeAsString?: boolean; // toggle whether the values should be stored as BigNumber (default) or a string, default is false
  alwaysParseAsBigInt?: boolean; // toggle whether all numbers should be BigInt Type, default is false
  protoAction?: "error" | "ignore" | "preserve"; // whether keep __proto__ property, default is "error", not allowed
  constructorAction?: "error" | "ignore" | "preserve"; // whether keep constructor property, default is "error", not allowed
}

Example: Store all numbers as strings

   jsonParse(
     `{ "name": "Joey", "adult": true, "fortune": 123456789987654321987654321, "mixed": ["football", 20, true, 98]}`,
     {
       storeAsString: true, // Convert all numbers to strings
     }
   )

output

{
   //Data after parse
   data: {
     name: "Joey",
     adult: true,
     fortune: "123456789987654321987654321",
     mixed: ["football", "20", true, "98"],
   },

   // After parse, field type
   dataSchema: {
     type: "object",
     originType: "object",
     value: {
       name: {
         type: "string",
         originType: "string",
         value: "Joey",
       },
       adult: {
         type: "boolean",
         originType: "boolean",
         value: true,
       },
       fortune: {
         type: "string", //The current type is string
         originType: "number", // The original type is number
         value: "1234567899987654321987654321",
       },
       mixed: {
         type: "array",
         originType: "array",
         value: [
           {
             type: "string",
             originType: "string",
             value: "football",
           },
           {
             type: "string",
             originType: "number",
             value: "20",
           },
           {
             type: "boolean",
             originType: "boolean",
             value: true,
           },
           {
             type: "string",
             originType: "number",
             value: "98",
           },
         ],
       },
     },
   },
}

Method jsonStringifyByDataSchema(obj: unknown,schema: ISchema, space: number | string)

Function: Serialize the obj object according to the data type provided by dataSchema Note: obj and schema are required to have the same structure What is the same structure:

  • If the object contains an array, the array sub-item type will not be determined after adding or deleting items.
  • If it is an object, the data type cannot be determined when adding a new key. If your object requires the above operations, please use the jsonStringifyByJsonSchema method

Parameter Description

obj: object to be serialized schema: dataSchema object generated by jsonParse space: number | string represents formatted indentation

Example

const data = {
  orderId: "123456789654432", // modified original value
  region: "Germany",
};

const dataSchema = {
  type: "object",
  originType: "object",
  value: {
    orderId: {
      type: "string",
      originType: "number",
      value: "123456789",
    },
    region: {
      type: "string",
      originType: "string",
      value: "China",
    },
  },
};

console.log(jsonStringifyByDataSchema(data, dataSchema, 2));

Output JSON string orderId was originally of type number, and the original data type is retained at this time

{
     "orderId": 123456789654432,
     "region": "Germany"
}

Method jsonStringifyByJsonSchema(obj: unknown, jsonSchema: ISchema, space: number | string)

Convert data types according to the common JSON Schema protocol, If the current field is string, determine whether schema is number, and if it is number, convert it, otherwise do not convert it.

Why use universal JSON Schema

When adding a field to the data or adding an item to the array, use the common JSON Schema constraint data type.

Parameter Description

obj: object to be serialized schema: Universal JSON schema protocol space: number | string represents formatted indentation

Example

const jsonSchema = {
  type: "object",
  properties: {
    id: {
      type: "number",
      title: "id of question",
    },
    title: {
      type: "string",
      title: "title of question",
    },
    choices: {
      type: "array",
      title: "choice list",
      items: {
        type: "string",
        title: "choice item",
      },
    },
  },
};

const data = {
  id: "123", // origin type is number
  title: "The three largest countries by area",
  choices: ["China", "Russia", "America", "Canada", 1],
};

console.log(jsonStringifyByJsonSchema(data, jsonSchema, 2));

output id is of number type in JSON schema, and the generated string is number

{
  "id": 123,
  "title": "The three largest countries by area",
  "choices": ["China", "Russia", "America", "Canada"]
}

Method jsonStringify(value, replacer, space)

Universal JSON.stringify method implementation with consistent usage

strong-json-parse's People

Stargazers

 avatar ximenchuixue avatar

Watchers

 avatar

strong-json-parse's Issues

jsonParse() : A StackOverflow error occurs when the deeply nested JSON character string is parsed.

jsonParse () does not support deeply nested JSON strings.

import { jsonParse } from "strong-json-parse";
let json = `{ "a": { "a": ... 1 ... } }`; //  Nesting depth: 9000
 jsonParse(json , { storeAsString: true });

error log:

RangeError: Stack overflow!
at m (oh_modules/.ohpm/@[email protected]/oh_modules/@stoplight/json/index.es.js:1:8199)
    at onObjectBegin (oh_modules/.ohpm/@[email protected]/oh_modules/@stoplight/json/index.es.js:1:8369)
    at anonymous (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:359:53)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:492:9)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:509:18)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:509:18)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:509:18)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:509:18)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:509:18)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:509:18)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:509:18)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:509:18)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:509:18)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:509:18)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:509:18)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:509:18)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:509:18)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:509:18)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:509:18)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:509:18)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:509:18)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:509:18)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:509:18)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:509:18)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:509:18)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:509:18)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:509:18)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:509:18)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:509:18)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:509:18)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:509:18)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:509:18)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:509:18)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.