Runtime: System.Text.Json : Failing at JsonDocument.Parse(json: fullName)

Created on 20 Aug 2019  路  6Comments  路  Source: dotnet/runtime

Hi,

testing "System.Text.Json" Version="4.6.0-preview8.19405.3" in VS 2019 16.2.2, I try to load a json file (UTF8 codified) with this content:

{
"option1": "value1"
}

with var doc = JsonDocument.Parse(json: fullName) but I receive this exception:

System.Text.Json.JsonReaderException
HResult=0x80131500
Message='C' is an invalid start of a value. LineNumber: 0 | BytePositionInLine: 0.
Source=System.Text.Json
StackTrace:
at System.Text.Json.ThrowHelper.ThrowJsonReaderException(Utf8JsonReader& json, ExceptionResource resource, Byte nextByte, ReadOnlySpan1 bytes) at System.Text.Json.Utf8JsonReader.ConsumeValue(Byte marker) at System.Text.Json.Utf8JsonReader.ReadFirstToken(Byte first) at System.Text.Json.Utf8JsonReader.ReadSingleSegment() at System.Text.Json.Utf8JsonReader.Read() at System.Text.Json.JsonDocument.Parse(ReadOnlySpan1 utf8JsonSpan, Utf8JsonReader reader, MetadataDb& database, StackRowStack& stack)
at System.Text.Json.JsonDocument.Parse(ReadOnlyMemory1 utf8Json, JsonReaderOptions readerOptions, Byte[] extraRentedBytes) at System.Text.Json.JsonDocument.Parse(ReadOnlyMemory1 json, JsonDocumentOptions options)
at System.Text.Json.JsonDocument.Parse(String json, JsonDocumentOptions options)
at ConsoleCore.Program.Main(String[] args) in C:\SRC_HOST\Protos\0300\Proto.Runtime\ConsoleCore\Program.cs:line 27

Please can you help me?

area-System.Text.Json question

Most helpful comment

I think the issue is that you are passing in path, instead of content, the I/O operation would need a separate call:

- JsonDocument.Parse("C:/path/to/test.json");
+ JsonDocument.Parse(System.IO.File.ReadAllText("C:/path/to/test.json"));

All 6 comments

Hmm. Sounds like there's a BOM (or something else, don't recognize the 'C' value) in there? Might be fixed with https://github.com/dotnet/corefx/pull/38470? Could you attach the file?

Attached!
test.zip

@khellang

I have changed from UTF8 to UTF8 with BOM using Notepad++ but same exception is raised again.

Cheers

I think the issue is that you are passing in path, instead of content, the I/O operation would need a separate call:

- JsonDocument.Parse("C:/path/to/test.json");
+ JsonDocument.Parse(System.IO.File.ReadAllText("C:/path/to/test.json"));

Can you share the repro app, because I can't repro the issue.
C# string jsonString = File.ReadAllText(@"E:\test.json"); using (JsonDocument dom = JsonDocument.Parse(jsonString)) { Assert.Equal("{\r\n \"option1\": \"value1\"\r\n}", dom.RootElement.ToString()); }

I think the issue is that you are passing in path, instead of content, the I/O operation would need a separate call

That's probably it :)
Good guess @am11

Hi @am11

Solved. You were right. I had to pass json content instead filename. Now works:

doc

Cheers!!!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Timovzl picture Timovzl  路  3Comments

omariom picture omariom  路  3Comments

EgorBo picture EgorBo  路  3Comments

chunseoklee picture chunseoklee  路  3Comments

GitAntoinee picture GitAntoinee  路  3Comments