-
Notifications
You must be signed in to change notification settings - Fork 4.9k
CoreFx #36125 (Try)Get for byte sbyte short ushort #38469
Conversation
src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.TryGet.cs
Outdated
Show resolved
Hide resolved
</data> | ||
<data name="FormatUInt16" xml:space="preserve"> | ||
<value>The JSON value is either of incorrect numeric format, or too large or too small for a UInt16.</value> | ||
</data> |
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.
Nit: grammatically, these should be reworded slightly, e.g.
The JSON value either uses an incorrect numeric format or is too large or too small for a Byte.
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.
Also, would it make sense to include in the error message the value that couldn't be parsed?
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.
Nit: grammatically, these should be reworded slightly, e.g.
Also, would it make sense to include in the error message the value that couldn't be parsed?
@stephentoub does this sound ok? Will make changes in all the places accordingly.
The JSON value could not be parsed as a number, uses incorrect numeric format or is too large or too small for a Byte.
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.
Functionally speaking, "uses an incorrect numeric format" is redundant with "could not be parsed as a number".
sbyte.Parse
's failures are "Input string was not in a correct format." or "Value was either too large or too small for a signed byte."
Putting them together into one, I'd go with
The JSON value either was not in a supported format, or is out of bounds for a signed byte.
("out of bounds" to remove the second "or")
It looks like the primitive Parse methods use the type name (e.g. Int32) except for byte/sbyte, where it's "unsigned byte" (lowercase) and "signed byte" (lowercase). https://github.com/dotnet/coreclr/blob/29810a78e5b93d8da9fb921d096226d249fc75a5/src/System.Private.CoreLib/Resources/Strings.resx#L3130-L3162
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.
The JSON value either was not in a supported format, or is out of bounds for a signed byte.
Should the tense be same in both clauses? We have was
and is
here. Intrigued, I searched and came across this,
https://grammar.collinsdictionary.com/easy-learning/joining-clauses
<quote>
When it is used in this way either must come in one of these places:
- before the subject in the first clause of the group.
- in front of the main verb and after any auxiliary verb.
</quote>
Looks like either
needs to be before the subject
in this case? E.g.,
Either the JSON value is not in a supported format, or is out of bounds for a signed byte.
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.
Either the JSON value is not in a supported format, or is out of bounds for a signed byte.
That exception message looks good to me.
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 suggest to include the invalid value. Can be pretty helpful for offline analysis. Quite often for example in random unit test failures in corefx we end up going in to add more logging of the actual value. Up to you
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 suggest to include the invalid value.
Do you mean the actual JSON string? I think transcoding the ValueSpan
/ValueSequence
back to UTF-16 string and appending that to the exception message might be useful.
string json = "[12345, 1]";
byte[] utf8 = Encoding.UTF8.GetBytes(json);
var reader = new Utf8JsonReader(utf8, ...);
reader.Read(); // Start Array
reader.Read(); // Number
// This method call will throw with the message:
// "Either the JSON value is not in a supported format, or is out of bounds for a signed byte."
// What should we append here? Re-parse "12345" as a larger numeric type and append the value to the exception message?
// OR, take the raw utf-8 bytes that represent 12345, transcode it back to UTF-16 string, and append that to the exception message?
reader.GetByte();
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.
@danmosemsft @ahsonkhan given the 6/26 deadline and that we haven't been including the actual JSON fragement into the error message until now, I am inclined to change the error message to the Either the JSON value is...
string discussed above. For more changes perhaps we can have another issue?
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.
That sounds good to me.
src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.TryGet.cs
Show resolved
Hide resolved
@ahsonkhan I have added equivalent APIs in JsonElement. |
public bool TryGetUInt32(out uint value) { throw null; } | ||
[System.CLSCompliantAttribute(false)] | ||
public bool TryGetUInt64(out ulong value) { throw null; } | ||
public bool TrySkip() { throw null; } | ||
public bool ValueTextEquals(System.ReadOnlySpan<byte> utf8Text) { throw null; } | ||
public bool ValueTextEquals(System.ReadOnlySpan<char> text) { throw null; } | ||
public bool ValueTextEquals(string text) { throw null; } | ||
public bool ValueTextEquals(string utf8Text) { throw null; } |
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 name change seems bad. Presumably the ref got manually corrected at some point and the src is out of date. (Or the src got changed after the last ref generation)
The correct name here is text
, as System.String isn't UTF-8.
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.
The discrepancy of parameter name between src and ref has been since introduction of the new API. Reference: f09135a
I will revert here and also change the parameter name in the API so that ref code auto-generated in future won't have this issue.
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.
The same issue came up here: #38409 (comment)
I will revert here and also change the parameter name in the API so that ref code auto-generated in future won't have this issue.
That would be awesome! I was just about to submit a PR for this change, but I will wait for you to update it instead. Thanks.
|
||
Assert.Throws<InvalidOperationException>(() => root.GetString()); | ||
const string ThrowsAnyway = "throws-anyway"; | ||
Assert.Throws<InvalidOperationException>(() => root.ValueEquals(ThrowsAnyway)); |
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.
There are a number of tests in this file that check for the InvalidOperationException on GetInt32(); the new one-and-two-byte-integer methods should be added to those tests as well.
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.
Adding the same! Thanks!
@@ -148,8 +160,8 @@ public abstract partial class JsonNamingPolicy | |||
public partial struct JsonReaderOptions | |||
{ | |||
private int _dummyPrimitive; | |||
public bool AllowTrailingCommas { get { throw null; } set { } } | |||
public System.Text.Json.JsonCommentHandling CommentHandling { get { throw null; } set { } } | |||
public bool AllowTrailingCommas { readonly get { throw null; } set { } } |
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.
If you pull from head of master into your branch, these changes will go away.
src/System.Text.Json/src/System/Text/Json/Document/JsonElement.cs
Outdated
Show resolved
Hide resolved
public float GetSingle() { throw null; } | ||
public string GetString() { throw null; } | ||
[System.CLSCompliantAttribute(false)] | ||
public uint GetUInt16() { throw null; } |
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 should be ushort
public uint GetUInt16() { throw null; } | |
public ushort GetUInt16() { throw null; } |
src/System.Text.Json/src/System/Text/Json/Document/JsonElement.cs
Outdated
Show resolved
Hide resolved
src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.TryGet.cs
Outdated
Show resolved
Hide resolved
byte[] b16 = new byte[2]; | ||
for (int i = 0; i < numberOfItems; i++) | ||
{ | ||
Shorts.Add(BitConverter.ToInt16(b16)); |
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 isn't really adding new short
values (it keeps adding 0 to the list). Same with ushort
test cases below.
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.
Missed random.NextBytes
. Added.
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.
The BitConverter.ToInt16
is implicitly casting the byte[]
to a ReadOnlySpan<byte>
and that API isn't available on netfx. Try using the other overload that takes byte[]
and index.
https://apisof.net/catalog/System.BitConverter.ToInt16(Byte(),Int32)
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.
Yup that whole test case is broken as you pointed out (it is putting in zeros). In few moments I am uploading a new patch which has all these fixes...
json = default; | ||
JsonTestHelper.AssertThrows<InvalidOperationException>(json, (jsonReader) => jsonReader.TryGetSingle(out _)); | ||
JsonTestHelper.AssertThrows<InvalidOperationException>(json, (jsonReader) => jsonReader.GetSingle()); | ||
|
||
json = default; | ||
JsonTestHelper.AssertThrows<InvalidOperationException>(json, (jsonReader) => jsonReader.TryGetUInt16(out _)); |
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.
These new test additions look good. Can you please share the code coverage numbers for the new APIs being added?
This needs to be committed by noon 6/26 to make 3.0. |
@WinCPP, let me know if you need any help or another review. I think just a few pieces of feedback are left to resolve (+ fixing the merge conflict). |
@ahsonkhan I am not able to run tests after doing rebase. I get following errors. Did a clean build, but still same issues. Has something changed...? Please help.
|
Not that I am aware off. I just grabbed master, and was able to build/run the S.T.Json.Tests myself. Can you try building If that doesn't work, try
What did you do for a clean build and what command did you use to run the tests?
|
In a different terminal window:
In the first window:
I don't really know why this is needed, but I got bit by it, too, and that was a viable workaround for me (different project, but same symptom) |
Yup had tried
Thanks for inputs I could proceed but only till some extent. Now the test cases are failing for something not related to my code change, errors as below. Just to re-confirm I switched to
One observations if it helps tell more about my system: On my system, for |
Are many tests failing or just the That particular test is trying to make sure the options cache is working as expected so there might be a bug that only reproduces intermittently. If it is consistently failing, can you please share the whole call stack as well, because I wasn't able to repro this issue (and it isn't happening on CI either). cc @steveharter I can try checking out your branch to see, is this it: https://github.com/WinCPP/corefx/tree/newtrygets? |
Many test cases are failing with same reason. Please note this is happening on master (without my changes) as well. Will unblock myself by skipping. I didn't do that thinking something went wrong with my code base after rebase to dotnet/master (including hostpolicy.dll) until I found it happening on master too. I am using
Please find whole output here in a gist file.
Yes above is the branch with latest changes (I just pushed them, please note they are not tested). However the above gist was captured using master branch https://github.com/WinCPP/corefx. I have not rebased it again with dotnet/master after this morning. |
@ahsonkhan please refer to above reply. After marking the failing tests as In the automated build |
Given they are not failing in CI, and I can't repro it locally either, it doesn't look like an issue. There may be some issue with the culture setting on your machine (where date times are being formatted differently), but I am not sure. One thing that might be worth trying is debug/output the DateTime value of the object being serialized from one of the tests. It should be in the correct (ISO) format. For instance, at this point, what does
There are issues tracking these, so we can ignore them for now. |
…et/corefx#38469) * CoreFx dotnet/corefx#36125 (Try)Get for byte sbyte short ushort * Init out values on failure return paths. * APIs in JsonElement and related tests * More test cases, test bug fixes, code fixes Commit migrated from dotnet/corefx@b2af402
Addresses (Try)Get APIs for byte, sbyte, short and ushort for #36125
@ahsonkhan @steveharter Kindly review.