logo

جاكسون تجاهل الحقول الفارغة

في هذا القسم، سوف نفهم كيف يمكننا تجاهل الحقول الفارغة في وقت إجراء تسلسل لفئة Java باستخدام Jackson.

يمكننا تجاهل الحقول الفارغة على مستوى الفصل أو مستوى الحقل أو عالميًا. دعونا نفهم كل واحد منهم واحدا تلو الآخر.

تجاهل الحقول الفارغة على مستوى الفصل الدراسي

لتجاهل الحقول الفارغة على مستوى الفصل، نستخدم @JsonInclude تعليق توضيحي مع include.NON_NULL . لنأخذ مثالاً لفهم كيف يمكننا استخدام التعليق التوضيحيJsonInclude لتجاهل الحقول الفارغة على مستوى الفصل الدراسي.

IgnoreNullFieldExample1.java

رحلة ولكن
 //import required classes and package if any import java.io.IOException; import java.util.Scanner; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; //create class IgnoreNullFieldExample1 to understand how we can ignore the null field on marshalling public class IgnoreNullFieldExample1 { //main() methods start public static void main(String args[]) { // create an instance of ObjectMapper class ObjectMapper mapper = new ObjectMapper(); // create Scanner class object Scanner sc= new Scanner(System.in); // using try-catch try { College colg; String name, university, street, state, city, rank; System.out.println('Enter College Name:'); name = sc.nextLine(); System.out.println('Enter University Name:'); university = sc.nextLine(); System.out.println('Enter Street:'); street = sc.nextLine(); System.out.println('Enter City:'); city = sc.nextLine(); System.out.println('Enter State:'); state = sc.nextLine(); System.out.println('Enter College Rank:'); rank = sc.nextLine(); // set values to College object by using constructor colg = new College(name, university, new Address(street, state, null), null); // serialize College using mapper String jsonString = mapper .writerWithDefaultPrettyPrinter() .writeValueAsString(colg); System.out.println(jsonString); } catch (IOException e) { e.printStackTrace(); } //close Scanner class object sc.close(); } } //create class College by using @JsonInclude(JsonInclude.Include.NON_NULL) @JsonInclude(JsonInclude.Include.NON_NULL) class College { //Creating properties of College class public String name; public String university; public Address address; public String rank; // constructor College(String name, String university, Address address, String rank){ this.name = name; this.university = university; this.address = address; this.rank = rank; } } //create class Address @JsonInclude(JsonInclude.Include.NON_NULL) class Address { public String street; public String state; public String city; Address(String street, String state, String city){ this.street = street; this.state = state; this.city = city; } } 

انتاج:

جاكسون تجاهل الحقول الفارغة

تجاهل حقل فارغ محدد

وهنا نستخدم @JsonInclude تعليق توضيحي مع include.NON_NULL على مستوى الحقل لتجاهل حقل فارغ محدد. لنأخذ مثالاً لفهم كيف يمكننا استخدام التعليق التوضيحي @JsonInclude لتجاهل حقل فارغ محدد.

IgnoreNullFieldExample2.java

 //import required classes and package if any import java.io.IOException; import java.util.Scanner; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; //create class IgnoreNullFieldExample2 to understand how we can ignore specific null fields on marshalling public class IgnoreNullFieldExample2 { //main() methods start public static void main(String args[]) { // create an instance of ObjectMapper class ObjectMapper mapper = new ObjectMapper(); // create Scanner class object Scanner sc= new Scanner(System.in); // using try-catch try { College colg; String name, university, street, state, city, rank; System.out.println('Enter College Name:'); name = sc.nextLine(); System.out.println('Enter University Name:'); university = sc.nextLine(); System.out.println('Enter Street:'); street = sc.nextLine(); System.out.println('Enter City:'); city = sc.nextLine(); System.out.println('Enter State:'); state = sc.nextLine(); System.out.println('Enter College Rank:'); rank = sc.nextLine(); // set values to College object by using constructor colg = new College(name, university, new Address(street, state, city), null); // serialize College using mapper String jsonString = mapper .writerWithDefaultPrettyPrinter() .writeValueAsString(colg); System.out.println(jsonString); } catch (IOException e) { e.printStackTrace(); } //close Scanner class object sc.close(); } } //create class College by using @JsonInclude(JsonInclude.Include.NON_NULL) class College { //Creating properties of College class public String name; public String university; public Address address; @JsonInclude(JsonInclude.Include.NON_NULL) public String rank; // constructor College(String name, String university, Address address, String rank){ this.name = name; this.university = university; this.address = address; this.rank = rank; } } //create class Address class Address { public String street; public String state; public String city; Address(String street, String state, String city){ this.street = street; this.state = state; this.city = city; } } 

انتاج:

إذا كان آخر في باش شل
جاكسون تجاهل الحقول الفارغة

تجاهل الحقول الفارغة على مستوى العالم

هنا، نستخدم JsonInclude.Include.NON_NULL في فئة ObjectMapper لتكوين سلوك تجاهل الحقول الفارغة عالميًا. نحن نستخدم ال مجموعةSerializationInclusion() طريقة مع تضمين.NON_NULL بالطريقة الآتية:

 mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL) 

لنأخذ مثالاً لفهم كيف يمكننا تجاهل الحقول الفارغة عالميًا باستخدام مجموعةSerializationInclusion() طريقة.

IgnoreNullFieldExample3.java

 //import required classes and package if any import java.io.IOException; import java.util.Scanner; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; //create class IgnoreNullFieldExample3 to understand how we can ignore null fields globally public class IgnoreNullFieldExample3 { //main() methods start public static void main(String args[]) { // create an instance of ObjectMapper class ObjectMapper mapper = new ObjectMapper(); // create Scanner class object Scanner sc= new Scanner(System.in); // using try-catch try { College colg; String name, university, street, state, city, rank; System.out.println('Enter College Name:'); name = sc.nextLine(); System.out.println('Enter University Name:'); university = sc.nextLine(); System.out.println('Enter Street:'); street = sc.nextLine(); System.out.println('Enter City:'); city = sc.nextLine(); System.out.println('Enter State:'); state = sc.nextLine(); System.out.println('Enter College Rank:'); rank = sc.nextLine(); // set values to College object by using constructor colg = new College(name, university, new Address(street, state, null), null); // serialize College using mapper String jsonString = mapper .setSerializationInclusion(JsonInclude.Include.NON_NULL) .writerWithDefaultPrettyPrinter() .writeValueAsString(colg); System.out.println(jsonString); } catch (IOException e) { e.printStackTrace(); } //close Scanner class object sc.close(); } } //create class College class College { //Creating properties of College class public String name; public String university; public Address address; public String rank; // constructor College(String name, String university, Address address, String rank){ this.name = name; this.university = university; this.address = address; this.rank = rank; } } //create class Address class Address { public String street; public String state; public String city; Address(String street, String state, String city){ this.street = street; this.state = state; this.city = city; } } 

انتاج:

جاكسون تجاهل الحقول الفارغة

يتم استخدام جميع الطرق المذكورة أعلاه لتجاهل الحقول الفارغة، حيث يتم استخدام كل طريقة في حالات مختلفة.