Comments (5)
I'm thinking about it all morning.
- Strawman approach
@Value.Immutable
abstract class Value {
public abstract int a1();
public abstract boolean a2();
@JsonCreator
public static Value of(@JsonProperty("a1") int a1, @JsonProperty("a2") boolean a2) {
return ImmutableValue.builder().a1(a1).a2(a2).build();
}
}
Manual approach is definitely insufficient and there will be a problems with many attributes, with optional attributes and so on.
- Generate jackson serializers/deserializers
@Value.Immutable
@Json.JacksonSerializers
abstract class Value {
public abstract int a1();
public abstract boolean a2();
}
// then use generated serializers where the type is used or on abstract value type
@JsonSerialize(using=ValueJacksonSerializer.class)
@JsonDeserialize(using=ValueJacksonSerializer.class)
Value value;
- Generating Jackson Modules. Problem with modules is that you need to register them and sometimes creation of
ObjectMapper
s is out of direct control.
What do you think? I need some input, in particular, what is the main use case for ObjectMapper integration: serialize immutable object as part of other Jackson serialized object or to manually call ObjectMapper as shown in examples ?
from immutables.
I think the main use case would be the former: being able to use the beans with existing code which happens to use ObjectMapper.
I'm not sure I see the point of adding Jackson annotations to the abstract class: this would seem to defeat the purpose of this excellent library.
So actually I was thinking that you would make the immutable class the thing which supports ObjectMapper.
E.g.:
public final class ImmutableFoo
extends Foo {
@JsonCreator
public static Value of(@JsonProperty("a1") int a1, @JsonProperty("a2") boolean a2) {
return builder().a1(a1).a2(a2).build();
}
...
from immutables.
Just released version 1.0 with Jackson integration.
Thank you for reporting this! Also, @cowtowncoder was able to fix bug in Jackson discovered during work on this request.
Please see @Jackson.Mapped annotation. I guess, this is a simplest possible way to integrate for now.
Documentation now is more or less updated for the changes in 1.0. Please see http://immutables.org
from immutables.
Fantastic! Thanks so much for this. I'm trying this out right now...:)
from immutables.
This works perfectly for me! Thanks so much - I think this will really help me persuade my colleagues to replace our bloated code with Immutable value types instead!
from immutables.
Related Issues (20)
- allow builder method and protected constructor HOT 1
- allow Check to use private interface methods
- builder should return interface/abstract HOT 1
- Not passing through Bean Validation constraints on containers
- feature: constructor method that returns builder HOT 1
- Can I create a builder for an existing class that I don't control? HOT 1
- Custom Nullable annotations produce compilation errors HOT 1
- implementation type not visible when using @Criteria and ImplementationVisibility.PACKAGE
- @Value.Default
- How to reducing annotation clutter?
- Using strict builders gives Jackson deserialize failures
- Generated 'from' method has trouble with parameterized interfaces that declare properties HOT 2
- Issue with nested quotes in strings when using passAnnotations HOT 1
- Underriding toString will select invalid static method in preference to valid default method HOT 1
- cannot find annotations noise
- xor options HOT 2
- from method HOT 2
- "Sandwich" style immutables in combination with Jackson and Set-type properties leads to compile time warnings in generated code HOT 1
- passAnnotations patterns HOT 1
- Better Support for Jackson's @JsonSubTypes HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from immutables.