Last active
January 12, 2019 08:16
-
-
Save irace/4391255 to your computer and use it in GitHub Desktop.
Object-to-JSON string in Java
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
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
public class ObjectToJSON { | |
public static void main(String[] args) { | |
Map<Object, Object> birthday = new HashMap<Object, Object>(); | |
birthday.put("day", 12); | |
birthday.put("month", 8); | |
birthday.put("year", 1986); | |
List<String> languages = new ArrayList<String>(); | |
languages.add("Objective-C"); | |
languages.add("JavaScript"); | |
languages.add("Java"); | |
Map<Object, Object> object = new HashMap<Object, Object>(); | |
object.put("firstName", "Bryan"); | |
object.put("lastName", "Irace"); | |
object.put("birthday", birthday); | |
object.put("languages", languages); | |
object.put("worksAtTumblr", true); | |
object.put("null", null); | |
System.out.println(toJSON(object)); | |
} | |
private static String toJSON(Object object) { | |
if (object instanceof Map) { | |
Map map = (Map) object; | |
StringBuilder builder = new StringBuilder("{"); | |
boolean first = true; | |
for (Object key : map.keySet()) { | |
if (!first) | |
builder.append(","); | |
builder.append(String.format("\"%s\": %s", key, toJSON(map.get(key)))); | |
first = false; | |
} | |
builder.append("}"); | |
return builder.toString(); | |
} else if (object instanceof List) { | |
List list = (List) object; | |
StringBuilder builder = new StringBuilder("["); | |
for (int i = 0; i < list.size(); i++) { | |
if (i > 0) | |
builder.append(","); | |
builder.append(toJSON(list.get(i))); | |
} | |
builder.append("]"); | |
return builder.toString(); | |
} else if (object instanceof Number || object instanceof Boolean) { | |
return object.toString(); | |
} else if (object instanceof String) { | |
return String.format("\"%s\"", ((String) object).replaceAll("\"", "\\\"")); | |
} else if (object == null) { | |
return "null"; | |
} else { | |
throw new IllegalArgumentException("ORLY?"); | |
} | |
} | |
} |
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
import com.google.common.base.Function; | |
import com.google.common.base.Joiner; | |
import com.google.common.collect.*; | |
import java.util.List; | |
import java.util.Map; | |
public class ObjectToJSON { | |
public static void main(String[] args) { | |
System.out.println(toJSON(ImmutableMap.of( | |
"firstName", "Bryan", | |
"lastName", "Irace", | |
"birthday", ImmutableMap.of("day", 12, "month", 8, "year", 1986), | |
"languages", ImmutableList.of("Objective-C", "Java", "JavaScript"), | |
"worksAtTumblr", true))); | |
} | |
private static String toJSON(Object object) { | |
if (object instanceof Map) { | |
final Map map = (Map) object; | |
return String.format("{%s}", Joiner.on(",").join(Collections2.transform(map.keySet(), new Function() { | |
@Override | |
public Object apply(Object key) { | |
return String.format("\"%s\" : %s", key, toJSON(map.get(key))); | |
} | |
}))); | |
} else if (object instanceof List) { | |
return String.format("[%s]", Joiner.on(",").join(Lists.transform((List) object, new Function() { | |
@Override | |
public Object apply(Object o) { | |
return toJSON(o); | |
} | |
}))); | |
} else if (object instanceof Number || object instanceof Boolean) { | |
return object.toString(); | |
} else if (object instanceof String) { | |
return String.format("\"%s\"", ((String) object).replaceAll("\"", "\\\"")); | |
} else { | |
throw new IllegalArgumentException("ORLY?"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
good