Last active
August 29, 2015 14:09
-
-
Save bluerogue/519000113dc08311da61 to your computer and use it in GitHub Desktop.
Json or toString without nulls - using reflection to omit null object fields from toString or toJson
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (C) 2014 Matthew Roberts | |
* MIT License, http://opensource.org/licenses/MIT | |
*/ | |
package org.matthewroberts; | |
import java.io.Serializable; | |
import java.lang.reflect.Field; | |
/** | |
* | |
* This is an example of using reflection to build a JSON-formatted String | |
* at runtime which omits any null values. You can use something similar | |
* to override a toString method. I've used a JSON format as an example | |
* because there are many cases where you may not want to expose null | |
* values. There are many frameworks that handle this sort of thing for | |
* you, but in this case we're just using good old fashioned Java. | |
* | |
* @author mroberts | |
* | |
*/ | |
public class JsonNoNulls implements Serializable { | |
private static final long serialVersionUID = 1L; | |
private String firstName, lastName, email; | |
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; | |
} | |
public String getEmail() { | |
return email; | |
} | |
public void setEmail(String email) { | |
this.email = email; | |
} | |
/** | |
* Returns a Json-formatted String, omitting null values. | |
* | |
* @return returns a Json-formatted String | |
*/ | |
public String toJsonNotNull() { | |
Class<?> clazz = null; | |
StringBuilder stringBuilder = new StringBuilder(); | |
try { | |
clazz = Class.forName(this.getClass().getName()); | |
} catch (ClassNotFoundException e) { | |
e.printStackTrace(); | |
} | |
Field[] classFields = clazz.getDeclaredFields(); | |
stringBuilder.append("{"); | |
for (int i = 0; i < classFields.length; i++) { | |
Field field = classFields[i]; | |
try { | |
if (field.get(this) != null | |
&& !field.getName().equals("serialVersionUID")) { | |
stringBuilder.append("\"" + field.getName() + "\":\"" | |
+ field.get(this) + "\","); | |
} | |
} catch (IllegalArgumentException e) { | |
e.printStackTrace(); | |
} catch (IllegalAccessException e) { | |
e.printStackTrace(); | |
} | |
} | |
stringBuilder.setLength(stringBuilder.length() - 1); | |
stringBuilder.append("}"); | |
return stringBuilder.toString(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (C) 2014 Matthew Roberts | |
* MIT License, http://opensource.org/licenses/MIT | |
*/ | |
package org.matthewroberts; | |
import junit.framework.TestCase; | |
import org.junit.Test; | |
public class JsonNoNullsTest extends TestCase { | |
@Test | |
public void testObjectFieldsNotNull() { | |
TestObject testObj = new TestObject(); | |
testObj.setFirstName("Matthew"); | |
testObj.setLastName("Roberts"); | |
String testJsonNotNull = testObj.toJsonNotNull(); | |
assertTrue(testObj.getFirstName().equals("Matthew")); | |
assertTrue(testObj.getLastName().equals("Roberts")); | |
assertTrue(testObj.getEmail() == null); | |
assertTrue(testJsonNotNull.contains("firstName")); | |
assertTrue(testJsonNotNull.contains("lastName")); | |
assertTrue(!testJsonNotNull.contains("email")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment