How to convert a bean to JSON object using exclude filter in Java?
You can use the JsonConfig class to configure the serialization process. We can use the setJsonPropertyFilter() method of JsonConfig to set the property filter when serializing to JSON. We need to implement a custom PropertyFilter class by overriding the apply() method of the PropertyFilter interface. Returns true if the attribute will be filtered out, false otherwise.
Syntax
public void setJsonPropertyFilter(PropertyFilter jsonPropertyFilter)
Example
import net.sf.json.JSONObject; import net.sf.json.JsonConfig; import net.sf.json.util.PropertyFilter; public class ConvertBeanToJsonExcludeFilterTest { public static void main(String[] args) { Student student = new Student("Sai", "Chaitanya", 20, "Hyderabad"); JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setJsonPropertyFilter(new CustomPropertyFilter()); JSONObject jsonObj = JSONObject.fromObject(student, jsonConfig); System.out.println(jsonObj.toString(3)); //pretty print JSON } public static class Student { private String firstName, lastName, address; public int age; public Student(String firstName, String lastName, int age, String address) { super(); this.firstName = firstName; this.lastName = lastName; this.age = age; this.address = address; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public int getAge() { return age; } public String getAddress() { return address; } } } // CustomPropertyFilter class class CustomPropertyFilter implements PropertyFilter { @Override public boolean apply(Object source, String name, Object value) { if(Number.class.isAssignableFrom(value.getClass()) || String.class.isAssignableFrom(value.getClass())) { return false; } return true; } }
Output
{ "firstName": "Sai", "lastName": "Chaitanya", "address": "Hyderabad", "age": 20 }
The above is the detailed content of How to convert a bean to JSON object using exclude filter in Java?. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

WebStorm Mac version
Useful JavaScript development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.