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

জাভাতে @JsonAutoDetect অ্যানোটেশন কখন ব্যবহার করবেন?

@JsonAutoDetect অ্যানোটেশন ক্লাস লেভেলে ব্যবহার করা হয় একটি ক্লাসের প্রপার্টিগুলোর দৃশ্যমানতা (visibility) সিরিয়ালাইজেশন এবং ডিসিরিয়ালাইজেশন সময় ওভাররাইড করতে। আমরা দৃশ্যমানতা সেট করতে পারি প্রপার্টিগুলোর সাথে যেমন "creatorVisibility", "fieldVisibility", "getterVisibility", "setterVisibility" এবং "isGetterVisibility"। JsonAutoDetect ক্লাস পাবলিক স্ট্যাটিক কনস্ট্যান্ট সংজ্ঞায়িত করতে পারে যা জাভা ক্লাস দৃশ্যমানতা লেভেলের সমান যেমন "ANY", "DEFAULT", "NON_PRIVATE", "NONE", "PROTECTED_AND_PRIVATE" এবং "PUBLIC_ONLY"।

উদাহরণ

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

public class JsonAutoDetectTest { 
    public static void main(String[] args) throws IOException { 
        Address address = new Address("Madhapur", "Hyderabad", "Telangana"); 
        Name name = new Name("Raja", "Ramesh"); 
        Student student = new Student(address, name, true); 
        
        ObjectMapper mapper = new ObjectMapper(); 
        String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(student); 
        System.out.println("JSON: " + jsonString); 
    } 
} 

// Address ক্লাস
class Address { 
    private String firstLine; 
    private String secondLine; 
    private String thirdLine; 
    
    public Address(String firstLine, String secondLine, String thirdLine) { 
        this.firstLine = firstLine; 
        this.secondLine = secondLine; 
        this.thirdLine = thirdLine; 
    } 
    
    public String getFirstLine() { return firstLine; } 
    public String getSecondLine() { return secondLine; } 
    public String getThirdLine() { return thirdLine; } 
} 

// Name ক্লাস
class Name { 
    private String firstName; 
    private String secondName; 
    
    public Name(String firstName, String secondName) { 
        this.firstName = firstName; 
        this.secondName = secondName; 
    } 
    
    public String getFirstName() { return firstName; } 
    public String getSecondName() { return secondName; } 
} 

// Student ক্লাস
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY) 
class Student { 
    private Address address; 
    private Name name; 
    private Boolean isActive; 
    
    public Student(Address address, Name name, Boolean isActive) { 
        this.address = address; 
        this.name = name; 
        this.isActive = isActive; 
    } 
}

আউটপুট

{
    "address": { 
        "firstLine": "Madhapur", 
        "secondLine": "Hyderabad", 
        "thirdLine": "Telangana" 
    }, 
    "name": { 
        "firstName": "Raja", 
        "secondName": "Ramesh" 
    }, 
    "isActive": true 
}
  1. জাভাতে pack() মেথড কখন ব্যবহার করবেন

  2. জাভাতে String-এর বদলে StringBuffer বা StringBuilder কখন ব্যবহার করব

  3. জাভাতে String-এর বদলে StringBuffer কেন ব্যবহার করব

  4. পাইথনে %r এবং %s-এর মধ্যে পার্থক্য এবং কখন কী ব্যবহার করা উচিত