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

JSON serialization of XML Comments appears to be inconsistent #3019

Open
AndersNestech opened this issue Feb 17, 2025 · 0 comments
Open

JSON serialization of XML Comments appears to be inconsistent #3019

AndersNestech opened this issue Feb 17, 2025 · 0 comments

Comments

@AndersNestech
Copy link

With two consecutive comments in the source XML, the comment text is lost in JSON.

            var xml = XElement.Parse("<bar> <!-- comment text --></bar>");

            var json = Newtonsoft.Json.JsonConvert.SerializeXNode(xml); // produces {"bar":{/* comment text */}}

whereas

            var xml = XElement.Parse("<bar><!-- comment1 --> <!-- comment2 --></bar>");

            var json = Newtonsoft.Json.JsonConvert.SerializeXNode(xml); // produces {"bar":{"#comment":[]}}

Arguably, comments should never be emitted by SerializeXNode - Douglas Crockford has apparently stated that:

JSON does not have comments. A JSON encoder MUST NOT output comments. A JSON decoder MAY accept and ignore comments.

but as they are emitted, consistency seems to dictate that these three comments should all have their full comment text in the output.

ShadowDanceMaster added a commit to ShadowDanceMaster/Newtonsoft.Json that referenced this issue Mar 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant