-
Notifications
You must be signed in to change notification settings - Fork 284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support ulong values for Json #1118
Conversation
0f12262
to
abfdf9a
Compare
Using ulong just pushes the limit, it doesn't really fix the problem. Me think supporting |
@@ -108,6 +110,7 @@ struct Json { | |||
Null = null_, /// Compatibility alias - will be deprecated soon | |||
Bool = bool_, /// Compatibility alias - will be deprecated soon | |||
Int = int_, /// Compatibility alias - will be deprecated soon | |||
Uint = uint_, /// Compatibility alias - will be deprecated soon |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I doubt we need to add a compatibility alias for a new feature.
I also vote for a BigInt here |
|
So, you are all prefer |
It should work similar to the current |
Ok, I'll do it. |
759fdcb
to
a73efec
Compare
@s-ludwig done |
@@ -92,28 +117,59 @@ struct Json { | |||
} | |||
} | |||
|
|||
version (VibeUseJsonBigInt) { | |||
long bigIntToLong() inout |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing private
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
Thanks @IgorStepanov! I'm leaning towards making the Apart from my comments above, seems to look fine. I'll do a more thorough review later. |
Ok, I've changed the code. See the second commit. |
Sorry for being late to the party, but what about supporting cent/ucent, instead? :) I'm not sure what the state of this is in DMD but I remember it was worked on recently for LDC. Seems like a bad spot to be in for performance sensitive scenarios - deciding between not supporting values >= 2^63 and < 2^64 or using BigInts which require extra heap storage currently, whereas the cent would fit in the Json value's buffer - converting cent back to ulong is also pretty fast. |
@marcioapm |
@s-ludwig I've add an overflow check to |
@@ -108,12 +128,12 @@ struct Json { | |||
Null = null_, /// Compatibility alias - will be deprecated soon | |||
Bool = bool_, /// Compatibility alias - will be deprecated soon | |||
Int = int_, /// Compatibility alias - will be deprecated soon | |||
BigInt = bigint, /// Compatibility alias - will be deprecated soon |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is unnecessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, removed.
19d39e8
to
6496323
Compare
Could you squash your commits into a single one ? |
@@ -17,4 +17,12 @@ void main() | |||
Json parent = obj; | |||
parent.remove("item1"); | |||
foreach (i; obj) writeln(i); | |||
|
|||
version (VibeJsonDisableBigInt) { | |||
} else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seem to be the only occurence of the version left.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad. I'll remove it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
All commits represents different approaches. If the my last approach will be considered best, I squash commits. Otherwice, it will be much easy to rollback to previous version. |
@@ -181,6 +199,8 @@ struct Json { | |||
/// ditto | |||
long opAssign(long v) { m_type = Type.int_; m_int = v; return v; } | |||
/// ditto | |||
BigInt opAssign(BigInt v) { m_type = Type.bigint; initBigInt(); m_bigint = v; return v; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IDK how BigInt
are implemented, but I can see the cycle: 'blitting the default value' -> 'assigning data' -> 'Do it again' as potentially problematic: if BigInt
use malloc
ed memory, it's more likely that on assignement, either malloc
or free
(or both) happen.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IDK how BigInt are implemented, but I can see the cycle
Default value may be only static value, because BigInt is a struct, and BigInt.init should be known at compile time.
Thus everytime, a
, b
and c
in the following example has a identical (bitwise) value. Thus I simply write BigInt.init to the m_data.
BigInt a;
BigInt b;
BigInt c;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I was refering to was, if someone calls opAssign
with a BigInt
value multiple time, the finalizers of all but the last 'instance' will not be run.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This case is fixed.
initBigInt()
is called only if old m_type
in assign is not Type.bigint
However there are another situation:
Json j = BigInt(...);
j = 5;
I may add a special destructor for it, however BigInt doesn't have ~this
, I've checked id.
What do you think?
That request is just to make the history easier to understand. I often find myself As per the rollback, with git, you can easily checkout a branch that points to your current commit, and squash them together. |
e9d66b6
to
02de141
Compare
BigInt opAssign(BigInt v) | ||
{ | ||
if (m_type != Type.bigint) | ||
initBigInt(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC, in the following code:
Json bi;
bi = BigInt("42");
bi = 42L;
bi = BigInt("42");
The bug is still present, isn't it ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I've fixed it. (see the below description)
@IgorStepanov WRT |
@Geod24 The destructor issue is actually a good point. In fact, the same issue is in std_data_json/ Also, I'll have to find the tagged union implementation for Phobos again that I had started some time ago and submit a PR. |
@s-ludwig @Geod24 I've fixed BigInt destruct issue. |
Looks good to me now. I'll just make some minor, mostly cosmetic, adjustments after merge. Does anyone else still see any issue? |
Well... I look forward to |
Good thing is that this change improves compatibility with Merging now. Thanks Igor for your work! |
- Json.Type.bigint -> "bigInt" (to match "BigInt") - Move private methods to the bottom of struct Json - Move type check into finiBigInt and rename to runDestructors
Okay, got the changes in. The only interface change has been to rename |
Thanks! |
I could tag an alpha/beta release right away. |
Okay 0.7.24-beta.1 is tagged now. |
Thanks a lot! :) |
??? |
I don't think that this really matters, since this is JSON and not JavaScript. JSON doesn't specify anything about the numerical representation, so the implementation is basically free to choose whatever fits. Since large integers are used in practice, it would be quite arbitrary to not support them. |
Hello, Vibe.D!
I've written about my problem on forum: http://forum.rejectedsoftware.com/groups/rejectedsoftware.vibed/thread/25482/
I use vibe.d on small commercial product, and two days ago I got this trouble.
One REST server (api.shodan.io) sent me a JSON like
{..., serial: 17559991181826658461, ...}
I read the official JSON standard: http://json.org/
Unfortunately, it doesn't define bounds for a number value, thus
99999999999999999999999999
is a correct JSON value.However the most real systems uses 64-bit (as maximum) signed and unsigned integers for describe integral values.
Thus, supporting of ulong is very needed for users.
I've added a new
Type
value:uint_
, which representsulong
values.I prefer
int_
type instead ofuint_
when it possible.If the integral value x <= long.max, I write is to Json as
int_
even if it hasn't the sign.If this way is unacceptable, please suggest another way to parse ulong values.
We may convert it to long or to string.
Get an exception during parsing of correct JSON answer of a REST server is very, because I can't get the REST answer as string (using
vibe.web.rest
) and parse it manually.Thanks.