Java Code Examples for com.google.gson.JsonElement#isJsonPrimitive()
The following examples show how to use
com.google.gson.JsonElement#isJsonPrimitive() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonConverter.java From external-resources with Apache License 2.0 | 6 votes |
@Nullable private Resource get(JsonElement json, int depth) throws JsonParseException { if (json.isJsonPrimitive()) { return get(json.getAsJsonPrimitive()); } else if (json.isJsonArray() && depth == 0) { ArrayList<Resource> resources = new ArrayList<>(); for (JsonElement element : json.getAsJsonArray()) { resources.add(get(element, 1)); } Resource[] resourcesArray = new Resource[resources.size()]; resourcesArray = resources.toArray(resourcesArray); return new Resource(resources.toArray(resourcesArray)); } else { return null; } }
Example 2
Source File: ConfigurationDeserializer.java From smarthome with Eclipse Public License 2.0 | 6 votes |
private Configuration deserialize(JsonObject propertiesObject) { Configuration configuration = new Configuration(); for (Entry<String, JsonElement> entry : propertiesObject.entrySet()) { JsonElement value = entry.getValue(); String key = entry.getKey(); if (value.isJsonPrimitive()) { JsonPrimitive primitive = value.getAsJsonPrimitive(); configuration.put(key, deserialize(primitive)); } else if (value.isJsonArray()) { JsonArray array = value.getAsJsonArray(); configuration.put(key, deserialize(array)); } else { throw new IllegalArgumentException( "Configuration parameters must be primitives or arrays of primities only but was " + value); } } return configuration; }
Example 3
Source File: MapTypeAdapterFactory.java From gson with Apache License 2.0 | 6 votes |
private String keyToString(JsonElement keyElement) { if (keyElement.isJsonPrimitive()) { JsonPrimitive primitive = keyElement.getAsJsonPrimitive(); if (primitive.isNumber()) { return String.valueOf(primitive.getAsNumber()); } else if (primitive.isBoolean()) { return Boolean.toString(primitive.getAsBoolean()); } else if (primitive.isString()) { return primitive.getAsString(); } else { throw new AssertionError(); } } else if (keyElement.isJsonNull()) { return "null"; } else { throw new AssertionError(); } }
Example 4
Source File: Indexes.java From java-cloudant with Apache License 2.0 | 6 votes |
/** * Utility to list indexes of a given type. * * @param type the type of index to list, null means all types * @param modelType the class to deserialize the index into * @param <T> the type of the index * @return the list of indexes of the specified type */ private <T extends Index> List<T> listIndexType(String type, Class<T> modelType) { List<T> indexesOfType = new ArrayList<T>(); Gson g = new Gson(); for (JsonElement index : indexes) { if (index.isJsonObject()) { JsonObject indexDefinition = index.getAsJsonObject(); JsonElement indexType = indexDefinition.get("type"); if (indexType != null && indexType.isJsonPrimitive()) { JsonPrimitive indexTypePrimitive = indexType.getAsJsonPrimitive(); if (type == null || (indexTypePrimitive.isString() && indexTypePrimitive .getAsString().equals(type))) { indexesOfType.add(g.fromJson(indexDefinition, modelType)); } } } } return indexesOfType; }
Example 5
Source File: ConvertJsonToXmlService.java From cs-actions with Apache License 2.0 | 6 votes |
private Element getXmlElementFromJsonElement(final JsonElement childJson, final String childTagName) { if (childJson.isJsonPrimitive()) { final String childValue = getJsonPrimitiveValue(childJson.getAsJsonPrimitive()); return createElement(childTagName).setText(childValue); } else if (childJson.isJsonObject()) { return convertJsonObjectToXmlElement(childJson.getAsJsonObject(), childTagName); } else if (childJson.isJsonArray()) { final String itemName = jsonArrayItemNames.containsKey(childTagName) ? jsonArrayItemNames.get(childTagName) : jsonArrayItemName; final Element container = createElement(childTagName); final List<Element> elements = convertToXmlElementsJsonArray(childJson.getAsJsonArray(), itemName); for (final Element element : elements) { container.addContent(element); } return container; } return null; }
Example 6
Source File: QuerySerializer.java From quaerite with Apache License 2.0 | 6 votes |
private static ParameterizableString buildParameterizableString(int i, AtomicBoolean isMap, String name, JsonElement element) { if (element.isJsonPrimitive()) { if (i > 0 && isMap.get()) { throw new IllegalArgumentException( "Can't mix String with map in this array. " + "Must be all same type" ); } isMap.set(false); return new ParameterizableString(name, Integer.toString(i), element.getAsString()); } else if (element.isJsonObject()) { if (i > 0 && isMap.get() == false) { throw new IllegalArgumentException( "Can't mix String with map in this array. " + "Must be all same type" ); } String id = JsonUtil.getSingleChildName(element.getAsJsonObject()); String val = element.getAsJsonObject().get(id).getAsString(); isMap.set(true); return new ParameterizableString(name, id, val); } else { throw new IllegalArgumentException("I'm sorry, but this must be a String or an object"); } }
Example 7
Source File: JsonConverter.java From iotplatform with Apache License 2.0 | 6 votes |
public static List<KvEntry> parseValues(JsonObject valuesObject) { List<KvEntry> result = new ArrayList<>(); for (Entry<String, JsonElement> valueEntry : valuesObject.entrySet()) { JsonElement element = valueEntry.getValue(); if (element.isJsonPrimitive()) { JsonPrimitive value = element.getAsJsonPrimitive(); if (value.isString()) { result.add(new StringDataEntry(valueEntry.getKey(), value.getAsString())); } else if (value.isBoolean()) { result.add(new BooleanDataEntry(valueEntry.getKey(), value.getAsBoolean())); } else if (value.isNumber()) { if (value.getAsString().contains(".")) { result.add(new DoubleDataEntry(valueEntry.getKey(), value.getAsDouble())); } else { result.add(new LongDataEntry(valueEntry.getKey(), value.getAsLong())); } } else { throw new JsonSyntaxException("Can't parse value: " + value); } } else { throw new JsonSyntaxException("Can't parse value: " + element); } } return result; }
Example 8
Source File: JsonUtil.java From p4ic4idea with Apache License 2.0 | 6 votes |
public static String[] asNullableStringArray(String key, JsonElement el) throws ResponseFormatException { if (el == null || el.isJsonNull()) { return null; } if (! el.isJsonArray()) { throw new ResponseFormatException(key, el); } JsonArray vay = el.getAsJsonArray(); List<String> ret = new ArrayList<>(vay.size()); for (int i = 0; i < vay.size(); i++) { JsonElement vi = vay.get(i); if (vi.isJsonNull()) { ret.add(null); } else if (vi.isJsonPrimitive()) { ret.add(vi.getAsString()); } else { throw new ResponseFormatException(key + '[' + i + ']', vi); } } return ret.toArray(new String[0]); }
Example 9
Source File: FlattenOperation.java From bender with Apache License 2.0 | 6 votes |
protected void perform(JsonObject obj) { Set<Entry<String, JsonElement>> entries = obj.entrySet(); Set<Entry<String, JsonElement>> orgEntries = new HashSet<Entry<String, JsonElement>>(entries); for (Entry<String, JsonElement> entry : orgEntries) { JsonElement val = entry.getValue(); if (val.isJsonPrimitive() || val.isJsonNull()) { continue; } obj.remove(entry.getKey()); if (val.isJsonObject()) { perform(obj, val.getAsJsonObject(), entry.getKey()); } else if (val.isJsonArray()) { perform(obj, val.getAsJsonArray(), entry.getKey()); } } }
Example 10
Source File: EventTypeExtractorImpl.java From java-slack-sdk with MIT License | 6 votes |
private static String extractFieldUnderEvent(String json, String fieldName) { try { JsonElement root = JsonParser.parseString(json); if (root != null && root.isJsonObject() && root.getAsJsonObject().has("event")) { JsonElement event = root.getAsJsonObject().get("event"); if (event.isJsonObject() && event.getAsJsonObject().has(fieldName)) { JsonElement eventType = event.getAsJsonObject().get(fieldName); if (eventType.isJsonPrimitive()) { return eventType.getAsString(); } } } } catch (JsonSyntaxException e) { log.debug("Failed to parse {} as a JSON data", json, e); } return ""; }
Example 11
Source File: NaturalLanguageValueAdapter.java From activitystreams with Apache License 2.0 | 5 votes |
/** * Method deserialize. * @param element JsonElement * @param type1 Type * @param context JsonDeserializationContext * @return NLV * @throws JsonParseException * @see com.google.gson.JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext) */ public NLV deserialize( JsonElement element, Type type1, JsonDeserializationContext context) throws JsonParseException { checkArgument( element.isJsonPrimitive() || element.isJsonObject()); if (element.isJsonPrimitive()) { JsonPrimitive prim = element.getAsJsonPrimitive(); checkArgument(prim.isString()); return NLV.SimpleNLV.make( prim.getAsString()); } else { try { JsonObject obj = element.getAsJsonObject(); NLV.MapNLV.Builder builder = NLV.MapNLV.make(); for (Entry<String,JsonElement> entry : obj.entrySet()) builder.set( entry.getKey(), entry.getValue().getAsString()); return builder.get(); } catch (Throwable t) { throw new IllegalArgumentException(); } } }
Example 12
Source File: JsonUtil.java From p4ic4idea with Apache License 2.0 | 5 votes |
public static String getNullableStringKey(JsonObject obj, String key) throws ResponseFormatException { JsonElement val = getNullableKey(obj, key); if (val == null) { return null; } if (val.isJsonPrimitive()) { return val.getAsString(); } throw new ResponseFormatException(key, val); }
Example 13
Source File: JsonUtil.java From quaerite with Apache License 2.0 | 5 votes |
public static String getPrimitive(JsonElement root, String key, String dflt) { if (! root.isJsonObject()) { return dflt; } JsonElement el = ((JsonObject)root).get(key); if (! el.isJsonPrimitive()) { return dflt; } return ((JsonPrimitive)el).getAsString(); }
Example 14
Source File: Config.java From writelatex-git-bridge with MIT License | 5 votes |
private String getOptionalString(JsonObject configObject, String name) { JsonElement element = configObject.get(name); if (element == null || !element.isJsonPrimitive()) { return ""; } return element.getAsString(); }
Example 15
Source File: JsonElementConstructor.java From celos with Apache License 2.0 | 5 votes |
private JsonElement deepCopy(String path, JsonElement el, Set<String> ignorePaths) { if (el.isJsonArray()) { return deepCopyJsonArray(path, el.getAsJsonArray(), ignorePaths); } if (el.isJsonObject()) { return deepCopyJsonObject(path, el.getAsJsonObject(), ignorePaths); } if (el.isJsonPrimitive() || el.isJsonNull()) { return el; } throw new IllegalStateException("JSONElement should be either Array, Object, Primitive or Null. So you cannot get here"); }
Example 16
Source File: Album.java From last.fm-api with MIT License | 5 votes |
@Override public Artist deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if(json.isJsonPrimitive()){ Artist artist=new Artist(); artist.name=json.getAsString(); return artist; } return context.deserialize(json,typeOfT); }
Example 17
Source File: AbstractGsonConverter.java From helper with MIT License | 5 votes |
@Override @Nullable public Object unwrapElement(JsonElement element) { if (element.isJsonNull()) { return null; } else if (element.isJsonArray()) { return unwrapArray(element.getAsJsonArray()); } else if (element.isJsonObject()) { return unwrapObject(element.getAsJsonObject()); } else if (element.isJsonPrimitive()) { return unwarpPrimitive(element.getAsJsonPrimitive()); } else { throw new IllegalArgumentException("Unknown element type: " + element); } }
Example 18
Source File: CheckboxSetting.java From ForgeWurst with GNU General Public License v3.0 | 5 votes |
@Override public void fromJson(JsonElement json) { if(!json.isJsonPrimitive()) return; JsonPrimitive primitive = json.getAsJsonPrimitive(); if(!primitive.isBoolean()) return; setChecked(primitive.getAsBoolean()); }
Example 19
Source File: JsonElementConversionWithAvroSchemaFactory.java From incubator-gobblin with Apache License 2.0 | 4 votes |
@Override Object convertField(JsonElement value) { for(JsonElementConverter converter: converters) { try { switch (converter.getTargetType()) { case STRING: { if (value.isJsonPrimitive() && value.getAsJsonPrimitive().isString()) { return converter.convert(value); } break; } case FIXED: case BYTES: case INT: case LONG: case FLOAT: case DOUBLE: { if (value.isJsonPrimitive() && value.getAsJsonPrimitive().isNumber()) { return converter.convert(value); } break; } case BOOLEAN:{ if (value.isJsonPrimitive() && value.getAsJsonPrimitive().isBoolean()) { return converter.convert(value); } break; } case ARRAY:{ if (value.isJsonArray()) { return converter.convert(value); } break; } case MAP: case ENUM: case RECORD:{ if (value.isJsonObject()) { return converter.convert(value); } break; } case NULL:{ if(value.isJsonNull()) { return converter.convert(value); } break; } case UNION: return new UnsupportedDateTypeException("does not support union type in union"); default: return converter.convert(value); } } catch (Exception e){} } throw new RuntimeException(String.format("Cannot convert %s to avro using schema %s", value.getAsString(), schemaNode.toString())); }
Example 20
Source File: GsonRuntime.java From jmespath-java with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public Number toNumber(JsonElement value) { return (value.isJsonPrimitive() && value.getAsJsonPrimitive().isNumber()) ? value.getAsNumber() : null; }