npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-to-map

v1.0.0

Published

json-to-map

Downloads

4

Readme

json-to-map

This library mainly solves the following 2 problems of JSON parsing 查看中文文档

  1. When parsing JSON containing numeric key, in the parsed object, all numeric key will be arranged in order from small to large.
    This will cause diff data problems in scenarios such as JSON diff. See the example below:
// Numeric keys are sorted, not in insertion order
const jsonStr = '{"a": 0, "2": 10, "1": 20, "b": 30}';
const obj = JSON.parse(jsonStr);
console.log(obj); // {1: 20, 2: 10, a: 0, b: 30}
// The order we expect is ["a", "2", "1", "b"]
console.log(Object.keys(obj)); // output: ['1', '2', 'a', 'b']

2.JSON will lose precision when parsing integers and decimals that exceed the precision.

// safe integer
// [-(2**53-1), 2**53-1] => [-9007199254740991, 9007199254740991]
const obj = JSON.parse(9007199254740999);
console.log(obj); // output: 9007199254741000 precision lost

For question 1, parse the JSON string and convert it into a Map. The insertion order of the Map is the JSON string order, ensuring orderliness.
Regarding question 2, when parsing JSON strings, integers that exceed the precision can be parsed into strings to ensure accuracy. When doing reverse conversion later, reverse serialization can be done based on the data type defined by JSON Schema.

Usage

1.parseJsonToMap parses JSON string into Map

function parseJsonToMap(source: string, options?: IOptions) {}
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
}

I. The JSON string containing numeric keys is arranged in order

import { parseJsonToMap } from "json-to-map";
const str = '{"a": 0, "2": 10, "1": 20, "b": 30}';
const map = parseJsonToMap(str);
console.log(map);
/*
    The sorting order is consistent with the json string declaration
    new Map([
     [
         "a",
         0
     ],
     [
         "2",
         10
     ],
     [
         "1",
         20
     ],
     [
         "b",
         30
     ]
])
*/

II: Numbers stored as strings

To parse numbers beyond JS precision into strings, use the configuration { storeAsString: true }

import { parseJsonToMap } from "json-to-map";
const str =
  '{"a": 123456789987654321, "2": 10, "1": 20, "b": 30, "c": true, "d": "123"}';
const map = parseJsonToMap(str, { storeAsString: true });
console.log(map);
/*
new Map([
    [
       "a",
       "123456789987654321"
    ],
    [
       "2",
       "10"
    ],
    [
       "1",
       "20"
    ],
    [
       "b",
       "30"
    ],
    [
       "c",
       true
    ],
    [
       "d",
       "123"
    ]
])
*/

2. Parse map into JSON string

The usage is exactly the same as JSON.stringify. The function is declared as follows:

function mapStringify(
   value: any,
   replacer?: ((key: string, value: any) => any) | Array<number | string> | null,
   space?: number | string
) {}

I. map is parsed into json string

import { mapStringify } from "json-to-map";

const map = new Map([
  ["a", 0],
  ["2", 10],
  ["1", 20],
  [
    "b",
    new Map([
      ["bb", 2],
      ["bb", 3],
      ["1", 4],
    ]),
  ],
]);

console.log(mapStringify(map));
/**
 *output
 * {"a":0,"2":10,"1":20,"b":{"bb":2,"bbb":3,"1":4}}
 */

II. map is parsed into json string, including control characters

import { mapStringify } from "json-to-map";

const map = new Map([
  ["a", 0],
  ["2", 10],
  ["1", 20],
  [
    "b",
    new Map([
      ["bb", 2],
      ["bb", 3],
      ["1", 4],
    ]),
  ],
]);

console.log(mapStringify(map, null, 2));
/*
output:
{
   "a": 0,
   "2": 10,
   "1": 20,
   "b": {
     "bb": 2,
     "bb": 3,
     "1": 4
   }
}
*/

Thanks

This library is based on json-bigint, and heartfelt thanks to the author sidorares for his work. The code implements a clear and concise top-down JSON parser.