Closed azell closed 8 years ago
I guess the json
attribute could be an enum that controls naming. e.g. NONE, STANDARD, LOWERCASE
. But I worry about supporting a ton of different naming conventions. I think though that you can add the annotations manually. I'll do a test when I get a chance.
Great, and I am guessing that the JsonIgnore / JsonIgnoreProperties annotations would also be useful.
I'd appreciate a PR or at least a straw-man design
Fair enough, let me start reading the case class code.
I did some experimenting and it looks like Jackson will use @JsonProperty
annotations set in the parent interface. So, we could have an option to @CaseClass(json = true)
that sets all Jackson annotations except @JsonProperty
s so that users can override them. Or maybe, the annotation processor could just not set @JsonProperty
if the parent interface already has it.
I see a couple of phases:
Templates.addField
, check if the method already has a JsonProperty annotation attached to it. If so, either skip the annotation in the generated code, or copy it from the interface.For number 1, I need to check if all of the required metadata is passed to addField
. The metadata would include the class and method.
One thing I have been bitten by before is depending on key order in the generated JSON. Ex:
String json = new ObjectMapper().writeValueAsString(object);
Assert.assertEquals("{\"age\":32,\"type\":\"large\",\"nombre\":\"me\"}", json);
A way around this is:
ObjectMapper mapper = new ObjectMapper();
Assert.assertEquals(mapper.readTree("{\"age\":32,\"type\":\"large\",\"nombre\":\"me\"}"), mapper.valueToTree(object));
Lastly ObjectMappers are immutable and fairly expensive to create. Adding a private final ObjectMapper mapper = new ObjectMapper();
to the class could be useful.
I don't use a single ObjectMapper because it's just a test. FYI
I know, just my OCD / DRY kicking in.
I would like to define a case class such as:
Not sure how much halva wants to expose a dependency on Jackson, in terms of the actual annotation used by the interface.