Skip to content

Commit

Permalink
fix(cli):resolve data precision loss in JSON messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ni00 authored and Red-Asuka committed Dec 4, 2023
1 parent 1c81f2a commit 3e82b95
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
2 changes: 2 additions & 0 deletions cli/package.json
Expand Up @@ -29,6 +29,7 @@
"concat-stream": "^2.0.0",
"core-js": "^3.26.0",
"js-yaml": "^4.1.0",
"json-bigint": "^1.0.0",
"mqtt": "^4.3.7",
"protobufjs": "^7.2.3",
"pump": "^3.0.0",
Expand All @@ -40,6 +41,7 @@
"@faker-js/faker": "^7.6.0",
"@types/concat-stream": "^2.0.0",
"@types/js-yaml": "^4.0.5",
"@types/json-bigint": "^1.0.4",
"@types/node": "^17.0.43",
"@types/pump": "^1.1.1",
"@types/readable-stream": "^2.3.13",
Expand Down
5 changes: 3 additions & 2 deletions cli/src/utils/convertPayload.ts
@@ -1,11 +1,12 @@
import chalk from 'chalk'
import { jsonParse, jsonStringify } from './jsonUtils'

const convertJSON = (value: Buffer | string, action: 'encode' | 'decode') => {
try {
if (action === 'decode') {
return JSON.stringify(JSON.parse(value.toString()), null, 2)
return jsonStringify(jsonParse(value.toString()), null, 2)
} else {
return Buffer.from(JSON.stringify(JSON.parse(value.toString())))
return Buffer.from(jsonStringify(jsonParse(value.toString())))
}
} catch (err) {
return chalk.red(err)
Expand Down
24 changes: 24 additions & 0 deletions cli/src/utils/jsonUtils.ts
@@ -0,0 +1,24 @@
const JSONBigNumber = require('json-bigint')
const JSONBigInt = require('json-bigint')({ useNativeBigInt: true })

export const jsonParse: typeof JSON.parse = (...args: any[]) => {
try {
// When JSON only contains integers, use the native bigint type.
return JSONBigInt.parse(...args)
} catch (_error) {
// When JSON contains floating-point numbers, use the bignumber library.
// The numbers in the parsed JSON Object will be represented as BigNumber Objects.
return JSONBigNumber.parse(...args)
}
}

export const jsonStringify: typeof JSON.stringify = (...args: any[]) => {
try {
// When JSON only contains integers, use the native bigint type.
return JSONBigInt.stringify(...args)
} catch (_error) {
// When JSON contains floating-point numbers, use the bignumber library.
// Integers will be represented in scientific notation.
return JSONBigNumber.stringify(...args)
}
}
17 changes: 17 additions & 0 deletions cli/yarn.lock
Expand Up @@ -77,6 +77,11 @@
resolved "https://registry.yarnpkg.com/@types/js-yaml/-/js-yaml-4.0.5.tgz#738dd390a6ecc5442f35e7f03fa1431353f7e138"
integrity sha512-FhpRzf927MNQdRZP0J5DLIdTXhjLYzeUTmLAu69mnVksLH9CJY3IuSeEgbKUki7GQZm0WqDkGzyxju2EZGD2wA==

"@types/json-bigint@^1.0.4":
version "1.0.4"
resolved "https://registry.yarnpkg.com/@types/json-bigint/-/json-bigint-1.0.4.tgz#250d29e593375499d8ba6efaab22d094c3199ef3"
integrity sha512-ydHooXLbOmxBbubnA7Eh+RpBzuaIiQjh8WGJYQB50JFGFrdxW7JzVlyEV7fAXw0T2sqJ1ysTneJbiyNLqZRAag==

"@types/node@*", "@types/node@^17.0.43":
version "17.0.43"
resolved "https://registry.npmjs.org/@types/node/-/node-17.0.43.tgz"
Expand Down Expand Up @@ -170,6 +175,11 @@ base64-js@^1.3.1:
resolved "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz"
integrity sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==

bignumber.js@^9.0.0:
version "9.1.2"
resolved "https://registry.yarnpkg.com/bignumber.js/-/bignumber.js-9.1.2.tgz#b7c4242259c008903b13707983b5f4bbd31eda0c"
integrity sha512-2/mKyZH9K85bzOEfhXDBFZTGd1CTs+5IHpeFQo9luiBG7hghdC851Pj2WAhb6E3R6b9tZj/XKhbg4fum+Kepug==

bl@^4.0.2:
version "4.1.0"
resolved "https://registry.npmjs.org/bl/-/bl-4.1.0.tgz"
Expand Down Expand Up @@ -449,6 +459,13 @@ js-yaml@^4.1.0:
dependencies:
argparse "^2.0.1"

json-bigint@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/json-bigint/-/json-bigint-1.0.0.tgz#ae547823ac0cad8398667f8cd9ef4730f5b01ff1"
integrity sha512-SiPv/8VpZuWbvLSMtTDU8hEfrZWg/mH/nV/b4o0CYbSxu1UIQPLdwKOCIyLQX+VIPO5vrLX3i8qtqFyhdPSUSQ==
dependencies:
bignumber.js "^9.0.0"

json-parse-better-errors@^1.0.1:
version "1.0.2"
resolved "https://registry.npmjs.org/json-parse-better-errors/-/json-parse-better-errors-1.0.2.tgz"
Expand Down

0 comments on commit 3e82b95

Please sign in to comment.