Giter VIP home page Giter VIP logo

Comments (5)

elucash avatar elucash commented on May 26, 2024

I'm thinking about it all morning.

  1. 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.

  1. 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;
  1. Generating Jackson Modules. Problem with modules is that you need to register them and sometimes creation of ObjectMappers 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.

jdmwood avatar jdmwood commented on May 26, 2024

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.

elucash avatar elucash commented on May 26, 2024

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.

jdmwood avatar jdmwood commented on May 26, 2024

Fantastic! Thanks so much for this. I'm trying this out right now...:)

from immutables.

jdmwood avatar jdmwood commented on May 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.