List of usage examples for com.google.gson JsonElement isJsonPrimitive
public boolean isJsonPrimitive()
From source file:nl.talsmasoftware.enumerables.support.json.gson.EnumerableDeserializer.java
License:Apache License
private String valueOf(JsonObject jsonObject) { final JsonElement value = jsonObject.get("value"); if (value == null) throw new IllegalStateException("Attribute \"value\" is required to parse an Enumerable JSON object."); else if (!value.isJsonPrimitive()) throw new IllegalStateException( "Attribute \"value\" must contain a String value for Enumerable JSON objects."); return value.getAsString(); }
From source file:org.apache.abdera2.activities.io.gson.BaseAdapter.java
License:Apache License
public ASBase deserialize(JsonElement el, Type type, JsonDeserializationContext context) throws JsonParseException { JsonObject obj = (JsonObject) el;// www . ja v a2 s . com ASBase.Builder<?, ?> builder; if (type == Collection.class) builder = Collection.makeCollection(); else if (type == Activity.class) builder = Activity.makeActivity(); else if (type == MediaLink.class) builder = MediaLink.makeMediaLink(); else if (type == PlaceObject.class) builder = PlaceObject.makePlace(); else if (type == Mood.class) builder = Mood.makeMood(); else if (type == Address.class) builder = Address.makeAddress(); else { JsonPrimitive ot = obj.getAsJsonPrimitive("objectType"); if (ot != null) { String ots = ot.getAsString(); Class<? extends ASObject.Builder> _class = objsmap.get(ots); if (_class != null) { builder = Discover.locate(_class, _class.getName()); try { builder = _class.getConstructor(String.class).newInstance(ots); } catch (Throwable t) { } } else builder = ASObject.makeObject(ots); } else { if (obj.has("verb") && (obj.has("actor") || obj.has("object") || obj.has("target"))) { builder = Activity.makeActivity(); } else if (obj.has("items")) { builder = Collection.makeCollection(); } else { builder = ASObject.makeObject(); // anonymous } } } for (Entry<String, JsonElement> entry : obj.entrySet()) { String name = entry.getKey(); if (name.equalsIgnoreCase("objectType")) continue; Class<?> _class = map.get(name); JsonElement val = entry.getValue(); if (val.isJsonPrimitive()) { if (_class != null) { builder.set(name, context.deserialize(val, _class)); } else { JsonPrimitive prim = val.getAsJsonPrimitive(); if (prim.isBoolean()) builder.set(name, prim.getAsBoolean()); else if (prim.isNumber()) builder.set(name, prim.getAsNumber()); else { builder.set(name, prim.getAsString()); } } } else if (val.isJsonArray()) { ImmutableList.Builder<Object> list = ImmutableList.builder(); JsonArray arr = val.getAsJsonArray(); processArray(arr, _class, context, list); builder.set(name, list.build()); } else if (val.isJsonObject()) { if (map.containsKey(name)) { builder.set(name, context.deserialize(val, map.get(name))); } else builder.set(name, context.deserialize(val, ASObject.class)); } } return builder.get(); }
From source file:org.apache.abdera2.activities.io.gson.BaseAdapter.java
License:Apache License
private void processArray(JsonArray arr, Class<?> _class, JsonDeserializationContext context, ImmutableList.Builder<Object> list) { for (JsonElement mem : arr) { if (mem.isJsonPrimitive()) { if (_class != null) { list.add(context.deserialize(mem, _class)); } else { JsonPrimitive prim2 = (JsonPrimitive) mem; if (prim2.isBoolean()) list.add(prim2.getAsBoolean()); else if (prim2.isNumber()) list.add(prim2.getAsNumber()); else list.add(prim2.getAsString()); }//from w w w. j a v a2s . com } else if (mem.isJsonObject()) { list.add(context.deserialize(mem, _class != null ? _class : ASObject.class)); } else if (mem.isJsonArray()) { JsonArray array = mem.getAsJsonArray(); ImmutableList.Builder<Object> objs = ImmutableList.builder(); processArray(array, _class, context, objs); list.add(objs.build()); } } }
From source file:org.apache.abdera2.activities.io.gson.MultimapAdapter.java
License:Apache License
protected static ImmutableList<Object> arraydes(JsonArray array, JsonDeserializationContext context) { ImmutableList.Builder<Object> builder = ImmutableList.builder(); for (JsonElement child : array) if (child.isJsonArray()) builder.add(arraydes(child.getAsJsonArray(), context)); else if (child.isJsonObject()) builder.add(context.deserialize(child, ASBase.class)); else if (child.isJsonPrimitive()) builder.add(primdes(child.getAsJsonPrimitive())); return builder.build(); }
From source file:org.apache.abdera2.activities.io.gson.MultimapAdapter.java
License:Apache License
public Multimap deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { Multimap mm = create(typeOfT);//from w w w .j av a2 s.c o m JsonObject obj = json.getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { String key = entry.getKey(); JsonElement val = entry.getValue(); if (val.isJsonArray()) for (JsonElement el : val.getAsJsonArray()) if (el.isJsonArray()) mm.put(key, arraydes(el.getAsJsonArray(), context)); else if (el.isJsonObject()) mm.put(key, context.deserialize(el, ASBase.class)); else if (el.isJsonNull()) mm.put(key, null); else if (el.isJsonPrimitive()) mm.put(key, primdes(el.getAsJsonPrimitive())); else if (val.isJsonObject()) mm.put(key, context.deserialize(val, ASBase.class)); else if (val.isJsonPrimitive()) mm.put(key, primdes(val.getAsJsonPrimitive())); } return mm; }
From source file:org.apache.edgent.apps.runtime.JobMonitorAppEvent.java
License:Apache License
/** * Gets a string property with the specified name from the given JSON * object.// ww w .j a v a 2s .c o m * * @param value a JSON object * @param name the property name * @return the property value * * @throws IllegalArgumentException if could not find a property with the * given name */ static String getProperty(JsonObject value, String name) { JsonElement e = value.get(name); if (e != null && e.isJsonPrimitive()) { try { return e.getAsString(); } catch (Exception ex) { } } throw new IllegalArgumentException("Could not find the " + name + " property in: " + value); }
From source file:org.apache.gobblin.converter.json.JsonSchema.java
License:Apache License
/** * Build a {@link JsonSchema} using {@link JsonArray} * This will create a {@link SchemaType} of {@link SchemaType#CHILD} * @param jsonObject//from ww w .ja v a 2 s . c o m */ public JsonSchema(JsonObject jsonObject) { JsonObject root = new JsonObject(); if (!jsonObject.has(COLUMN_NAME_KEY) && !jsonObject.has(DATA_TYPE_KEY)) { root.addProperty(COLUMN_NAME_KEY, DEFAULT_RECORD_COLUMN_NAME); root.add(DATA_TYPE_KEY, jsonObject); jsonObject = root; } if (!jsonObject.has(COLUMN_NAME_KEY) && jsonObject.has(DATA_TYPE_KEY)) { jsonObject.addProperty(COLUMN_NAME_KEY, DEFAULT_RECORD_COLUMN_NAME); } setJsonSchemaProperties(jsonObject); JsonElement typeElement = getDataType().get(TYPE_KEY); if (typeElement.isJsonPrimitive()) { this.type = Type.valueOf(typeElement.getAsString().toUpperCase()); } else if (typeElement.isJsonArray()) { JsonArray jsonArray = typeElement.getAsJsonArray(); if (jsonArray.size() != 2) { throw new RuntimeException("Invalid " + TYPE_KEY + "property in schema for union types"); } this.type = UNION; JsonElement type1 = jsonArray.get(0); JsonElement type2 = jsonArray.get(1); if (type1.isJsonPrimitive()) { this.firstType = buildBaseSchema(Type.valueOf(type1.getAsString().toUpperCase())); } if (type2.isJsonPrimitive()) { this.secondType = buildBaseSchema(Type.valueOf(type2.getAsString().toUpperCase())); } if (type1.isJsonObject()) { this.firstType = buildBaseSchema(type1.getAsJsonObject()); } if (type2.isJsonObject()) { this.secondType = buildBaseSchema(type2.getAsJsonObject()); } } else { throw new RuntimeException("Invalid " + TYPE_KEY + "property in schema"); } this.json = jsonObject; JsonArray jsonArray = new JsonArray(); jsonArray.add(jsonObject); this.jsonArray = jsonArray; this.schemaNestedLevel = CHILD; }
From source file:org.apache.gobblin.converter.json.JsonSchema.java
License:Apache License
/** * Fetches dataType.values from the JsonObject * @return// w w w .jav a2s .co m */ public JsonSchema getValuesWithinDataType() { JsonElement element = this.getDataType().get(MAP_ITEMS_KEY); if (element.isJsonObject()) { return new JsonSchema(element.getAsJsonObject()); } if (element.isJsonArray()) { return new JsonSchema(element.getAsJsonArray()); } if (element.isJsonPrimitive()) { return buildBaseSchema(Type.valueOf(element.getAsString().toUpperCase())); } throw new UnsupportedOperationException( "Map values can only be defined using JsonObject, JsonArray or JsonPrimitive."); }
From source file:org.apache.gobblin.converter.json.JsonSchema.java
License:Apache License
public JsonSchema getItemsWithinDataType() { JsonElement element = this.getDataType().get(ARRAY_ITEMS_KEY); if (element.isJsonObject()) { return new JsonSchema(element.getAsJsonObject()); }//from w w w . j a v a 2s . c om if (element.isJsonPrimitive()) { return buildBaseSchema(Type.valueOf(element.getAsString().toUpperCase())); } throw new UnsupportedOperationException( "Array items can only be defined using JsonObject or JsonPrimitive."); }
From source file:org.apache.hadoop.hive.json.JsonSchemaFinder.java
License:Apache License
private static HiveType pickType(JsonElement json) { if (json.isJsonPrimitive()) { JsonPrimitive prim = (JsonPrimitive) json; if (prim.isBoolean()) { return new BooleanType(); } else if (prim.isNumber()) { BigDecimal dec = prim.getAsBigDecimal(); if (dec.scale() > 0) { return new FloatingPointType(dec.doubleValue()); } else { return new IntegerType(dec.longValue()); }// w w w . j a v a 2s .c o m } else { String str = prim.getAsString(); if (DATE_PATTERN.matcher(str).matches()) { return new TimestampType(); } else if (HEX_PATTERN.matcher(str).matches()) { return new BinaryType(); } else { return new StringType(); } } } else if (json.isJsonNull()) { return new NullType(); } else if (json.isJsonArray()) { ListType result = new ListType(); for (JsonElement child : ((JsonArray) json)) { HiveType sub = pickType(child); if (result.elementType == null) { result.elementType = sub; } else { result.elementType = mergeType(result.elementType, sub); } } return result; } else { JsonObject obj = (JsonObject) json; StructType result = new StructType(); for (Map.Entry<String, JsonElement> field : obj.entrySet()) { String fieldName = field.getKey(); HiveType type = pickType(field.getValue()); result.fields.put(fieldName, type); } StringBuilder builder = new StringBuilder(); boolean first = true; for (String key : result.fields.keySet()) { if (first) { first = false; } else { builder.append(","); } builder.append(key); } result.values.add(builder.toString()); return result; } }