@JsonValue টীকা পদ্ধতি স্তরে দরকারী। আমরা জাভা অবজেক্ট থেকে একটি JSON স্ট্রিং তৈরি করতে এই টীকাটি ব্যবহার করতে পারি। যদি আমরা একটি ক্রমিক বস্তু মুদ্রণ করতে চাই তাহলে toString()কে ওভাররাইড করুন পদ্ধতি কিন্তু @JsonValue টীকা ব্যবহার করে , আমরা একটি উপায় সংজ্ঞায়িত করতে পারি যেখানে জাভা অবজেক্টকে সিরিয়াল করা হয়।
সিনট্যাক্স
@Target(value={ANNOTATION_TYPE,METHOD,FIELD})
@Retention(value=RUNTIME)
public @interface JsonValue উদাহরণ
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
public class JsonValueAnnotationTest {
public static void main(String args[]) throws Exception {
ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.writeValueAsString(new Student());
System.out.println(jsonString);
}
}
// Student class
class Student {
@JsonProperty
private int studentId = 115;
@JsonProperty
private String studentName = "Sai Adithya";
@JsonValue
public String toJson() {
return this.studentName + "," + this.studentId + "," + this.toString();
}
@Override
public String toString() {
return "Student[" +
"studentId = " + studentId +
", studentName = " + studentName +
']';
}
} আউটপুট
"Sai Adithya,115,Student[studentId = 115, studentName = Sai Adithya]"