List of usage examples for com.google.gson JsonElement getAsString
public String getAsString()
From source file:cc.kave.commons.utils.json.LocalDateTimeConverter.java
License:Apache License
@Override public LocalDateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { DateTimeFormatter fmt = new DateTimeFormatterBuilder().append(DateTimeFormatter.ISO_LOCAL_DATE_TIME) .optionalStart().appendOffset("+HH:mm", "").optionalEnd().toFormatter(); return fmt.parse(json.getAsString(), LocalDateTime::from); }
From source file:cc.kave.commons.utils.json.RuntimeTypeAdapterFactory.java
License:Apache License
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) { if (type.getRawType() != baseType) { return null; }/* w w w. j a va 2 s . c om*/ final Map<String, TypeAdapter<?>> labelToDelegate = new LinkedHashMap<String, TypeAdapter<?>>(); final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = new LinkedHashMap<Class<?>, TypeAdapter<?>>(); for (Map.Entry<String, Class<?>> entry : labelToSubtype.entrySet()) { TypeAdapter<?> delegate = gson.getDelegateAdapter(this, TypeToken.get(entry.getValue())); labelToDelegate.put(entry.getKey(), delegate); subtypeToDelegate.put(entry.getValue(), delegate); } return new TypeAdapter<R>() { @Override public R read(JsonReader in) throws IOException { JsonElement jsonElement = Streams.parse(in); // (kave adaptation) was: ".remove(typeFiledName)" JsonElement labelJsonElement = jsonElement.getAsJsonObject().get(typeFieldName); if (labelJsonElement == null) { throw new JsonParseException("cannot deserialize " + baseType + " because it does not define a field named " + typeFieldName); } String label = labelJsonElement.getAsString(); @SuppressWarnings("unchecked") // registration requires that // subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) labelToDelegate.get(label); if (delegate == null) { throw new JsonParseException("cannot deserialize " + baseType + " subtype named " + label + "; did you forget to register a subtype?"); } return delegate.fromJsonTree(jsonElement); } @Override public void write(JsonWriter out, R value) throws IOException { if (value == null) { Streams.write(null, out); return; } Class<?> srcType = value.getClass(); String label = subtypeToLabel.get(srcType); @SuppressWarnings("unchecked") // registration requires that // subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) subtypeToDelegate.get(srcType); if (delegate == null) { throw new JsonParseException( "cannot serialize " + srcType.getName() + "; did you forget to register a subtype?"); } JsonObject jsonObject = delegate.toJsonTree(value).getAsJsonObject(); // (kave adaptation) disabled check // if (jsonObject.has(typeFieldName)) { // throw new JsonParseException("cannot serialize " + // srcType.getName() // + " because it already defines a field named " + // typeFieldName); // } JsonObject clone = new JsonObject(); clone.add(typeFieldName, new JsonPrimitive(label)); for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) { clone.add(e.getKey(), e.getValue()); } Streams.write(clone, out); } }; }
From source file:cc.recommenders.utils.gson.GsonFieldNameDeserializer.java
License:Open Source License
@Override public IFieldName deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { final String identifier = json.getAsString(); return VmFieldName.get(identifier); }
From source file:cc.recommenders.utils.gson.GsonMethodNameDeserializer.java
License:Open Source License
@Override public IMethodName deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { String identifier = json.getAsString(); return VmMethodName.get(identifier); }
From source file:cc.recommenders.utils.gson.GsonTypeNameDeserializer.java
License:Open Source License
@Override public ITypeName deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { String identifier = json.getAsString(); return VmTypeName.get(identifier); }
From source file:ccm.pay2spawn.permissions.Group.java
License:Open Source License
public Group(JsonObject jsonObject) { name = jsonObject.get("name").getAsString(); if (jsonObject.has("parent")) parent = jsonObject.get("parent").getAsString(); for (JsonElement node : jsonObject.getAsJsonArray("nodes")) nodes.add(new Node(node.getAsString())); }
From source file:ccm.pay2spawn.permissions.Player.java
License:Open Source License
public Player(JsonObject jsonObject) { name = jsonObject.get("name").getAsString(); if (jsonObject.has("groups")) for (JsonElement groupName : jsonObject.getAsJsonArray("groups")) groups.add(groupName.getAsString()); if (jsonObject.has("overrideNodes")) for (JsonElement node : jsonObject.getAsJsonArray("overrideNodes")) overrideNodes.add(new Node(node.getAsString())); }
From source file:ccm.pay2spawn.util.Helper.java
License:Open Source License
/** * Fill in variables from a donation/*from www .java 2s . co m*/ * * @param dataToFormat data to be formatted * @param donation the donation data * * @return the fully var-replaced JsonElement */ public static JsonElement formatText(JsonElement dataToFormat, Donation donation, Reward reward) { if (dataToFormat.isJsonPrimitive() && dataToFormat.getAsJsonPrimitive().isString()) { return new JsonPrimitive(Helper.formatText(dataToFormat.getAsString(), donation, reward)); } if (dataToFormat.isJsonArray()) { JsonArray out = new JsonArray(); for (JsonElement element : dataToFormat.getAsJsonArray()) { out.add(formatText(element, donation, reward)); } return out; } if (dataToFormat.isJsonObject()) { JsonObject out = new JsonObject(); for (Map.Entry<String, JsonElement> entity : dataToFormat.getAsJsonObject().entrySet()) { out.add(entity.getKey(), Helper.formatText(entity.getValue(), donation, reward)); } return out; } return dataToFormat; }
From source file:ch.berta.fabio.popularmovies.data.rest.MovieDbClient.java
License:Apache License
/** * Returns a custom date deserializer that handles empty strings and returns today's date instead. * * @return the Gson object to use/*from w ww. j a v a2 s.c o m*/ */ private static Gson getGsonObject() { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { final DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.US); @Override public Date deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { try { return dateFormat.parse(json.getAsString()); } catch (ParseException e) { return new Date(); } } }); return gsonBuilder.create(); }
From source file:ch.cern.db.flume.sink.kite.parser.JSONtoAvroParser.java
License:GNU General Public License
private Object getElementAsType(Schema schema, JsonElement element) { if (element == null || element.isJsonNull()) return null; switch (schema.getType()) { case BOOLEAN: return element.getAsBoolean(); case DOUBLE:/* w ww .ja va2 s. c o m*/ return element.getAsDouble(); case FLOAT: return element.getAsFloat(); case INT: return element.getAsInt(); case LONG: return element.getAsLong(); case NULL: return null; case UNION: return getElementAsType(schema.getTypes().get(0), element); // case FIXED: // case ARRAY: // case BYTES: // case ENUM: // case MAP: // case RECORD: // case STRING: default: return element.getAsString(); } }