svelte-jsoneditor icon indicating copy to clipboard operation
svelte-jsoneditor copied to clipboard

Support for LONG values

Open spirostz opened this issue 2 years ago • 6 comments

1st Step:

Put this in the window

{
  "maxLongHere": 9223372036854775807
}

2nd Step:

Press the Format Json button

3rd Step (problem replicated):

Notice that value changed (was 9223372036854775807 now its 9223372036854776000 )

{
  "maxLongHere": 9223372036854776000
}

spirostz avatar Jun 10 '22 11:06 spirostz

Thanks for reporting. The editor uses the browser's built-in JSON.parse, which parses the JSON into regular JavaScript object and number, and JavaScript numbers cannot hold all digits of a long value.

It would be nice to see if we can integrate https://github.com/josdejong/lossless-json for example. This is much slower than JSON.parse though, so we need to think through how to apply it. Maybe make it a config option. Or see if we can automatically detect if there are long values with a regexp for example and then select the right JSON parser.

EDIT: or maybe there are other JSON parsers out there that are about as fast as the built-in JSON.parse and which we can extend with support for LONG values.

josdejong avatar Jun 13 '22 07:06 josdejong

Hello, What I suggest is 1st to find a way to ensure the integrity of the json. Changing values like this is dangerous since there is no way to spot it easily. One way would be to generate the hash (crc32 will do) of the json after having spaces and line breaks removed. Then, after any manipulation (eg. Formatting) generate the hash again. In case it's different, just fail (whatever fail means in your case). Overall, I fully appreciate your excellent work on this very helpful tool that I use for several years!

spirostz avatar Jun 13 '22 16:06 spirostz

That's a good idea Tzoras, the first (small) step could be to show a warning when the editor detects a JSON document with LONG values, so the user is at least aware.

josdejong avatar Jun 13 '22 16:06 josdejong

And glad to hear the tool is useful for you 😄👍

josdejong avatar Jun 13 '22 16:06 josdejong

Javascript support Bigint. To transform Bigint to/from JSON, you might find the "Use within JSON" section of this document interesting...

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt

DrPaulBrewer avatar Jul 17 '22 20:07 DrPaulBrewer

Thanks Paul. I would love to be able to use the reviver of JSON.parse like explained in the MDN documentation you're referring to. Problem though is that you would need to know upfront which properties will contain a LONG value, and they must be saved as a string in the JSON document. When using the reviver, any numeric value is already parsed into a number (and the LONG precision is already lost) before it reaches the reviver callback.

So, as far as I know, the only way to solve this is to not use the native JSON.parse but a custom JSON parser which has support for large numbers, like https://github.com/josdejong/lossless-json that I created for this very purpose. This library isn't yet integrated in svelte-jsoneditor and has drawbacks like performance.

josdejong avatar Jul 18 '22 07:07 josdejong

Can you embed lossless-json in svelte-jsoneditor as an option?

majalineage avatar Sep 16 '22 09:09 majalineage

Yes, actually, I'm working on implementing support for that as we speak 😄 . I hope to finish this within two weeks, it's in the feat/lossless-json branch.

josdejong avatar Sep 16 '22 11:09 josdejong

This has been fixed now in [email protected] by implementing a new option parser, allowing you to configure your own JSON parser like the LosslessJSON parser.

Example: https://github.com/josdejong/svelte-jsoneditor/blob/main/src/routes/examples/custom_json_parser/%2Bpage.svelte Docs: https://github.com/josdejong/svelte-jsoneditor#properties Article: https://jsoneditoronline.org/indepth/parse/why-does-json-parse-corrupt-large-numbers/

josdejong avatar Sep 30 '22 08:09 josdejong

This has been fixed now in [email protected] by implementing a new option parser, allowing you to configure your own JSON parser like the LosslessJSON parser.

Example: https://github.com/josdejong/svelte-jsoneditor/blob/main/src/routes/examples/custom_json_parser/%2Bpage.svelte Docs: https://github.com/josdejong/svelte-jsoneditor#properties Article: https://jsoneditoronline.org/indepth/parse/why-does-json-parse-corrupt-large-numbers/

When I use vanilla-jsoneditor with typescript, can't find parser optional in JSONEditorPropsOptional, try work with parse params is OK, but need to ignore ts check

liuzhaowei55 avatar Dec 10 '22 17:12 liuzhaowei55

Thanks for reporting @liuzhaowei55, fixed via https://github.com/josdejong/svelte-jsoneditor/commit/410fd801297faa46afef201569bfd79792de17e1 (not yet published)

josdejong avatar Dec 12 '22 12:12 josdejong

Thanks, is the types.ts finished? And should they be set to the optional

liuzhaowei55 avatar Dec 12 '22 13:12 liuzhaowei55

owwww, good point, they should indeed be optional, thanks. Fixed via https://github.com/josdejong/svelte-jsoneditor/commit/4bc33e88450396a1860db43baabe15986bfc7cd1

josdejong avatar Dec 12 '22 13:12 josdejong

wating for the next version ~

liuzhaowei55 avatar Dec 12 '22 14:12 liuzhaowei55

The JSONEditorPropsOptional interface is fixed now in v0.11.3

josdejong avatar Dec 13 '22 09:12 josdejong

hi, I'm using LosslessJSONParser option in vanilla-jsoneditor, when i use editor.get(), the LONG values type changed from number to LosslessNumber object, which isn't my expected. How can i resolve this?

image

XueMeijing avatar Jan 04 '23 05:01 XueMeijing

@XueMeijing how to deal with LosslessNumber objects is described in the documentation of lossless-json:

https://github.com/josdejong/lossless-json#readme

josdejong avatar Jan 04 '23 08:01 josdejong

@XueMeijing how to deal with LosslessNumber objects is described in the documentation of lossless-json:

https://github.com/josdejong/lossless-json#readme

thanks for your quickly reply, i have read the documentation and got a workaround. Thank you very much~

XueMeijing avatar Jan 05 '23 05:01 XueMeijing