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

Add comment support to json #170

Closed
altro3 opened this issue Mar 27, 2022 · 18 comments · Fixed by #778
Closed

Add comment support to json #170

altro3 opened this issue Mar 27, 2022 · 18 comments · Fixed by #778
Labels
status: awaiting feedback status: under consideration The issue is being considered, but has not been accepted yet

Comments

@altro3
Copy link
Contributor

altro3 commented Mar 27, 2022

Feature description

This is not a common case, but sometimes when you use json for the configuration format, you need to make some notes inside the json . I know the json standard doesn't support comments, but for example jackson has a feature to deserialize json with comments.

There is currently no way to configure objectMapper in serde to support comments. I also think that it is necessary to add the ability to create a custom objectMapper with non-typical settings, for example, support for comments :-).

@graemerocher graemerocher added the status: under consideration The issue is being considered, but has not been accepted yet label Mar 31, 2022
@altro3
Copy link
Contributor Author

altro3 commented Jan 30, 2024

@graemerocher @dstepanov @sdelamo Any news about this ?

You need to understand why this feature is extremely necessary:

Without it, it is impossible to use the json format for configuration normally. Of course, comments are required in large configurations. otherwise, it is impossible to work normally with such a configuration. If anything, jackson supports comments in json

@dstepanov
Copy link
Contributor

You can configure Jackson read/write features see SerdeJacksonConfiguration

@sdelamo
Copy link
Contributor

sdelamo commented Jan 31, 2024

As you said, JSON does not support comments in the specification. I would not complicate the code to support a not standard feature.

Moreover, we are not recommending people to use JSON for configuration but formats such as properties, yaml, toml. Formats which support comments.

@altro3
Copy link
Contributor Author

altro3 commented Jan 31, 2024

The decision on the choice of format does not always depend on you. Yes, I understand that the specification does not provide for comments in json. But all popular solutions for working with json support the inclusion of comments in the parser, I mean jackson and gson.

As I understand from the project, this is an attempt to speed up the work of these libraries at the expense of the micronaut code generator. If so, then it is quite logical to support all the functionality of these libraries so that it is really possible to connect micronaut-serde.

So, if I were you, I would not focus on specifications or other bureaucratic inventions, but rather look at what is actually used by real programmers

My opinion is that you should create not on the basis of rules invented by someone, but on the basis of logic, common sense and efficiency. And if the rules get in your way, then your task is to expand the rules.

Programs and computers work based on rules. So who are we - programs or real people?

@graemerocher
Copy link
Contributor

@altro3 did you try @dstepanov's recommendation of setting the Jackson feature to allow comments ?https://fasterxml.github.io/jackson-core/javadoc/2.8/com/fasterxml/jackson/core/JsonParser.Feature.html#ALLOW_COMMENTS

@altro3
Copy link
Contributor Author

altro3 commented Feb 1, 2024

I'll check it out in a few days. There's no time for that right now.

@graemerocher
Copy link
Contributor

I see, would be good to know because if it works without changes I don't see why anything needs to be done. It doesn't make sense to enable by default parser options that add overhead or are used in niche cases. As long as they are configurable and can be enabled by the user I see no issue here.

@altro3
Copy link
Contributor Author

altro3 commented Feb 3, 2024

@dstepanov @graemerocher I checked it out. It turned out to be an interesting situation. Yes, the comment support works as it should. Thanks!

but!

Now I can't turn off support for comments in any way and they are always parsed, ignoring the settings:

micronaut:
  serde:
    jackson:
      parser-features:
        ALLOW_COMMENTS: false
      read-features:
        ALLOW_JAVA_COMMENTS: false

You can reproduce it here: https://github.com/altro3/micronaut3-bug

Just send reqesut like this:
изображение

@altro3
Copy link
Contributor Author

altro3 commented Feb 3, 2024

@dstepanov There's another problem: you've added the ability to clone a mapper - that's fine. but! It's funny, but now it's not possible to set the "allow comments" setting, and this setting is very important, because it's most convenient to create a default mapper with comments turned off, but for configs use a cloned mapper with comments enabled in JSON

@dstepanov
Copy link
Contributor

It wasn’t me who added it :) Any idea why did that break it?

@altro3
Copy link
Contributor Author

altro3 commented Feb 4, 2024

@graemerocher @dstepanov I just don't understand the logic of how this feature works in the mikronaut series. It says so in jackson:

Feature that determines whether parser will allow use of Java/C/C++ style comments (both '/'+'*' and '//' varieties) within 
parsed content or not.
Since JSON specification does not mention comments as legal construct, this is a non-standard feature; however, in the wild
 this is extensively used. As such, feature is disabled by default for parsers and must be explicitly enabled.

In general, it is not clear to me whether enabling or disabling this feature affects performance. But it is clear that this feature should be turned off for the mapper, which is used for data exchange
I also understand that it should be possible to enable this feature exclusively for local use or special cases.

And now it turns out that comments are ALWAYS allowed and there is no way to disable them.

I repeat: it is not clear to me how critical this is and how it affects performance. But from my point of view, it should be possible to disable support for comments and throw an error in this case if json with comments comes

@dstepanov
Copy link
Contributor

@altro3 What do you think can be done here? The comment feature is part of the Jackson; according to the code, it should be disabled by default. Is there anything else we can do?

@altro3
Copy link
Contributor Author

altro3 commented Feb 19, 2024

@dstepanov So that's the problem. This feature is ALWAYS on at the moment! there is no way to disable parsing of json's with comments inside.

It looks like a configuration mapping error. my serde settings:

micronaut:
  serde:
    jackson:
      parser-features:
        ALLOW_COMMENTS: false
      read-features:
        ALLOW_JAVA_COMMENTS: false

изображение

So, now comments ALLOWED always. And can't disable this feature and it enabled by default. This is a bug, I believe.

Sample app to reproduce thi buug: https://github.com/altro3/micronaut3-bug

@dstepanov
Copy link
Contributor

Your example fails with:

{
  "_links": {
    "self": [
      {
        "href": "/test",
        "templated": false
      }
    ]
  },
  "_embedded": {
    "errors": [
      {
        "message": "Invalid JSON: Unexpected character ('/' (code 47)): maybe a (non-standard) comment? (not recognized as one since Feature 'ALLOW_COMMENTS' not enabled for parser)\n at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 2, column: 4]"
      }
    ]
  },
  "message": "Invalid JSON"
}

Also fails without any extra parser configuration.

@altro3
Copy link
Contributor Author

altro3 commented Feb 19, 2024

Omg... I'm sorry, I didn't know this was a postman feature. It turns out that postman allows you to write comments and removes them himself before sending a request.

It turns out that one problem remains: when copying jsonMapper, it is not possible to enable/disable comment support.

Sample code

        var configMapper = objectMapper.cloneWithConfiguration(
                new DeserializationConfiguration()
                new SerializationConfiguration()
        );

Now DeserializationConfiguration is very poor:
изображение

How can I create normal objectMapper without comments support and copy it with comments support for different goals?

@dstepanov
Copy link
Contributor

We should introduce interface JacksonObjectMapper extends ObjectMapper with some extra clone method that supports SerdeJacksonConfiguration.

@altro3
Copy link
Contributor Author

altro3 commented Feb 19, 2024

Yes, this feature is clearly missing now :-(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: awaiting feedback status: under consideration The issue is being considered, but has not been accepted yet
Projects
None yet
4 participants