Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mixin Issue #1380

Closed
mnadeem opened this issue Sep 21, 2016 · 8 comments
Closed

Mixin Issue #1380

mnadeem opened this issue Sep 21, 2016 · 8 comments

Comments

@mnadeem
Copy link

mnadeem commented Sep 21, 2016

User Class

 public class User {
private Integer userId;
private String password;

public User(Integer userId, String password) {
    this.userId = userId;
    this.password = password;
}

public Integer getUserId() {
    return userId;
}

public String getPassword() {
    return password;
}

@Override
public String toString() {
    return "User [userId=" + userId + ", password=" + password + "]";
}
}

Corresponding mixing

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public abstract class UserMixin {


@JsonCreator
public UserMixin(
        @JsonProperty("userId") Integer userId
        , @JsonProperty("password") String password) {
    System.out.println("mixed-in constructor.");
}

}

TEST class

import java.io.IOException;

import com.fasterxml.jackson.databind.ObjectMapper;

 public class JacksonMixIn {
public static void main(String[] args) throws IOException {


    User user = new User(1,  "password");

    ObjectMapper mapper = new ObjectMapper();
    final String json = mapper.writeValueAsString(user);
    System.out.println(json);

    ObjectMapper mapper2 = new ObjectMapper();
    mapper2.addMixIn(User.class, UserMixin.class);


    System.out.println(mapper2.writeValueAsString(user));


    final User deserializedUser = mapper2.readValue(json, User.class);
    System.out.println(deserializedUser);
}

}

If I run the class everything as expected

{"userId":1,"password":"password"}
{"userId":1,"password":"password"}
User [userId=1, password=password]

Removed the following two getters from User class

public Integer getUserId() {
    return userId;
}

public String getPassword() {
    return password;
}

Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class com.github.dexecutor.redisson.User and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) )
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:269)
at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:68)
at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:32)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:130)
at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:3631)
at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:2998)
at com.github.dexecutor.redisson.JacksonMixIn.main(JacksonMixIn.java:14)

How do I marshal/un marshall objects in this case??

Note: I cant add the getter: it would be 3rd party class

@cowtowncoder
Copy link
Member

cowtowncoder commented Sep 21, 2016

As per exception, you may disable the features (SerializationFeature.FAIL_ON_EMPTY_BEANS), to get an empty JSON Object (since there are no getters).

But it depends: would you want to get some properties serialized, or is empty Object fine?

@mnadeem
Copy link
Author

mnadeem commented Sep 21, 2016

Is there any way (With the help of mixing or some other way), can I serilize/deserialize User object with out modifying(i.e. Adding getter/setter) it

@cowtowncoder
Copy link
Member

@mnadeem asking same question again without more information is not going to get you an answer.
As far as I can see:

  1. Deserialization works just fine, unless I am missing something.
  2. Serialization would succeed if you did WHAT THE EXCEPTION SUGGEST YOU TO DO. You did read the message did you?

So what is your remaining problem?

@mnadeem
Copy link
Author

mnadeem commented Sep 22, 2016

Well my problem (As I stated earlier) is I have got 3rd party library, and for some reason those have to be marshalled/marshalled into json, and I cannot provided getters only for the sake of marshalling/unmarshalling. I was expecting the framework to be smart enough (Use reflection, what not).

If the framework cannot provide such basic thing, that means, (as I understand) is not the write one for the use case (And I have to escalate it, not to be used any further)

Well I don't have to go any further (I got the answer), I understand, why the framework is like this, partly because people like @cowtowncoder is maintaining it, smart enough in giving childish answer

Dude, I read that, I have seen it, and that is the reason I have made that one, to appear in bold, not sure if you have seen it.

I feel there should be a guide line to respond to issues as well.

@cowtowncoder
Copy link
Member

@mnadeem You do not make any sense whatever. So your not asking any more questions without adequate information is a big win for me. Feel free to use some other library and pester its maintainers.

@mnadeem
Copy link
Author

mnadeem commented Sep 22, 2016

For those of you interested in solution (and don't care about use case), use the following code, and things would work like a charm

   ObjectMapper mapper = new ObjectMapper();
   mapper.setVisibility(mapper.getSerializationConfig().getDefaultVisibilityChecker().withFieldVisibility(JsonAutoDetect.Visibility.ANY).withGetterVisibility(JsonAutoDetect.Visibility.NONE)
            .withSetterVisibility(JsonAutoDetect.Visibility.NONE)
            .withCreatorVisibility(JsonAutoDetect.Visibility.NONE));

Above code wont force you to have public getter of private field.

@cowtowncoder
Copy link
Member

@mnadeem thank you for sharing the solution for anyone else looking.

For sake of completeness, there is also @JsonAutoDetect, applicable to classes directly and via mix-ins.

@mnadeem
Copy link
Author

mnadeem commented Sep 23, 2016

I have documented about this in this blog

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants