Showing posts with label gson. Show all posts
Showing posts with label gson. Show all posts

Tuesday, 14 December 2021

Gson: Convert json string to Pretty print json string

Below snippet convert the json to Pretty json string.

String uglyJson = "{\"name\": \"Krishna\", \"age\": 31}";

Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonElement jsonElement = JsonParser.parseString(uglyJson);
String prettyJsonString = gson.toJson(jsonElement);

 

Find the below working application.

 

PrettyJson.java

package com.sample.app.json;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

public class PrettyJson {

	public static void main(String args[]) {
		String uglyJson = "{\"name\": \"Krishna\", \"age\": 31}";

		Gson gson = new GsonBuilder().setPrettyPrinting().create();
		JsonElement jsonElement = JsonParser.parseString(uglyJson);
		String prettyJsonString = gson.toJson(jsonElement);

		System.out.println(prettyJsonString);
	}

}

 

Output

{
  "name": "Krishna",
  "age": 31
}

 

 

 

Previous                                                    Next                                                    Home

Monday, 13 December 2021

Gson: Disable HTML escaping

In this post, I am going to explain how to disable HTML escaping in Gson library. By default, Gson escapes HTML characters such as <, >.

 

For example,

 

Model.java

package com.sample.app.json;

public class Model {

	private String data;

	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}

}

 

JsonEscapingDemo.java

package com.sample.app.json;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class JsonEscapingDemo {

	public static void main(String args[]) {
		Gson gson = new GsonBuilder().create();

		Model obj = new Model();
		obj.setData("Hello this is <Krishna>");

		String json = gson.toJson(obj);
		System.out.println(json);
	}

}

 

Output

{"data":"Hello this is \u003cKrishna\u003e"}

 

As you see above snippet, HTML characters < and > are escaped with unicodes \u003c, \u003e.

 

How can I disable HTML escaping?

Disable HTML escaping using disableHtmlEscaping() method.

 

Example

 

Gson gson = new GsonBuilder().disableHtmlEscaping().create();

 

JsonEscapingDemo.java

package com.sample.app.json;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class JsonEscapingDemo {

	public static void main(String args[]) {
		Gson gson = new GsonBuilder().disableHtmlEscaping().create();

		Model obj = new Model();
		obj.setData("Hello this is <Krishna>");

		String json = gson.toJson(obj);
		System.out.println(json);
	}

}

 

Output

{"data":"Hello this is <Krishna>"}

 

 

 

 

 

 

 

 

 

Previous                                                    Next                                                    Home

Sunday, 19 April 2020

Gson: Convert json to a typed array list

Using TypeToken, you can convert a json to typed array list.

Example
Type type = new TypeToken<List<Employee>>() {}.getType();
List<Employee> deserializedEmps = gson.fromJson(json, type);

Employee.java
package com.sample.app.model;

public class Employee {
 private int id;

 private String firstName;
 private String lastName;

 public Employee() {

 }

 public Employee(int id, String firstName, String lastName) {
  super();
  this.id = id;
  this.firstName = firstName;
  this.lastName = lastName;
 }

 public int getId() {
  return id;
 }

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

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 @Override
 public String toString() {
  return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
 }

}

App.java
package com.sample.app;

import java.util.Arrays;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.sample.app.model.Employee;
import java.lang.reflect.Type;
import com.google.gson.reflect.TypeToken;

public class App {

 public static void main(String args[]) throws ClassNotFoundException {
  Employee emp1 = new Employee(1, "Ravindra", "Gidugu");
  Employee emp2 = new Employee(2, "Rahim", "Khan");
  Employee emp3 = new Employee(3, "Joel", "Chelli");
  Employee emp4 = new Employee(4, "Ram", "Gurram");

  List<Employee> emps = Arrays.asList(emp1, emp2, emp3, emp4);

  Gson gson = new GsonBuilder().setPrettyPrinting().create();

  String json = gson.toJson(emps);

  Type type = new TypeToken<List<Employee>>() {}.getType();

  List<Employee> deserializedEmps = gson.fromJson(json, type);

  for (Employee emp : deserializedEmps) {
   System.out.println(emp);
  }
 }
}

Output
Employee [id=1, firstName=Ravindra, lastName=Gidugu]
Employee [id=2, firstName=Rahim, lastName=Khan]
Employee [id=3, firstName=Joel, lastName=Chelli]
Employee [id=4, firstName=Ram, lastName=Gurram]



Previous                                                    Next                                                    Home

Sunday, 5 January 2020

Gson: Convert HashMap to json string

Following snippet converts HashMap to json object.

Gson gson = new Gson();
Map<String,String> countriesAndCapitals = new HashMap<> ();
String json = gson.toJson(countriesAndCapitals);

App.java
package com.sample.app;

import java.util.HashMap;
import java.util.Map;

import com.google.gson.Gson;

public class App {

 public static void main(String args[]) {
  Gson gson = new Gson();

  Map<String, String> countriesAndCapitals = new HashMap<>();

  countriesAndCapitals.put("India", "Delhi");
  countriesAndCapitals.put("Nepal", "Katmandu");
  countriesAndCapitals.put("Canada", "Ottawa");

  String json = gson.toJson(countriesAndCapitals);

  System.out.println(json);
 }

}

Output
{"Canada":"Ottawa","Nepal":"Katmandu","India":"Delhi"}


Previous                                                    Next                                                    Home