Tuesday 29 June 2021

Moshi: Pretty print json

There are couple of ways to get pretty print json string.

Approach 1: Using indent method of JsonAdapter.

String json = jsonAdapter.indent("  ").toJson(emp1);

 

Approach 2: Setting indentation to JsonWriter.

Buffer buffer = new Buffer();
JsonWriter jsonWriter = JsonWriter.of(buffer);
jsonWriter.setIndent("   ");

jsonAdapter.toJson(jsonWriter, emp1);

json = buffer.readUtf8();

 

Find the below working application.

 

Project.java

package com.sample.app.model;

public class Project {
	private String projectName;
	private String projectDescription;

	public Project(String projectName, String projectDescription) {
		super();
		this.projectName = projectName;
		this.projectDescription = projectDescription;
	}

	public String getProjectName() {
		return projectName;
	}

	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}

	public String getProjectDescription() {
		return projectDescription;
	}

	public void setProjectDescription(String projectDescription) {
		this.projectDescription = projectDescription;
	}

}

 

Employee.java

package com.sample.app.model;

import java.util.List;

import com.squareup.moshi.Json;

public class Employee {
	@Json(name = "emp_id")
	private Integer id;

	@Json(name = "emp_name")
	private String name;

	@Json(name = "emp_hobbies")
	private List<String> hobbies;

	private Project project;

	public Employee(Integer id, String name, List<String> hobbies, Project project) {
		this.id = id;
		this.name = name;
		this.hobbies = hobbies;
		this.project = project;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<String> getHobbies() {
		return hobbies;
	}

	public void setHobbies(List<String> hobbies) {
		this.hobbies = hobbies;
	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

}

ObjectToJsonString.java

package com.sample.app;

import java.io.IOException;
import java.util.Arrays;

import com.sample.app.model.Employee;
import com.sample.app.model.Project;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.JsonWriter;
import com.squareup.moshi.Moshi;

import okio.Buffer;

public class ObjectToJsonString {
	public static void main(String args[]) throws IOException {
		Project project = new Project("CabinPCS", "Cabin pressure control system");

		Employee emp1 = new Employee(1, "Dhatri Sure", Arrays.asList("Trekking", "singing"), project);

		Moshi moshi = new Moshi.Builder().build();
		JsonAdapter<Employee> jsonAdapter = moshi.adapter(Employee.class);

		// Approach 1
		System.out.println("Using aproach 1");
		String json = jsonAdapter.indent("  ").toJson(emp1);
		System.out.println(json);

		// Approach 2
		System.out.println("\n\nUsing aproach 2");
		Buffer buffer = new Buffer();
		JsonWriter jsonWriter = JsonWriter.of(buffer);
		jsonWriter.setIndent("   ");

		jsonAdapter.toJson(jsonWriter, emp1);

		json = buffer.readUtf8();
		System.out.println(json);

	}
}


Output

Using aproach 1
{
  "emp_hobbies": [
    "Trekking",
    "singing"
  ],
  "emp_id": 1,
  "emp_name": "Dhatri Sure",
  "project": {
    "projectDescription": "Cabin pressure control system",
    "projectName": "CabinPCS"
  }
}


Using aproach 2
{
   "emp_hobbies": [
      "Trekking",
      "singing"
   ],
   "emp_id": 1,
   "emp_name": "Dhatri Sure",
   "project": {
      "projectDescription": "Cabin pressure control system",
      "projectName": "CabinPCS"
   }
}






 

 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment