search
HomeJavaCheck the type of Json property value when converted to Java class

php editor Youzi brings you the Java Q&A topic of this article: "Checking the type of Json attribute value when converting to a Java class". During the development process, we often need to convert JSON data into Java objects, but how to effectively check and handle the type of attribute values ​​​​is an important skill in this process. This article will answer this question for you and help you better understand and use JSON data conversion technology.

Question content

Suppose I have the following json data. Here I have an id of integer type. The type of id in the java class is string.

{
  "id": 1,
  "name": "user1"
}
@lombok.data
@allargsconstructor
@noargsconstructor
class data {
    string id;
    string name;
}

I want to convert json to the class while strictly checking whether the value of the json attribute is the same type as the class field.

String json = "{\"id\":1,\"name\":\"user1\"}";
ObjectMapper objectMapper = new ObjectMapper(new JsonFactory());

Data data = objectMapper.readValue(json, Data.class);
System.out.println(data);

data(id=1, name=user1)

I hope no conversion should happen here, but it will be converted.

Solution

One way I guess is to use a custom deserializer and do a simple type check:

public class customdeserializer extends stddeserializer<string> {

  protected customdeserializer() {
    this(null);
  }

  protected customdeserializer(class<?> vc) {
    super(vc);
  }

  @override
  public string deserialize(jsonparser p, deserializationcontext ctxt) throws ioexception, jacksonexception {
    if (p.currenttoken() != jsontoken.value_string) {
      throw new invalidformatexception(p, "expected string type", p.getvalueasstring(), string.class);
    }
    return p.getvalueasstring();
  }
}

Then use this deserializer in the field you want to check:

public class Data {
  @JsonDeserialize(using = CustomDeserializer.class)
  String id;
}

The above is the detailed content of Check the type of Json property value when converted to Java class. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:stackoverflow. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),