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

Can JacksonFactory have methods to configure the parser? #923

Closed
suztomo opened this issue Dec 19, 2019 · 2 comments
Closed

Can JacksonFactory have methods to configure the parser? #923

suztomo opened this issue Dec 19, 2019 · 2 comments
Assignees
Labels
type: question Request for information or clarification. Not an issue.

Comments

@suztomo
Copy link
Member

suztomo commented Dec 19, 2019

Can com.google.api.client.json.jackson2.JacksonFactory have methods to configure the parser?

Is your feature request related to a problem? Please describe.

I'm always frustrated when I write escaped double quotes in Java String literal, especially when writing test cases for JSON.

        "{\"cumulative\":true, \"integer\":{\"highBits\":0, \"lowBits\":0}, "
            + "\"nameAndKind\":{\"kind\":\"SUM\", "
            + "\"name\":\"transformedValue-ElementCount\"}}"

Concrete example: suztomo/beam@74e9335#diff-d2f80ee068a253407b9347a92b0f49a5L119

As com.google.api.client.json.jackson2.JacksonFactory does not provide API to configure the parser, I had to implement my own JacksonFactory and JacksonParser for my POC above (NonstrictJacksonFactory and MyJacksonParser).

Describe the solution you'd like

In my test cases, I want to write JSON key without double-quotes and strings in JSON with single quotes. I wish JacksonFactory allows me to set the following options:

      this.factory.configure(ALLOW_UNQUOTED_FIELD_NAMES, true);
      this.factory.configure(ALLOW_SINGLE_QUOTES, true);

With these configuration, the example JSON text above can be written as:

        "{cumulative:true, integer:{highBits:0, lowBits:0}, "
            + "nameAndKind:{kind:'SUM', "
            + "name:'transformedValue-ElementCount'}}"

This is much easier to read compared to previous example of many escape characters.

Describe alternatives you've considered
Write test case with double-quotes escaped:

        "{\"cumulative\":true, \"integer\":{\"highBits\":0, \"lowBits\":0}, "
            + "\"nameAndKind\":{\"kind\":\"SUM\", "
            + "\"name\":\"transformedValue-ElementCount\"}}"

Write test case with ImmutableMap.of:

    ImmutableMap<String, Object> expected = ImmutableMap.of("cumulative", true,
        "integer", ImmutableMap.of("highBits", 0, "lowBits", 0L),
        "nameAndKind", ImmutableMap.of("kind", "SUM", "name", "transformedValue-ElementCount"));
    assertEquals(expected, result);

(Thanks @BenWhitehead !)

Additional context

I was trying to enhance Apache Beam's test cases that relies on toString() of objects.
suztomo/beam@314b74b . I came up with my assertEqualsOnJson method but still do not like the escaped double quotes in test assertions.

@yoshi-automation yoshi-automation added triage me I really want to be triaged. 🚨 This issue needs some love. labels Dec 20, 2019
@codyoss codyoss added type: question Request for information or clarification. Not an issue. and removed 🚨 This issue needs some love. triage me I really want to be triaged. labels Dec 26, 2019
@codyoss
Copy link
Member

codyoss commented Dec 26, 2019

Maybe wrapping JsonNode with some sort of builder pattern would work make this easier to write?

@suztomo
Copy link
Member Author

suztomo commented Dec 26, 2019

@codyoss Thank you for response. I ended up converting objects into JSON text and then asserts with JSONassert library. (Not the other way around)
https://github.com/apache/beam/pull/10441/files#diff-51b7cac6d770ee64a9f56f42c283fe49R41

@suztomo suztomo closed this as completed Dec 26, 2019
clundin25 pushed a commit to clundin25/google-http-java-client that referenced this issue Aug 11, 2022
…is#1440) (googleapis#923)

Source-Link: googleapis/synthtool@505ce5a
Post-Processor: gcr.io/cloud-devrel-public-resources/owlbot-java:latest@sha256:2567a120ce90fadb6201999b87d649d9f67459de28815ad239bce9ebfaa18a74
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

3 participants