Jackson Renames Primitive Boolean Field by Removing 'Is'

Jackson renames primitive boolean field by removing 'is'

This is a slightly late answer, but may be useful for anyone else coming to this page.

A simple solution to changing the name that Jackson will use for when serializing to JSON is to use the @JsonProperty annotation, so your example would become:

public class MyResponse implements Serializable {

private boolean isSuccess;

@JsonProperty(value="isSuccess")
public boolean isSuccess() {
return isSuccess;
}

public void setSuccess(boolean isSuccess) {
this.isSuccess = isSuccess;
}
}

This would then be serialised to JSON as {"isSuccess":true}, but has the advantage of not having to modify your getter method name.

Note that in this case you could also write the annotation as @JsonProperty("isSuccess") as it only has the single value element

Jackson annotation unable to parse boolean field

The problem is you are mixing two different libraries jackson and Gson, JsonProperty, JsonIgnoreProperties belongs to jackson and you are trying parse using Gson in test case

Using Gson : You can use @SerializedName while using Gson, you can find more information here about Gson

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class A {
public static final Type UNMARSHALLER_TYPE = new TypeToken<A>(){}.getType();

@SerializedName("id")
String id;

@SerializedName("name")
String name;

@SerializedName("is_enabled")
boolean isEnabled;
}

Using Jackson : you can use ObjectMapper, You can find more information here about jackson

 @Test
public void testAUnmarshaller() throws IOException {
// given
ObjectMapper objectMapper = new ObjectMapper();
PapiSite payload = objectMapper.readValue(RESPONSE_JSON, A.class);

// then
assertNotNull(payload);
assertTrue(payload.isEnabled());
assertEquals("XXX", payload.getName());
}

@JsonProperty seems to only work for non boolean members

Summarising the statements from comments: if you change your boolean field name to enabled

@JsonProperty("custom_name")
private boolean enabled;

public boolean isEnabled() {
return enabled;
}

public void setEnabled(boolean isEnabled) {
this.enabled = isEnabled;
}

it will give desired output { "custom_name" : true }

The original problem is not following java field naming conventions:

@JsonProperty("custom_name")
@Column(name = "fl_enabled")
private boolean isEnabled;

public boolean isEnabled() {
return isEnabled;
}

public void setEnabled(boolean isEnabled) {
this.isEnabled = isEnabled;
}

When FasterXML serializes an object, it looks for fields and getters.

In your case, it found boolean field isEnabled and serialized it with custom_name.

Then it excluded getter of this boolean field from processing - possible getter names to exclude are getIsEnabled and 'isIsEnabled' - you don't have any.

Then it finds method isEnabled that looks like a getter for field enabled. So it takes its value and serializes it with key enabled.

So again, the problem is you should not name boolean fields with prefix is.

Boolean field in entity changes name in JSON

Seems like you may have hit upon a bug if isBitwise works well but not isHiddenOnQuote.

Also, noticed that you are not using the @Getter etc annotations from Lombok. Perhaps you can try using the following annotations to force the correct name:

@get:JsonProperty("isHiddenOnQuote")

@param:JsonProperty("isHiddenOnQuote")

I got this from this answer on Stackoverflow:
https://stackoverflow.com/a/55100741/4402505

EDIT: Fixed the property name.

IntelliJ / Lombok messing around with is prefix and removing it

The correct Java Bean naming convention for your getter method would be isIsAuthenticated. Jackson tries to use the Java bean convention, and in this case, your class doesn't conform to it. You can try adding the @JsonProperty(value = "isAuthenticated") annotation on the isAuthenticated method directly. Additionally, to prevent any duplicate fields in the JSON, you could then add @JsonIgnore on the isAuthenticated field, if you don't need to serialize that field from JSON.

Small explanation why you have 2 fields in JSON now:

The correct property name, that Jackson derives from your isAuthenticated() method is authenticated. The property name is derived such that the getter prefix (which is is for boolean methods) is removed and the following letter converted to lower case). That's why you see the JSON property authenticated.

Additionally Jackson sees your isAuthenticated field, and creates a JSON property for it as well, which is then named isAuthenticated. By adding the @JsonProperty annotation on the field itself, nothing changes for Jackson, because for Jackson, the isAuthenticated() method has no relation at all to the field and setter method and thus they are treated as two different JSON properties.

Jackson's ObjectMapper().writeValueAsString() ignores variables that begin with is_

Jackson apparently scans the class that you pass in for getters and setters
and then use those methods for serialization and deserialization.

"Get", "Set", "is" are eliminated and what remains in those methods will be used as Json field.

Hence your "is_something" is changed into "_something" and so on.

Handling boolean values and empty strings for a property in Jackson?

I would suggest configure object mapper with this feature ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, so in case of empty string it will be assigned to null

ObjectMapper mapper = new ObjectMapper()
.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);

And you can happily declare this field as Boolean type, be aware in case of empty string this field value will be null

@JsonProperty("observed")
private Boolean observedValue;


Related Topics



Leave a reply



Submit