From a6692b8d20b6d88524395887e66e25ee7757e2bd Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Wed, 19 Jun 2024 11:11:58 -0700 Subject: [PATCH] Fixed a regression where floating-point numbers were truncated to integers. --- .vscode/settings.json | 1 - CHANGELOG.md | 4 ++ README.md | 5 ++ package.json | 2 +- src/jsonformatter.test.ts | 108 ++++++++++++++++++++++++++++++++++++++ src/jsonformatter.ts | 6 +-- src/manifest.chrome.json | 2 +- src/manifest.firefox.json | 7 ++- 8 files changed, 128 insertions(+), 7 deletions(-) create mode 100644 src/jsonformatter.test.ts diff --git a/.vscode/settings.json b/.vscode/settings.json index f881f4e..a0d164f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -8,7 +8,6 @@ "editor.codeActionsOnSave": { "source.fixAll": "explicit" }, - "jest.jestCommandLine": "pnpm test --", "[javascript]": { "editor.codeActionsOnSave": { "source.organizeImports": "never" diff --git a/CHANGELOG.md b/CHANGELOG.md index c6d328c..ce27d9d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## JSONView 3.0.1 + +- Fixed a regression where floating-point numbers were truncated to integers. + ## JSONView 3.0.0 - JSONView is now compatible with Manifest V3, which is required in Chrome. diff --git a/README.md b/README.md index cdb79c9..2a79d39 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ - [Install for Firefox](https://addons.mozilla.org/en-US/firefox/addon/jsonview/) - [Install for Chrome](https://chrome.google.com/webstore/detail/jsonview/gmegofmjomhknnokphhckolhcffdaihd) - [Install for Edge](https://microsoftedge.microsoft.com/addons/detail/jsonview/kmpfgkgaimakokfhgdahhiaaiidiphco) +- There is no version for Safari because it costs $100/year to publish a free extension to the Mac App Store. Normally, when encountering a [JSON](http://json.org) document (content type `application/json`), Firefox simply prompts you to download the view. With the JSONView extension, JSON documents are shown in the browser similar to how XML documents are shown. The document is formatted, highlighted, and arrays and objects can be collapsed. Even if the JSON document contains errors, JSONView will still show the raw text. @@ -49,3 +50,7 @@ pnpm start ``` `jsonview-chrome.zip` and `jsonview-firefox.zip` can then be manually uploaded to the extension sites. + +- Chrome: https://chrome.google.com/webstore/devconsole/ +- Firefox: https://addons.mozilla.org/en-US/developers/addons +- Edge: https://partner.microsoft.com/en-us/dashboard/microsoftedge/overview diff --git a/package.json b/package.json index 664ef75..6fe7d79 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "id": "jsonview@brh.numbera.com", - "version": "3.0.0", + "version": "3.0.1", "name": "jsonview", "title": "JSONView", "description": "View JSON documents in the browser.", diff --git a/src/jsonformatter.test.ts b/src/jsonformatter.test.ts new file mode 100644 index 0000000..459680a --- /dev/null +++ b/src/jsonformatter.test.ts @@ -0,0 +1,108 @@ +import { strict as assert } from "node:assert"; +import test from "node:test"; +import { valueToHTML } from "./jsonformatter.js"; +import { safeStringEncodeNums } from "./safe-encode-numbers.js"; + +const jsonContent = [ + [`{}`, `{ }`], + [ + `{ "hey": "guy" }`, + `{}`, + ], + [ + `{ "float": 10.5 }`, + `{}`, + ], + [ + `{ "10.5": "hello" }`, + `{}`, + ], + [ + // bigger than max safe integer + `{ "anumber": 9117199254740991 }`, + `{}`, + ], + [ + `{ "anobject": {"whoa": "nuts","anarray": [1,2,"thr

ee"], "more":"stuff"} }`, + `{}`, + ], + [ + `{ "awesome": true }`, + `{}`, + ], + [ + `{ "bogus": false }`, + `{}`, + ], + [ + `{ "meaning": null }`, + `{}`, + ], + [ + `{ "japanese": "明日がある。" }`, + `{}`, + ], + [ + `{ "link": "http://jsonview.com" }`, + `{}`, + ], + [ + `{ "notLink": "http://jsonview.com is great" }`, + `{}`, + ], + [ + `{ "aZero": 0 }`, + `{}`, + ], + [ + `{ "emptyString": "" }`, + `{}`, + ], + [ + `{"string_with_nulls":"\\u0000*\\u0000_hello"}`, + `{}`, + ], + [ + `{"":"18"}`, + `{}`, + ], + [`[]`, `[ ]`], + [`null`, `null`], + [`true`, `true`], + [`1`, `1`], + [ + `[1,2,"thr

ee"]`, + `[]`, + ], + [ + `{"hey": "g'uy'"}`, + `{}`, + ], + [ + `{ "value":[ { "@some.text":"W/\\"12241774\\"" } ] }`, + `{}`, + ], + [ + `{"key":"\\"value\\u201d"}`, + `{}`, + ], +]; + +Object.defineProperties(globalThis, { + chrome: { + value: { + i18n: { + getMessage: (message: string) => message, + }, + runtime: { + getURL: (url: string) => url, + }, + }, + }, +}); + +for (const [content, result] of jsonContent) { + test(`valueToHTML ${content}`, () => { + assert.equal(valueToHTML(JSON.parse(safeStringEncodeNums(content)), "", 0), result); + }); +} diff --git a/src/jsonformatter.ts b/src/jsonformatter.ts index a55921a..f9f6022 100644 --- a/src/jsonformatter.ts +++ b/src/jsonformatter.ts @@ -74,7 +74,7 @@ function decorateWithSpan(value: any, className: string) { } // Convert a basic JSON datatype (number, string, boolean, null, object, array) into an HTML fragment. -function valueToHTML(value: any, path: string, indent: number) { +export function valueToHTML(value: any, path: string, indent: number) { if (value === null) { return decorateWithSpan("null", "null"); } else if (Array.isArray(value)) { @@ -89,8 +89,8 @@ function valueToHTML(value: any, path: string, indent: number) { case "boolean": return decorateWithSpan(value, "bool"); case "string": - if (value.charCodeAt(0) === 8203 && !isNaN(parseInt(value.slice(1), 10))) { - return decorateWithSpan(parseInt(value.slice(1), 10), "num"); + if (value.charCodeAt(0) === 8203 /* zero-width space */ && !isNaN(Number(value.slice(1)))) { + return decorateWithSpan(Number(value.slice(1)), "num"); } else if (/^(http|https|file):\/\/[^\s]+$/i.test(value)) { return `"${jsString( value diff --git a/src/manifest.chrome.json b/src/manifest.chrome.json index d48ad82..f902744 100644 --- a/src/manifest.chrome.json +++ b/src/manifest.chrome.json @@ -1,7 +1,7 @@ { "manifest_version": 3, "name": "JSONView", - "version": "3.0.0", + "version": "3.0.1", "description": "__MSG_extensionDescription__", "author": "Benjamin Hollis", "homepage_url": "https://jsonview.com/", diff --git a/src/manifest.firefox.json b/src/manifest.firefox.json index 6f47409..c7a769f 100644 --- a/src/manifest.firefox.json +++ b/src/manifest.firefox.json @@ -1,7 +1,7 @@ { "manifest_version": 3, "name": "JSONView", - "version": "3.0.0", + "version": "3.0.1", "description": "__MSG_extensionDescription__", "author": "Benjamin Hollis", "homepage_url": "https://jsonview.com/", @@ -12,6 +12,11 @@ "128": "icon128.png", "256": "icon256.png" }, + "browser_specific_settings": { + "gecko": { + "id": "jsonview@brh.numbera.com" + } + }, "background": { "scripts": ["background.js"], "type": "module"