svelte-jsoneditor
svelte-jsoneditor copied to clipboard
Support for LONG values
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
}
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.
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!
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.
And glad to hear the tool is useful for you 😄👍
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
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.
Can you embed lossless-json in svelte-jsoneditor as an option?
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.
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/
This has been fixed now in
[email protected]
by implementing a new optionparser
, 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
Thanks for reporting @liuzhaowei55, fixed via https://github.com/josdejong/svelte-jsoneditor/commit/410fd801297faa46afef201569bfd79792de17e1 (not yet published)
Thanks, is the types.ts finished? And should they be set to the optional
owwww, good point, they should indeed be optional, thanks. Fixed via https://github.com/josdejong/svelte-jsoneditor/commit/4bc33e88450396a1860db43baabe15986bfc7cd1
wating for the next version ~
The JSONEditorPropsOptional
interface is fixed now in v0.11.3
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?
@XueMeijing how to deal with LosslessNumber
objects is described in the documentation of lossless-json:
https://github.com/josdejong/lossless-json#readme
@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~