Java Language Serialization Serialization with Jackson 2

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!


Following is an implementation that demonstrates how an object can be serialized into its corresponding JSON string.

class Test {

    private int idx;
    private String name;

    public int getIdx() {
        return idx;

    public void setIdx(int idx) {
        this.idx = idx;

    public String getName() {
        return name;

    public void setName(String name) { = name;


Test test = new Test();
ObjectMapper mapper = new ObjectMapper();

String jsonString;
try {
    jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(test);
} catch (JsonProcessingException ex) {
    // Handle Exception


  "idx" : 1,
  "name" : "abc"

You can omit the Default Pretty Printer if you don't need it.

The dependency used here is as follows:


Got any Java Language Question?