Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8280051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:31:58+00:00 2026-06-08T09:31:58+00:00

I’m quite new to the Jackson library (version 1.9). I’m using it only since

  • 0

I’m quite new to the Jackson library (version 1.9). I’m using it only since a couple of weeks, and I find it very flexible and time-saving when it’s about serializing and deserializing objects in Java.

I’m experiencing troubles, though, into deserializing “flat” JSONs to a class which is a composition of another, when both are meant to be immutable.

My situation is pretty much the following:

class Foo {

    private final String var1;

    Foo(String var1) {
            this.var1 = var1;
    }
    // getters omitted
}

class A {
    private final Foo foo;
    private final String var2;

    A(/* @JsonUnwrapped doesn't work here */ Foo foo, String var2) {
            this.foo = foo;
            this.var2 = var2;
    }

    @JsonUnwrapped
    Foo getFoo() {
        return foo;
    }

    String getVar2() {
        return var2;
    }
}

class B extends Foo {
    private final String var2;

    B(String var1, String var2) {
            super(var1);
            this.var2 = var2;
    }
    // getters omitted
}

And the JSON to deserialize is something like this:

{ "var1" : "some_value", "var2" : "some_other_value" }

The question is: is there an annotation-based way (so, without the need of using a custom deserializer) to tell Jackson to compose the given JSON to a ‘A’ instance?
I’ve tried using the @JsonUnwrapped attribute for the Foo argument in class ‘A’ constructor, but it’s not supported in multi-argument constructor as it would need a JsonProperty to work (which doesn’t make sense, because there is actually no single property for those items).
Serialization, instead, works perfectly using this pattern.

It would also work with a non-immutable class by using separate setters, but I’d like to know if there’s a way to do the same by only using the constructors (or a builder, which would make sense as in reality the fields are much more than the one in the example).

The very same method obviously works with class ‘B’ which inherits from ‘Foo’.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T09:32:00+00:00Added an answer on June 8, 2026 at 9:32 am

    Note that Jackson’s deserialization processing doesn’t necessarily respect the immutability of final fields. So, a simple approach would be to just provide no-argument (private) constructors for Jackson to use.

    import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
    import com.fasterxml.jackson.annotation.JsonUnwrapped;
    import com.fasterxml.jackson.annotation.PropertyAccessor;
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    public class JacksonFoo
    {
      public static void main(String[] args) throws Exception
      {
        // {"var1":"some_value", "var2":"some_other_value"}
        String jsonInput = "{\"var1\":\"some_value\", \"var2\":\"some_other_value\"}";
    
        ObjectMapper mapper = new ObjectMapper().setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
    
        A a = new A(new Foo("some_value"), "some_other_value");
        System.out.println(mapper.writeValueAsString(a));
        // output: {"var1":"some_value","var2":"some_other_value"}
    
        A aCopy = mapper.readValue(jsonInput, A.class);
        System.out.println(mapper.writeValueAsString(aCopy));
        // output: {"var1":"some_value","var2":"some_other_value"}
      }
    }
    
    class Foo
    {
      private final String var1;
    
      Foo(String var1) {this.var1 = var1;}
    
      private Foo() {this.var1 = null;}
    }
    
    class A
    {
      @JsonUnwrapped
      private final Foo foo;
      private final String var2;
    
      A(Foo foo, String var2)
      {
        this.foo = foo;
        this.var2 = var2;
      }
    
      private A()
      {
        this.foo = null;
        this.var2 = null;
      }
    }
    

    If you really don’t want to provide such (extra) constructors, then it would be nice if a similar solution could be devised using @JsonCreator, but I wasn’t able to get such a thing to work. So, I recommend logging an enhancement request at https://github.com/FasterXML/jackson-core/issues, maybe to better support annotating a @JsonCreator argument with both @JsonUnwrapped and @JsonProperty.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.