Comments (6)
You're looking for YYJSON_READ_NUMBER_AS_RAW
. You can see a practical example of using it in py_yyjson:
- Writing RAW: https://github.com/TkTech/py_yyjson/blob/main/yyjson/document.c#L260
- Reading RAW: https://github.com/TkTech/py_yyjson/blob/main/yyjson/document.c#L107
from yyjson.
According to RFC 8259, implementations can set limits on the range and precision of numbers accepted. This means that either accepting or rejecting large numbers is in line with the standard. The most common numeric types for C are only 64-bit int64, uint64, and double, so most C/C++ JSON libraries limit the precision to this range. For JavaScript, V8 also limits the numbers to the double range:
> JSON.stringify(123e999)
< "null"
> JSON.parse("123e999")
< Infinity
If you want to handle the numbers yourself, you can use YYJSON_READ_NUMBER_AS_RAW
as @TkTech suggested, which will parse all numbers into RAW
type strings. If you only want to reject Inf NaN literals but accept large numbers, it is not supported now, but maybe we can add a new flag YYJSON_READ_BIGNUM_AS_RAW
or YYJSON_READ_BIGNUM_AS_NULL
.
Could you tell me more about your application scenario?
from yyjson.
YYJSON_READ_BIGNUM_AS_RAW
flag has been added: f2525dc
from yyjson.
For JavaScript, V8 also limits the numbers to the double range
In the example provided, v8 parsed 123e999
to Infinity
, which is a behavior defined by the ecma262 standard for JSON.parse
. The standard stipulates that JavaScript reads a valid JSON string and then does the PARSE, which is similar to evaluating (123e999) in JS, converting the JSON Value into the JavaScript value. 123e999
is converted to Infinity
at this time.
Therefore, I think this behavior of v8 parse 123e999
to Infinity
is actually due to ecma262, not ECMA-404 or RFC-8259.
I am working on JSON optimization for a self-developed JavaScript virtual machine, expecting to use yyjson as the json parser and writer backend. But because of the issue I mentioned above, I can't just parse a '{"a": 123e999}'
with yyjson and generate the data structure I need because yyjson_read
will just return NULL
or yyjson_read_err
. I think translate 123e999
to Inf
is a behavior that should be handled by my JS Engine instead of yyjson.
Since ecma262 determines whether json is valid based on the ECMA-404, which is somewhat different from RFC-8259, it does not reject a large number like 1e999 at parse, which is a problem I ran into.
I'll try YYJSON_READ_NUMBER_AS_RAW
as a workaround, but I think the issue I mentioned is real, and maybe we could have more discussion about it. Add a new flag YYJSON_READ_BIGNUM_AS_RAW
would also be a good idea, maybe we can try to implement it.
from yyjson.
Thanks, I'll try that
from yyjson.
@ibireme Well done! I've tried this flag and it worked! Thx 🎉🎉🎉!
from yyjson.
Related Issues (20)
- Move ownership of a user allocated C string to yyjson object HOT 6
- Support `yyjson_mut_obj_add_obj` & `yyjson_mut_obj_add_arr` like `yyjson_mut_arr_add_*` HOT 6
- mem leak HOT 1
- Static Code Analysis: Identical sub-expressions on both sides of operator "<<" HOT 1
- Shared libraries need to be properly versioned on Linux HOT 1
- Json files are not newline terminated when pretty printing HOT 3
- memory issue for recursive function HOT 7
- yyjson_ptr_get_sint() returns false for positive integer HOT 5
- How to change the object key in mutable object? HOT 2
- error: -Wno-implicit-int and -Wno-implicit-function-declaration detected HOT 2
- Support Schema validator HOT 1
- A character make fail the JSON (empty JSON) HOT 1
- Does yyjson have parameters to set decimal places? HOT 4
- Discussion / Feedback request: Incorporating bytearray type HOT 8
- incorrectly formatted bool fields. HOT 3
- I need to merge multiple JSON strings HOT 1
- Is there a method to store the array of primitive types? HOT 4
- Release life cycle HOT 3
- Remove static qualifiers to support wrapping in other languages HOT 5
- Report row and line number when generating errors, instead of an absolute position HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from yyjson.