- Notifications
You must be signed in to change notification settings - Fork 463
Description
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.