List of usage examples for com.google.gson JsonElement getAsJsonPrimitive
public JsonPrimitive getAsJsonPrimitive()
From source file:org.qcert.runtime.UnaryOperators.java
License:Apache License
public static JsonElement substring(int start, int end, JsonElement e) { String str = e.getAsJsonPrimitive().getAsString(); return new JsonPrimitive(str.substring(start, end)); }
From source file:org.qcert.runtime.UnaryOperators.java
License:Apache License
public static JsonElement substring(int start, JsonElement e) { String str = e.getAsJsonPrimitive().getAsString(); return new JsonPrimitive(str.substring(start)); }
From source file:org.qcert.util.SchemaUtil.java
License:Apache License
/** * Make a Type from a JsonElement representing a Type. * TODO support for embedded objects (other than collections) is incomplete. * @param type the JsonElement to be made into a Type * @param brandMap the map from type names to brands in the event of an embedded objectl * @return the Type/*from w w w.ja va 2 s .c om*/ */ private static Type makeType(JsonElement type) { if (type.isJsonPrimitive() && type.getAsJsonPrimitive().isString()) { return new PrimitiveType(type.getAsJsonPrimitive().getAsString()); } // Other than primitive types and object type references we only support the $coll convention at this time JsonElement coll = type.getAsJsonObject().get("$coll"); if (coll == null) throw new UnsupportedOperationException("Don't yet know how to handle encoded type " + type); Type element = makeType(coll); return new ListType(element); }
From source file:org.restexpress.plugin.gson.json.GsonTimepointSerializer.java
License:Apache License
@Override public Date deserialize(final JsonElement json, final Type typeOf, final JsonDeserializationContext context) throws JsonParseException { try {/* www. ja v a2s . c o m*/ return HttpDateTimeFormat.parseAny(json.getAsJsonPrimitive().getAsString()); } catch (final ParseException e) { throw new JsonParseException(e); } }
From source file:org.restexpress.serialization.json.GsonEncodingStringSerializer.java
License:Apache License
@Override public String deserialize(JsonElement json, Type typeOf, JsonDeserializationContext context) throws JsonParseException { return json.getAsJsonPrimitive().getAsString(); }
From source file:org.sickbeard.json.deserializer.BooleanDeserializer.java
License:Open Source License
@Override public Boolean deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { try {/*from ww w .jav a 2s . com*/ String value = arg0.getAsJsonPrimitive().getAsString(); if (value.toLowerCase().equals("true")) { return true; } else if (value.toLowerCase().equals("false")) { return false; } else { return Integer.valueOf(value) != 0; } } catch (ClassCastException e) { throw new JsonParseException("Cannot parse Boolean string '" + arg0.toString() + "'", e); } catch (Exception e) { throw new JsonParseException("Cannot parse Boolean string '" + arg0.toString() + "'", e); } }
From source file:org.structr.core.JsonInputGSONAdapter.java
License:Open Source License
@Override public JsonInput deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonInput wrapper = new JsonInput(); if (json.isJsonObject()) { JsonObject obj = json.getAsJsonObject(); for (Entry<String, JsonElement> entry : obj.entrySet()) { String key = entry.getKey(); JsonElement elem = entry.getValue(); // static mapping of IdProperty if present if ((idProperty != null) && "id".equals(key)) { key = idProperty.jsonName(); }/*from w w w. jav a2 s . com*/ if (elem.isJsonNull()) { wrapper.add(key, null); } else if (elem.isJsonObject()) { wrapper.add(key, deserialize(elem, typeOfT, context)); } else if (elem.isJsonArray()) { JsonArray array = elem.getAsJsonArray(); List list = new LinkedList(); for (JsonElement element : array) { if (element.isJsonPrimitive()) { list.add(fromPrimitive((element.getAsJsonPrimitive()))); } else if (element.isJsonObject()) { // create map of values list.add(deserialize(element, typeOfT, context)); } } wrapper.add(key, list); } else if (elem.isJsonPrimitive()) { // wrapper.add(key, elem.getAsString()); wrapper.add(key, fromPrimitive((JsonPrimitive) elem)); } } } else if (json.isJsonArray()) { JsonArray array = json.getAsJsonArray(); for (JsonElement elem : array) { if (elem.isJsonPrimitive()) { wrapper.add(elem.toString(), ((JsonPrimitive) elem).getAsString()); } else if (elem.isJsonObject()) { wrapper.add(elem.toString(), deserialize(elem, typeOfT, context)); } else if (elem.isJsonArray()) { wrapper.add(elem.toString(), deserialize(elem, typeOfT, context)); } } } return wrapper; }
From source file:org.structr.core.PropertySetGSONAdapter.java
License:Open Source License
private PropertySet deserializeFlatNameValue(JsonElement json, Type typeOfT, JsonDeserializationContext context) { PropertySet wrapper = new PropertySet(); if (json.isJsonObject()) { JsonObject obj = json.getAsJsonObject(); for (Entry<String, JsonElement> entry : obj.entrySet()) { String key = entry.getKey(); JsonElement elem = entry.getValue(); // static mapping of IdProperty if present if ((idProperty != null) && "id".equals(key)) { key = idProperty;// w ww. j a v a 2 s . c om } if (elem.isJsonNull()) { wrapper.add(key, null); } else if (elem.isJsonObject()) { wrapper.add(key, deserializeFlatNameValue(elem, typeOfT, context)); } else if (elem.isJsonArray()) { JsonArray array = elem.getAsJsonArray(); List list = new LinkedList(); for (JsonElement element : array) { if (element.isJsonPrimitive()) { list.add(fromPrimitive((element.getAsJsonPrimitive()))); } else if (element.isJsonObject()) { // create map of values list.add(deserializeFlatNameValue(element, typeOfT, context)); } } wrapper.add(key, list); } else if (elem.isJsonPrimitive()) { // wrapper.add(key, elem.getAsString()); wrapper.add(key, fromPrimitive((JsonPrimitive) elem)); } } } else if (json.isJsonArray()) { JsonArray array = json.getAsJsonArray(); for (JsonElement elem : array) { if (elem.isJsonPrimitive()) { wrapper.add(elem.toString(), ((JsonPrimitive) elem).getAsString()); } else if (elem.isJsonObject()) { wrapper.add(elem.toString(), deserializeFlatNameValue(elem, typeOfT, context)); } else if (elem.isJsonArray()) { wrapper.add(elem.toString(), deserializeFlatNameValue(elem, typeOfT, context)); } } } return wrapper; }
From source file:org.structr.core.rest.JsonInputGSONAdapter.java
License:Open Source License
public static JsonInput deserialize(final JsonElement json, final JsonDeserializationContext context) throws JsonParseException { final JsonInput wrapper = new JsonInput(); if (json.isJsonObject()) { final JsonObject obj = json.getAsJsonObject(); for (final Entry<String, JsonElement> entry : obj.entrySet()) { final String key = entry.getKey(); final JsonElement elem = entry.getValue(); if (elem.isJsonNull()) { wrapper.add(key, null);//from w w w . jav a2 s.co m } else if (elem.isJsonObject()) { wrapper.add(key, deserialize(elem, context)); } else if (elem.isJsonArray()) { final JsonArray array = elem.getAsJsonArray(); final List list = new LinkedList(); for (final JsonElement element : array) { if (element.isJsonPrimitive()) { list.add(fromPrimitive((element.getAsJsonPrimitive()))); } else if (element.isJsonObject()) { // create map of values list.add(deserialize(element, context)); } } wrapper.add(key, list); } else if (elem.isJsonPrimitive()) { // wrapper.add(key, elem.getAsString()); wrapper.add(key, fromPrimitive(elem.getAsJsonPrimitive())); } } } else if (json.isJsonArray()) { final JsonArray array = json.getAsJsonArray(); for (final JsonElement elem : array) { if (elem.isJsonPrimitive()) { wrapper.add(elem.toString(), fromPrimitive(elem.getAsJsonPrimitive())); } else if (elem.isJsonObject()) { wrapper.add(elem.toString(), deserialize(elem, context)); } else if (elem.isJsonArray()) { wrapper.add(elem.toString(), deserialize(elem, context)); } } } else { // when we arrive here, the input element was // not one of the expected types => error throw new JsonSyntaxException("Invalid JSON, expecting object or array"); } return wrapper; }
From source file:org.structr.rest.JsonInputGSONAdapter.java
License:Open Source License
private JsonInput deserialize(JsonElement json, JsonDeserializationContext context) throws JsonParseException { JsonInput wrapper = new JsonInput(); if (json.isJsonObject()) { JsonObject obj = json.getAsJsonObject(); for (Entry<String, JsonElement> entry : obj.entrySet()) { String key = entry.getKey(); JsonElement elem = entry.getValue(); if (elem.isJsonNull()) { wrapper.add(key, null);/* ww w .ja v a 2 s. co m*/ } else if (elem.isJsonObject()) { wrapper.add(key, deserialize(elem, context)); } else if (elem.isJsonArray()) { final JsonArray array = elem.getAsJsonArray(); final List list = new LinkedList(); for (JsonElement element : array) { if (element.isJsonPrimitive()) { list.add(fromPrimitive((element.getAsJsonPrimitive()))); } else if (element.isJsonObject()) { // create map of values list.add(deserialize(element, context)); } } wrapper.add(key, list); } else if (elem.isJsonPrimitive()) { // wrapper.add(key, elem.getAsString()); wrapper.add(key, fromPrimitive(elem.getAsJsonPrimitive())); } } } else if (json.isJsonArray()) { JsonArray array = json.getAsJsonArray(); for (JsonElement elem : array) { if (elem.isJsonPrimitive()) { wrapper.add(elem.toString(), fromPrimitive(elem.getAsJsonPrimitive())); } else if (elem.isJsonObject()) { wrapper.add(elem.toString(), deserialize(elem, context)); } else if (elem.isJsonArray()) { wrapper.add(elem.toString(), deserialize(elem, context)); } } } return wrapper; }