Skip to content
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

System.Text.Json: Add JsonReaderState.LineNumber and BytePositionInLine Property #50629

Closed
weifenluo opened this issue Apr 2, 2021 · 4 comments
Labels
api-needs-work API needs work before it is approved, it is NOT ready for implementation api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Text.Json
Milestone

Comments

@weifenluo
Copy link

weifenluo commented Apr 2, 2021

Background and Motivation

In order to write a descent custom JSON deserializer, the LineNumber and BytePositionInLine, along with Path information should be provided in JsonException. The only way to get LineNumber and BytePositionInLine is through JsonReaderState struct. Unfortunately they're internal, which can only be consumed by the standard built-in deserializer.

JsonException has constructor to accept LineNumber and BytePositionInLine parameter, but this information is not available unless using reflection.

Proposed API

namespace System.Text.Json
{
    public struct JsonReaderState
    {
        public long LineNumber => _lineNumber;
        public long BytePositionInLine => _bytePositionInLine;
    }
}

Usage Examples

private static JsonException GetJsonExceptionToRethrow(string message, string path, JsonReaderState readerState, Exception ex)
{
    return new JsonException(message, path, readerState.LineNumber, readerState.BytePositionInLine, ex);
}

Alternative Designs

JsonReaderState is the only source of the information. Naturally this is the only way.

Risks

Don't see any risk.

@weifenluo weifenluo added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Apr 2, 2021
@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Apr 2, 2021
@ghost
Copy link

ghost commented Apr 2, 2021

Tagging subscribers to this area: @eiriktsarpalis, @layomia
See info in area-owners.md if you want to be subscribed.

Issue Details

Background and Motivation

In order to write a descent custom JSON (de)serializer, the LineNumber and BytePositionInLine, along with Path information should be provided in JsonException. The only way to get LineNumber and BytePositionInLine is through JsonReaderState struct. Unfortunately they're internal, which can only be consumed by the standard built-in deserializer.

JsonException has constructor to accept LineNumber and BytePositionInLine parameter, but this information is not available unless using reflection.

Proposed API

namespace System.Text.Json
{
    public struct JsonReaderState
    {
        public long LineNumber => _lineNumber;
        public long BytePositionInLine => _bytePositionInLine;
    }
}

Usage Examples

private static JsonException GetJsonExceptionToRethrow(string message, string path, JsonReaderState readerState, Exception ex)
{
    return new JsonException(message, path, readerState.LineNumber, readerState.BytePositionInLine, ex);
}

Alternative Designs

JsonReaderState is the only source of the information. Naturally this is the only way.

Risks

Don't see any risk.

Author: weifenluo
Assignees: -
Labels:

api-suggestion, area-System.Text.Json, untriaged

Milestone: -

@layomia
Copy link
Contributor

layomia commented Jun 1, 2021

I agree this could be good information for custom serializers to provide, but would like to see more users ask for this before exposing API here (either on JsonReaderState or Utf8JsonReader itself). I'll move to future for now.

@layomia layomia removed the untriaged New issue has not been triaged by the area owner label Jun 1, 2021
@layomia layomia removed their assignment Jun 1, 2021
@layomia layomia modified the milestones: 6.0.0, Future Jun 1, 2021
@sjschukin
Copy link

I have to write custom deserializer that skips some JSON fields with invalid data. Unfortunately I've to use reflection to show the position where the error occured. Voted for this feature.

@eiriktsarpalis eiriktsarpalis modified the milestones: Future, 7.0.0 Aug 6, 2021
@eiriktsarpalis eiriktsarpalis added the api-needs-work API needs work before it is approved, it is NOT ready for implementation label Oct 14, 2021
@eiriktsarpalis
Copy link
Member

Duplicate of #28482

@eiriktsarpalis eiriktsarpalis marked this as a duplicate of #28482 Oct 19, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Nov 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-needs-work API needs work before it is approved, it is NOT ready for implementation api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Text.Json
Projects
None yet
Development

No branches or pull requests

4 participants