1

I am working on a project that has a fair amount of data objects that use a "fluent interface" or "method chaining" on their setters so all of the setters in each data object return this. I have looked around and found this question, but it is unclear to me whether this can be used with Yaml as the annotations specifically mention JSON, and also this seems to enable mapping to objects using an actual builder pattern, which is a little different. The project is currently using SnakeYaml, but that can be tossed away if some other lib like Jackson can do this.

  • Well did you try it? As long as the setters are properly named and the arguments properly typed, it shouldn't be a problem; SnakeYAML will just throw away the return value. However, if you have a separate builder object as in the linked question, you'd need to write a custom constructor for SnakeYAML. – flyx Mar 12 '20 at 08:54
  • Yes. I tried it and it did not work. If the return type of my setters is anything put void it throws an error about not being able to create the properties. – Cahlen Brancheau Mar 12 '20 at 12:28
  • Unfortunate. Then SnakeYAML can't help you. You could try creating a constructor annotated with Jackson's `@JsonCreator` which works with YAML. – flyx Mar 12 '20 at 13:28
  • I get that SnakeYaml can't do it. My question is about whether it is even possible to do with any library. Telling me to use a different library so that I can construct the data objects in a way that is entirely unrelated to my question is not helpful. – Cahlen Brancheau Mar 12 '20 at 14:05
  • Firstly, you mentioned Jackson yourself. Secondly, questions solely searching for a library [are off-topic here](https://stackoverflow.com/help/on-topic). Thirdly, if you reject suggestions, do it by explaining why that solution doesn't work for you instead of saying „I don't want to do it that way“. – flyx Mar 12 '20 at 14:25
  • I asked if it was possible with any lib. I want to know how to do this, if it can be done, and I don't care what lib is used. Your response amounted to "Why do that when you could do *not* that.", which is not only unhelpful, but does not answer the asked question. I am trying to minimally change our code and adding densely annotated constructors with up to 50 parameters is not an option. If it's not possible then it's not possible, but that is still my question. It is a very narrow question about java deserialization with a specific, non-standard, bean pattern. – Cahlen Brancheau Mar 12 '20 at 14:56
  • You're interpreting things in my comment I clearly didn't say. The link to Jackson indicated that you could live with changing the code (adding annotations) so adding a constructor seemed a viable solution. It is not an answer, true, which is why I wrote it as *comment*. To come back to the problem: Do the setters contain logic? If they only modify private fields, you could configure Jackson to access the private fields (via reflection) instead of using the setters. This would not require modification of the classes. – flyx Mar 12 '20 at 15:43

1 Answers1

0

It turns out that this is in fact possible. Jackson supports yaml and will work with builder style setters by default without any extra configuration.