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, addition needed for JsonWriterOptions, to avoid URI serialization issue #95182

Closed
Thorium opened this issue Nov 23, 2023 · 11 comments

Comments

@Thorium
Copy link
Contributor

Thorium commented Nov 23, 2023

If you use System.Text.Json with JsonWriterOptions, by default settings (Indented off), it will create a following JSON:
{"url":"https://google.com","prop":"x"}

...which looks correct to me, but as you can see, the major automatic parsers like GitHub, Google, Microsoft, etc fail to end the url link to the end-quote, and treat the URL as combining all the rest of the stuff going as part of that url. And as you can see, the full url is not working.

If a single space is added after the comma, then behold, the parsers are clever now:
{"url":"https://google.com", "prop":"x"}

If you do Indented=true, then there is way too much unnecessary characters for a production system.
Could we consider adding a serialization option of "a space after comma", please?

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Nov 23, 2023
@ghost
Copy link

ghost commented Nov 23, 2023

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

Issue Details

If you use System.Text.Json with JsonWriterOptions, by default settings (Indented off), it will create a following JSON:
{"url":"https://google.com","prop":"x"}

...which looks correct to me, but as you can see, the major automatic parsers like GitHub, Google, Microsoft, etc fail to end the url link to the end-quote, and treat the URL as combining all the rest of the stuff going as part of that url. And as you can see, the full url is not working.

If a single space is added after the comma, then behold, the parsers are clever now:
{"url":"https://google.com", "prop":"x"}

If you do Indented=true, then there is way too much unnecessary characters for a production system.
Could we consider adding a serialization option of "a space after comma", please?

Author: Thorium
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

@gregsdennis
Copy link
Contributor

gregsdennis commented Nov 23, 2023

This, to me, seems like a bug in their parsers. STJ is producing valid JSON.

@Thorium
Copy link
Contributor Author

Thorium commented Nov 24, 2023

if all the parsers are doing it wrong, we'd need to adopt...

@gregsdennis
Copy link
Contributor

I disagree. Deviation from standard should never become common practice.

@eiriktsarpalis
Copy link
Member

eiriktsarpalis commented Nov 24, 2023

Could you share an example of a JSON parser that is doing this? It seems pretty flagrant.

Generally speaking the STJ parser is pretty strict about compliance of the JSON that it accepts, see #32291 (comment).

@vcsjones
Copy link
Member

vcsjones commented Nov 25, 2023

major automatic parsers like GitHub

GitHub is not “parsing” the JSON in your example. It’s just a markdown rendering that automatically converts links in to clickable links. Our markdown pipeline does not look in issue content for “does this look like JSON?” and try to interpret it.

I would expect someone to use a code fence in this situation to avoid this.

{"url":"https://google.com","prop":"x"}

Spacing aside, you will still run in to problems with less than or greater than signs

For example, this would incorrectly be displayed:

{
    "potato": "foo<bar>"
}

{
"potato": "foo"
}

As <bar> is interpreted as an HTML tag.

@Thorium
Copy link
Contributor Author

Thorium commented Nov 26, 2023

Adding one spacing option that is still within the standard syntax shouldn't be too hard...

I don't even mind Github but the same is done by Word, Outlook and others.

My problem is that I provide WebApi returning JSON and the customers of that API complain the URLs being wrong, and when things are escalated to manager level, the managers clearly see the URLs being wrong. I would expect the case not to be unique.

Edit: Of course one can just say .ToString().Replace("\",\"", "\", \""); and everything works fine and business people are happy, but that kind of questions the full usage of System.Text.Json with performance, streaming support, etc.

@eiriktsarpalis
Copy link
Member

eiriktsarpalis commented Nov 27, 2023

Adding one spacing option that is still within the standard syntax shouldn't be too hard...

It's adding yet another source of branching in the hot path though. We might consider it if this were some kind of web standard, but it seems to me that the only motivating use case is to provide a workaround for a specific bug in tools that weren't designed to display JSON in the first place. And per @vcsjones's comment it's not even solving the problem in its entirety.

Does Json.NET have a formatting option that works around the issue?

@Thorium
Copy link
Contributor Author

Thorium commented Nov 27, 2023

Does Json.NET have a formatting option that works around the issue?

If you do Newtonsoft.Json.Formatting.None, it's as compact and fast as it can,
but if you do Newtonsoft.Json.Formatting.Indented, you can control everything:
https://www.newtonsoft.com/json/help/html/Properties_T_Newtonsoft_Json_JsonTextWriter.htm

@eiriktsarpalis
Copy link
Member

But isn't it the case that this only controls the indentation level and the indentation char (FWIW we're tracking an equivalent feature in #63882)? That wouldn't suffice to give you the desired behavior though.

@Thorium
Copy link
Contributor Author

Thorium commented Nov 29, 2023

As there are workarounds available, and this seems to be not supported scenario in the near future, I'll just close this issue.

@Thorium Thorium closed this as completed Nov 29, 2023
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Nov 29, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Dec 30, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants