কম্পিউটার টিউটোরিয়াল

জাভাতে Jackson ব্যবহার করে সিরিয়ালাইজেশন время একটি ক্লাস বাদ দেওয়া (ignore) কীভাবে করবেন?

Jackson @JsonIgnoreType অ্যানোটেশন একটি ক্লাস কে সিরিয়ালাইজেশন প্রক্রিয়ার সময় বাদ দেওয়ার জন্য ব্যবহৃত হয় এবং এটি একটি ক্লাসের সকল প্রপার্টি বা ফিল্ড কে চিহ্নিত করে যা JSON অবজেক্ট সিরিয়ালাইজ এবং ডিসিরিয়ালাইজ করার সময় বাদ দেওয়া হবে।

সিনট্যাক্স

@Target(value={ANNOTATION_TYPE,TYPE}) 
@Retention(value=RUNTIME) 
public @interface JsonIgnoreType

উদাহরণ

import com.fasterxml.jackson.annotation.*; 
import com.fasterxml.jackson.core.*; 
import com.fasterxml.jackson.databind.*; 
import java.io.*; 

public class JsonIgnoreTypeTest { 
    public static void main(String args[]) throws IOException { 
        Employee emp = new Employee(); 
        ObjectMapper mapper = new ObjectMapper(); 
        String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(emp); 
        System.out.println(jsonString); 
    } 
} 

// Employee ক্লাস
class Employee { 
    @JsonIgnoreType 
    public static class Address { 
        public String firstLine = null; 
        public String secondLine = null; 
        public String thirdLine = null; 
        
        @Override 
        public String toString() { 
            return "Address{" + 
                "firstLine='" + firstLine + '\'' + 
                ", secondLine='" + secondLine + '\'' + 
                ", thirdLine='" + thirdLine + '\'' + 
                '}'; 
        } 
    } 
    
    public long empId = 115; 
    public String empName = "Raja Ramesh"; 
    public Address empAddress = new Address(); 
    
    @Override 
    public String toString() { 
        return "Employee{" + 
            "empId=" + empId + 
            ", empName='" + empName + '\'' + 
            ", empAddress=" + empAddress + 
            '}'; 
    } 
}

আউটপুট

{
    "empId" : 115, 
    "empName" : "Raja Ramesh" 
}
  1. জাভা File ক্লাস ব্যবহার করার পদ্ধতি

  2. জাভা HashMap ক্লাসের ব্যবহার: একটি সম্পূর্ণ গাইড

  3. জাভাতে ডিফল্ট মেথড ব্যবহার করে ডায়ামন্ড সমস্যা সমাধান

  4. জাভাতে জ্যাকসন লাইব্রেরি ব্যবহার করে নাল এবং খালি ফিল্ড উপেক্ষা করার পদ্ধতি