[JAVA] [jackson] I want to receive JSON values "0" and "1" as boolean

Motivation

reference

Overview

Serialization and delicacy

Sample code

JSON you want to parse

{
    "name": "Sample item",
    "required": "1"
}

Data object

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class SampleItem {

  private String name;

  @JsonDeserialize(using=NumericBooleanDeserializer.class) //Deserializer class created below
  private boolean required;  //This can be boolean. If there is no corresponding value in JSON, it will be FALSE.
}

Deserializer

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;

/**
 *Settings for JSON string conversion (Boolean).
 * - 0 → false
 * - 1 → true
 */
public static class NumericBooleanDeserializer extends JsonDeserializer<Boolean> {
  @Override
  public Boolean deserialize(JsonParser parser, DeserializationContext context) throws IOException {
    return !"0".equals(parser.getText());
  }
}

Recommended Posts

[jackson] I want to receive JSON values "0" and "1" as boolean
I want to get some properties as JSON strings in Jackson!
I want to use Combine in UIKit as well.
Convert Java enum enums and JSON to and from Jackson
I want to transition screens with kotlin and java!
[Java] Convert JSON to Java and Java to JSON-How to use GSON and Jackson-
I want to convert characters ...
I want to bring Tomcat to the server and start the application
I want to make a list with kotlin and java!
I want to call a method and count the number
I want to make a function with kotlin and java!
Create API to send and receive Json data in Spring
I want to exchange JSON data (object) by Ajax between Java and JavaScript! ~ Spring edition ~
I want to implement various functions with kotlin and java!
I want to return multiple return values for the input argument
I want to give edit and delete permissions only to the poster
Webpack and webpacker I want to tell Ruby people right now
I want to be able to think and write regular expressions myself. ..
I want to return to the previous screen with kotlin and java!
I really want to do "new T ()"! (And without inspection exceptions)
I want to perform asynchronous processing and periodic execution with Rail !!!
Swift: I want to chain arrays
I want to use FormObject well
I want to convert InputStream to String
I want to docker-compose up Next.js!
[Ruby] Boolean values ​​and logical operators
Sample code to serialize and deserialize Java Enum enums and JSON in Jackson
I want to display images with REST Controller of Java and Spring!
[Active Admin] I want to customize the default create and update processing
[Ruby] I want to extract only the value of the hash and only the key
I want to pass the argument of Annotation and the argument of the calling method to aspect
I want to develop a web application!
I want to write a nice build.gradle
I want to make an ios.android app
I want to display background-ground-image on heroku.
I want to use DBViewer with Eclipse 2018-12! !!
I want to write a unit test!
I want to install PHP 7.2 on Ubuntu 20.04.
I want to stop Java updates altogether
I want to use @Autowired in Servlet
I want to target static fields to @Autowired
I want to do team development remotely
How to assemble JSON directly in Jackson
Acquisition of JSON data and rotation of values
Convert JSON to TSV and TSV to JSON with Ruby
I want to control the start / stop of servers and databases with Alexa
I want you to use Scala as Better Java for the time being
Rails6 I want to make an array of values with a check box
I want to recursively get the superclass and interface of a certain class
I tried to convert JavaBean and XML with Jackson formatter XML in this era
I want to find the MD5 checksum of a file in Java and get the result as a string in hexadecimal notation.