Skip to content

System.Text.Json.JsonReader is not reading numbers correctly #54096

@Gonkers

Description

@Gonkers

Description

System.Text.Json cannot read JSON that just consists of just a number. According to the specifications, this should be valid.

System.Text.Json.JsonReaderException
  HResult=0x80131500
  Message='0x00' is an invalid end of a number. Expected 'E' or 'e'. LineNumber: 0 | BytePositionInLine: 5.
  Source=System.Text.Json

Here are some unit tests to reproduce the issue.

        public class NumberValueTests
        {
            public static JsonTokenType GetTokenType(string jsonValueOnly, byte[] buffer)
            {
                var bytesEncoded = Encoding.UTF8.GetBytes(jsonValueOnly, buffer);
                var jsonReaderOptions = new JsonReaderOptions { AllowTrailingCommas = true, CommentHandling = JsonCommentHandling.Skip };
                var readerState = new JsonReaderState(jsonReaderOptions);
                var reader = new Utf8JsonReader(buffer, false, readerState);
                reader.Read();

                return reader.TokenType;
            }


            [Fact]
            public void  NumberValues()
            {
                Assert.Equal(JsonTokenType.String, GetTokenType("\"4444\"", new byte[10]));
                Assert.Equal(JsonTokenType.False, GetTokenType("false", new byte[10]));
                Assert.Equal(JsonTokenType.True, GetTokenType("true", new byte[10]));
                Assert.Equal(JsonTokenType.Null, GetTokenType("null", new byte[10]));

                // !!!  THESE SHOULD BE NUMBER  !!!
                Assert.Equal(JsonTokenType.Number, GetTokenType("44.44", new byte[5]));
                Assert.Equal(JsonTokenType.Number, GetTokenType("4444", new byte[4]));

                // !!!  THIS SHOULD NOT THOW AN INTERNAL EXCEPTION  !!!
                Assert.Throws<System.Text.Json.JsonReaderException>(() => GetTokenType("4444", new byte[10]));
            }
        }

Configuration

  • Which version of .NET is the code running on? 5.0.300
  • What OS and version, and what distro if applicable? Windows 10
  • What is the architecture (x64, x86, ARM, ARM64)? x64
  • Do you know whether it is specific to that configuration? I don't know.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions