Skip to content

Instantly share code, notes, and snippets.

@pmlopes
Created May 1, 2014 08:00
Show Gist options
  • Save pmlopes/d7187315fe3f8f0cf140 to your computer and use it in GitHub Desktop.
Save pmlopes/d7187315fe3f8f0cf140 to your computer and use it in GitHub Desktop.
Suggested Json API for Vert.x 3.x
public class JsonArray extends ArrayList<Object> implements JsonElement<Integer>, List<Object> {
private static final long serialVersionUID = 1l;
public JsonArray() {
super();
}
public JsonArray(List<?> list) {
super(list);
}
public JsonArray(Object... elements) {
super();
if (elements != null) {
for (Object t : elements) {
add(t);
}
}
}
@Override
@SuppressWarnings("unchecked")
public <V> V getAt(Integer index) {
return (V) super.get(index);
}
@Override
public <V> JsonArray putAt(Integer key, V value) {
super.add(key, value);
return this;
}
@Override
public boolean isArray() {
return true;
}
@Override
public boolean isObject() {
return false;
}
@Override
public JsonObject asJsonObject() {
throw new ClassCastException();
}
@Override
public JsonArray asJsonArray() {
return this;
}
@Override
public boolean getBoolean(Integer index) {
Boolean value = (Boolean) super.get(index);
if (value == null) {
return false;
}
return value;
}
@Override
public Number getNumber(Integer index) {
Number value = (Number) super.get(index);
if (value == null) {
return null;
}
return value;
}
@Override
public String getString(Integer index) {
String value = (String) super.get(index);
if (value == null) {
return null;
}
return value;
}
@SuppressWarnings("unchecked")
@Override
public JsonArray getArray(Integer index) {
List<?> value = (List<?>) super.get(index);
if (value == null) {
return null;
}
if (value instanceof JsonArray) {
return (JsonArray) value;
}
return new JsonArray(value);
}
@SuppressWarnings("unchecked")
@Override
public JsonObject getObject(Integer index) {
Map<String, ?> value = (Map<String, ?>) super.get(index);
if (value == null) {
return null;
}
if (value instanceof JsonObject) {
return (JsonObject) value;
}
return new JsonObject(value);
}
@Override
public byte[] getBinary(Integer index) {
Object value = super.get(index);
if (value != null) {
if (value instanceof String) {
// expected Base64 string
return Base64.decode((String) value);
}
if (value instanceof byte[]) {
// this value was never encoded (just passed around in the heap)
return (byte[]) value;
}
throw new DecodeException("invalid format");
}
return null;
}
@Override
public Date getDate(Integer index) {
Object value = super.get(index);
if (value != null) {
if (value instanceof String) {
// expected ISO8601 UTC string
try {
// parse it...
} catch (ParseException e) {
throw new DecodeException(e.getMessage());
}
}
if (value instanceof Date) {
// this value was never encoded (just passed around in the heap) maybe bad idea...
return (Date) value;
}
throw new DecodeException("invalid format");
}
return null;
}
}
public interface JsonElement<K> {
<V> V getAt(K keyOrIndex);
<V> JsonElement<K> putAt(K keyOrIndex, V value);
boolean isArray();
boolean isObject();
JsonObject asJsonObject();
JsonArray asJsonArray();
// basic json types
boolean getBoolean(K keyOrIndex);
Number getNumber(K keyOrIndex);
String getString(K keyOrIndex);
JsonArray getArray(K keyOrIndex);
JsonObject getObject(K keyOrIndex);
// extension ???
byte[] getBinary(K keyOrIndex);
Date getDate(K keyOrIndex);
}
public class JsonObject extends HashMap<String, Object> implements JsonElement<String>, Map<String, Object> {
private static final long serialVersionUID = 1l;
public JsonObject() {
super();
}
public JsonObject(Map<String, ?> source) {
super(source);
}
@Override
@SuppressWarnings("unchecked")
public <V> V getAt(String key) {
return (V) super.get(key);
}
@Override
public <V> JsonObject putAt(String key, V value) {
super.put(key, value);
return this;
}
@Override
public boolean isArray() {
return false;
}
@Override
public boolean isObject() {
return true;
}
@Override
public JsonObject asJsonObject() {
return this;
}
@Override
public JsonArray asJsonArray() {
throw new ClassCastException();
}
@Override
public boolean getBoolean(String key) {
Boolean value = (Boolean) super.get(key);
if (value == null) {
return false;
}
return value;
}
@Override
public Number getNumber(String key) {
Number value = (Number) super.get(key);
if (value == null) {
return null;
}
return value;
}
@Override
public String getString(String key) {
String value = (String) super.get(key);
if (value == null) {
return null;
}
return value;
}
@Override
public JsonArray getArray(String key) {
List<?> value = (List<?>) super.get(key);
if (value == null) {
return null;
}
if (value instanceof JsonArray) {
return (JsonArray) value;
}
return new JsonArray(value);
}
@SuppressWarnings("unchecked")
@Override
public JsonObject getObject(String key) {
Map<String, ?> value = (Map<String, ?>) super.get(key);
if (value == null) {
return null;
}
if (value instanceof JsonObject) {
return (JsonObject) value;
}
return new JsonObject(value);
}
@Override
public byte[] getBinary(String key) {
Object value = super.get(key);
if (value != null) {
if (value instanceof String) {
// expected Base64 string
return Base64.decode((String) value);
}
if (value instanceof byte[]) {
// this value was never encoded (just passed around in the heap)
return (byte[]) value;
}
throw new DecodeException("invalid format");
}
return null;
}
@Override
public Date getDate(String key) {
Object value = super.get(key);
if (value != null) {
if (value instanceof String) {
// expected ISO8601 UTC string
try {
// parse it... and return...
} catch (ParseException e) {
throw new DecodeException(e.getMessage());
}
}
if (value instanceof Date) {
// this value was never encoded (just passed around in the heap), maybe bad idea...
return (Date) value;
}
throw new DecodeException("invalid format");
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment