-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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 ability to serialize Comments on Properties #35251
Comments
Tagging subscribers to this area: @jozkee |
No. That's not "JSON". It's a JSON-like format. As long as JSON has official spec, and something doesn't conform to that spec, it's not "JSON" by any means. Use jsonc, hjson, JSON5, YAML, XML, or whatever you like instead. Don't expect the only stable and sensible data format in the wild to break just because you can't understand how to use it. See https://stackoverflow.com/questions/244777/can-comments-be-used-in-json , https://nlohmann.github.io/json/features/comments/ etc. for related discussion. |
This is a valid feature request that (according to the votes) other users also need.
That's right. As long as it's opt in (and that's what I am proposing here), the default behavior conforms with "the" standard. The parser already has support for comments in deserialization (which is also not "JSON compliant"). This feature request just asks to extend that support. Also, notice my comment requesting this feature as a separate library if it doesn't make it into the core APIs. That would be fine, too.
Please be polite. I didn't do anything to you. We're all just trying to create APIs that make us developers happy and more productive. |
I concur with @FyiurAmron's comments. It's unlikely such a design would be accepted. |
@eiriktsarpalis A lot of projects are currently dichting Newtonsoft.JSON, since that project is basically abandoned in favor of System.Text.Json. Pulling in an entirely separate JSON parsing library would be counter-productive and may cause dependency problems (which is one of the reasons why ASP.NET ditched Newtonsoft.JSON). It's fine closing this issue when it doesn't align with the project's goals. I'm also open for recommendations from @FyiurAmron for libraries that support that proposed feature, since the problem hasn't gone away. |
It might be possible in the future via #60518. We're considering implementing this in 7.0.0 |
Does this have a similar API surface as Newtonsoft's JSON? If I get this right, this would not offer the ability to implement the feature proposed here (feel free to correct me). More details in this issue: JamesNK/Newtonsoft.Json#1211 |
No, it's not possible, since current design consists of tightly-coupled classes, with virtually no way to inject 3rd-party behaviour in the (de)serializer. It's borderline possible via reflection and assembly hacking, but I assume that's not what you want.
Once again, you're barking at a wrong tree. Please, for the sake of the argument, read what I wrote at least once with actual understanding on your side. It's not a "JSON parsing library" once you ditch the RFC. You require something that is not a JSON library. I'm not arguing you shouldn't do it. Do it, if it benefits you. Just be aware that using a non-standard format means you'll have to support it yourself, and that may (or may not) be counter-productive. As far as I know, support for emitting comments was omitted (even if intended at some point, at least looking at the docs & code) from First, Currently, those classes are If you really think this feature is worthwhile enough to warrant rewriting a lib that took, in essence, some years to complete, feel free to make a proposal to change the design of
I've already did that; I've already answered your question. FWIW, did you wonder why e.g. JSON5 for C# was abandoned? (Hint: yes, it's because it's a big and non-trivial piece of code, and it has to be maintained, and nobody wanted to do it) |
Sorry, I seem to have overlooked it. Json5 doesn't work for my use case, but maybe it will work for others reading this issue later, thanks!
When using System.Text.Json's APIs/primitives, that kind of stuff would be a problem of the implementor, not this project, if this would be possible to do so.
Well, skipping comments is implemented, they are non-standard as well. On the companion issue of Newtonsoft's JSON, it was mentioned that "implementing it ATM is trivial", so I assumed that it would be here, too due to some similarities in the design. Once again, I understand that this may not align with the project's goals. No need to discuss that any further. |
sigh I'll be repeating myself here (at least partially), but that can't be reasonably avoided or expected to be avoided. https://github.com/hjson/hjson-cs FWIW, I know of no RFC-conforming JSON parser that would emit comments (like I said, the producer is bound to be more strict than the consumer; lax parsers are commonplace to handle the abuse of the standard in the wild).
IMVHO, in
https://seriot.ch/projects/parsing_json.html Decision to accept a non-conforming input that is semi-common is a result of a conscious compromise, not an argument that the serializer "[does] not [have to be] compliant to the standard".
Yes, in JSON.NET it actually is/was, as you already know.
That's the one assumption that went too far. The interface is similar (since that was the intention), the implementation, arguably, is not.
Just please remember, that there are also different approaches to software engineering ฅ ̳͒•ˑ̫• ̳͒ฅ |
Well, except the reference to Yaml, all of the link you posted were never mentioned before in any related thread. Using a YAML parser to parse JSON would implicitly allow the input to be YAML, which is not what we tried to achieve in this feature request. There are reasons to avoid YAML for configuration files. Unless that YAML parser can be restricted to only allow JSON, that would not solve the problem. Also, the comment sign
Basically the same story as for YAML. HJSON (like JSON5 and JSON6) is very uncommon and not even supported in most editors (unlike JSON/JSONC), which is mandatory for use in config files. That would actually solve the problem, since thats basically what this FR (and the companion issue in Newtonsoft.JSON) is all about. Sadly, as you said, it is not written in C# and must basically be re-written in its entirety. (De)serializing stuff in JS is much simpler due to the dynamic type system, so this cannot be ported over 1:1 to C#.
I'd do that, but that fork/patch/draft PR doesn't even implement the feature how it was proposed. It's open for comments, though.
That must be why this repository has a code of conduct that prohibits such "approaches". |
They were, if you'd kindly spend 5 minutes using Google.
AFAIK, no line in .NET CoC prohibits sense of humour. I had already advised you on that on numerous occasions, and I'd suggest you take that suggestion, instead of facilitating the "broken record" approach. For me, this discussion has run its course. |
System.Text.Json already supports comments during deserialization.
JSON config files of VSCode and TypeScript and other programs use comments on their properties to add some in-line documentation.
For programs that use JSON as a config format, it would be nice if they could just annotate their classes and have these annotations as comments in the serialized JSON. This would be useful to generate a default/initial configuration file.
For example, consider:
Could serialize to something like this:
or this:
Of course, multi-line comments could be used instead of single-line comments (just mimicking VSCode's config here).
If not baked directly into the API, it would be nice if we had the ability to add this comment serialization as a separate library.
There is some need for the feature according to this SO question: https://stackoverflow.com/questions/9495718 and JamesNK/Newtonsoft.Json#1211
The text was updated successfully, but these errors were encountered: