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

JSON5: Provide a way to read/write a comment using JsonNode #734

Open
ikhoon opened this issue Dec 20, 2021 · 3 comments
Open

JSON5: Provide a way to read/write a comment using JsonNode #734

ikhoon opened this issue Dec 20, 2021 · 3 comments

Comments

@ikhoon
Copy link

ikhoon commented Dec 20, 2021

I'd like to parse JSON5 containing comments. JsonReadFeature#ALLOW_JAVA_COMMENTS ignores comments when deserializing a JSON into a JsonNode.

private void _skipComment() throws IOException
{
if ((_features & FEAT_MASK_ALLOW_JAVA_COMMENTS) == 0) {

As a result, we lose the comment information and cannot read/recover the original content from the parsed JsonNode.
A commentable JsonNode is useful so as to retrieve the original comment and serialize the JsonNode to JSON5 format.

@cowtowncoder
Copy link
Member

Ok. So, it might be possible to write some comments through JsonNode by using "raw value" (and at streaming level, JsonGenerator.writeRaw() (NOT "rawValue" actually)) but that is definitely cumbersome.
But the real issue, and the reason why I am not sure we can ever fully support comments same way as regular tokens is that they can appear at any point in content and can not really be modeled well as nodes.

One thing that might be doable on reading side would be to allow registration of a callback to call when comment is encountered. This alone would only work at lowest level and require caller to handle difference between tokens read, comments, and somehow retain comment information. Further, any stylistic knowledge (white-space surrounding comment) would be lost.

So... while I can understand the need for exposing comments, I have never figured out a way they could be nicely handled at API level. In XML things are bit easier than JSON, in this case.

@ikhoon
Copy link
Author

ikhoon commented Dec 27, 2021

First of all, thank you for your interest in this issue. 🙇‍♂️

I am not sure we can ever fully support comments same way as regular tokens is that they can appear at any point in content and can not really be modeled well as nodes.

Agreed. It should be difficult to fully support comments. I speculated that some limited comments could be retained in positions right above nodes as a known limitation.

{
   // A single line note for a node
   foo: "bar",
   /**
    * Multiline note for a node
    * could be supported.
    */
   hello: "world"
}

I have never figured out a way they could be nicely handled at API level.

I'm not so sure that the following API style is possible but I just imagined:

interface CommentableNode {
    String comment();
}

public class CommentableArrayNode extends ArrayNode implements CommentableNode {
    ...
}

JsonNode node = ...;
if (node instanceof CommentableNode) {
    // do something with the comment
}

@cowtowncoder
Copy link
Member

cowtowncoder commented Jan 1, 2022

Unfortunately I don't thing add-on interfaces help a lot with the core issue (or maybe I just don't see it).
Support would still need to be added in JsonNode implementations.
But I guess there are multiple types of challenges in modeling here, both from containing and exposing information in general, and then api design etc.

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

2 participants