Posted In: GSON, JSON

Check two JSON Objects for equality – GSON

 

This example uses GSON library

 

<dependency>
	<groupId>com.google.code.gson</groupId>
	<artifactId>gson</artifactId>
	<version>2.8.0</version>
</dependency>

 

Examples using GSON

 

1. Success test with a JSON string

@Test
public void testJson3() {
	try {
		JsonParser parser = new JsonParser();
		JsonElement expected = parser.parse(expectedData3());
		JsonElement actual = parser.parse(actualData3());
		assertEquals(expected, actual);
	} catch (Exception e) {
		e.printStackTrace();
	}
}

private String expectedData3() {
	String json = "{" + "	\"portfolio\": [" 
		+ "		{"
        + "			\"stock\": \"Infosys\","
        + "			\"quantity\": \"100\","
        + "			\"buy\": \"1011\","
        + "			\"profit\": \"10000\"" 
        + "		},"
        + "		{"
        + "			\"stock\": \"State Bank of India\","
        + "			\"quantity\": \"1000\","
        + "			\"buy\": \"204\","
        + "			\"profit\": \"20000\"" 
        + "		},"
        + "		{"
        + "			\"stock\": \"Reliance Industries\","
        + "			\"quantity\": \"100\","
        + "			\"buy\": \"1200\","
        + "			\"profit\": \"12000\"" 
        + "		}"
        + "	]" + "}";
return json;
}

private String actualData3() {
	String json = "{" + "	\"portfolio\": [" 
			+ "		{"
	        + "			\"stock\": \"Infosys\","
	        + "			\"quantity\": \"100\","
	        + "			\"buy\": \"1011\","
	        + "			\"profit\": \"10000\"" 
	        + "		},"
	        + "		{"
	        + "			\"stock\": \"State Bank of India\","
	        + "			\"quantity\": \"1000\","
	        + "			\"buy\": \"204\","
	        + "			\"profit\": \"20000\"" 
	        + "		},"
	        + "		{"
	        + "			\"stock\": \"Reliance Industries\","
	        + "			\"quantity\": \"100\","
	        + "			\"buy\": \"1200\","
	        + "			\"profit\": \"12000\"" 
	        + "		}"
	        + "	]" + "}";
	return json;
}

 

2. Error test with a JSON string

@Test
public void testJson4() {
	try {
		JsonParser parser = new JsonParser();
		JsonElement expected = parser.parse(expectedData4());
		JsonElement actual = parser.parse(actualData4());
		assertEquals(expected, actual);
	} catch (Exception e) {
		e.printStackTrace();
	}
}

private String expectedData4() {
	String json = "{" + "	\"portfolio\": [" 
			+ "		{"
	        + "			\"stock\": \"Infosys\","
	        + "			\"quantity\": \"100\","
	        + "			\"buy\": \"1011\","
	        + "			\"profit\": \"10000\"" 
	        + "		},"
	        + "		{"
	        + "			\"stock\": \"State Bank of India\","
	        + "			\"quantity\": \"1000\","
	        + "			\"buy\": \"204\","
	        + "			\"profit\": \"20000\"" 
	        + "		},"
	        + "		{"
	        + "			\"stock\": \"Reliance Industries\","
	        + "			\"quantity\": \"100\","
	        + "			\"buy\": \"1200\","
	        + "			\"profit\": \"12000\"" 
	        + "		}"
	        + "	]" + "}";
	return json;
}

private String actualData4() {
	String json = "{" + "	\"portfolio\": [" 
			+ "		{"
	        + "			\"stock\": \"Infosys1\","
	        + "			\"quantity\": \"100\","
	        + "			\"buy\": \"1011\","
	        + "			\"profit\": \"10000\"" 
	        + "		},"
	        + "		{"
	        + "			\"stock\": \"State Bank of India\","
	        + "			\"quantity\": \"1000\","
	        + "			\"buy\": \"204\","
	        + "			\"profit\": \"200000\"" 
	        + "		},"
	        + "		{"
	        + "			\"stock\": \"Reliance Industries\","
	        + "			\"quantity\": \"1000\","
	        + "			\"buy\": \"1200\","
	        + "			\"profit\": \"12000\"" 
	        + "		}"
	        + "	]" + "}";
	return json;
}

 

3. Success test with a JSON string

This test covers How do I compare 2 json files having same key-values but in different order in java?

@Test
public void testJson5() {
	try {
		JsonParser parser = new JsonParser();
		JsonElement expected = parser.parse(expectedData5());
		JsonElement actual = parser.parse(actualData5());
		assertEquals(expected, actual);
	} catch (Exception e) {
		e.printStackTrace();
	}
}

private String expectedData5() {
	String json = "{" +
			"  \"portfolio1\":" + 
			"    {" +
			"      \"stock\": \"Infosys\"," +
			"      \"quantity\": \"100\"," +
			"      \"buy\": \"1011\"," +
			"      \"profit\": \"10000\"" +
			"    }," +
			"  \"portfolio2\":" + 
			"    {" +
			"      \"stock\": \"SBI\"," +
			"      \"quantity\": \"1000\"," +
			"      \"buy\": \"211\"," +
			"      \"profit\": \"20000\"" +
			"    }" +
			"}";
	return json;
}

private String actualData5() {
	String json = "{" +
			"  \"portfolio2\":" + 
			"    {" +
			"      \"stock\": \"SBI\"," +
			"      \"quantity\": \"1000\"," +
			"      \"buy\": \"211\"," +
			"      \"profit\": \"20000\"" +
			"    }," +
			"  \"portfolio1\":" + 
			"    {" +
			"      \"stock\": \"Infosys\"," +
			"      \"quantity\": \"100\"," +
			"      \"buy\": \"1011\"," +
			"      \"profit\": \"10000\"" +
			"    }" +
			"}";
		return json;
}

 

Tags: , ,

by , on February 24th, 2017

  • Categories