Example usage for com.google.gson JsonElement getAsString

List of usage examples for com.google.gson JsonElement getAsString

Introduction

In this page you can find the example usage for com.google.gson JsonElement getAsString.

Prototype

public String getAsString() 

Source Link

Document

convenience method to get this element as a string value.

Usage

From source file:ca.tokenize.sally.api.DateDeserializer.java

License:Open Source License

@Override
public Date deserialize(JsonElement element, Type type, JsonDeserializationContext context) {
    String date = element.getAsString();

    SimpleDateFormat format = new SimpleDateFormat(SALLY_DATE_FORMAT);
    format.setTimeZone(TimeZone.getTimeZone("UTC"));

    try {/*from ww w  .  j a va  2  s .  c o m*/
        return format.parse(date);
    } catch (ParseException e) {
        return null;
    }
}

From source file:ca.ualberta.cmput301.t03.trading.serialization.TradeStateDeserializer.java

License:Open Source License

public TradeState deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    try {// w w  w .j  a v  a  2 s  . c o  m
        return TradeStateDeserializer.fromString(json.getAsString());
    } catch (IllegalTradeStateDeserialization illegalTradeStateDeserialization) {
        illegalTradeStateDeserialization.printStackTrace();
        return null;
    }
}

From source file:ca.ualberta.cs.drivr.UriSerializer.java

License:Apache License

@Override
public Uri deserialize(final JsonElement src, final Type srcType, final JsonDeserializationContext context)
        throws JsonParseException {
    return Uri.parse(src.getAsString());
}

From source file:ca.ualberta.cs.team1travelexpenseapp.gsonUtils.RuntimeTypeAdapterFactory.java

License:Apache License

public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
    if (type.getRawType() != baseType) {
        return null;
    }/*from   w w  w .j a va2  s .  co  m*/

    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);
            JsonElement labelJsonElement = jsonElement.getAsJsonObject().remove(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 {
            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();
            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.kave.commons.utils.json.DurationConverter.java

License:Apache License

@Override
public Duration deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    String in = json.getAsString();

    int dot = in.indexOf('.');
    if (dot == -1) {
        in = "0." + in + ".0";
    } else if (dot > in.indexOf(':')) {
        in = "0." + in;
    } else if (in.lastIndexOf('.') < in.lastIndexOf(':')) {
        in = in + ".0";
    }/*from   ww w.j  a  v  a  2s  .  co  m*/

    String daysStr = in.substring(0, in.indexOf('.'));
    String hoursStr = in.substring(in.indexOf('.') + 1, in.indexOf(':'));
    String minStr = in.substring(in.indexOf(':') + 1, in.lastIndexOf(':'));
    String secStr = in.substring(in.lastIndexOf(':') + 1, in.lastIndexOf('.'));
    String tickStr = in.substring(in.lastIndexOf('.') + 1, in.length());

    long days = Long.parseLong(daysStr);
    long hours = Long.parseLong(hoursStr);
    long min = Long.parseLong(minStr);
    long seconds = Long.parseLong(secStr);
    long ticks = Long.parseLong(tickStr);
    long nanos = 100 * ticks;

    LocalDateTime start = LocalDateTime.MIN;
    LocalDateTime end = start.plusDays(days).plusHours(hours).plusMinutes(min).plusSeconds(seconds)
            .plusNanos(nanos);
    return Duration.between(start, end);
}

From source file:cc.kave.commons.utils.json.GsonNameDeserializer.java

License:Apache License

@Override
public IName deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    String str = json.getAsString();
    return NameSerialization.deserialize(str);
}

From source file:cc.kave.commons.utils.json.legacy.GsonFieldNameDeserializer.java

License:Open Source License

@Override
public ICoReFieldName deserialize(final JsonElement json, final Type typeOfT,
        final JsonDeserializationContext context) throws JsonParseException {
    final String identifier = json.getAsString();
    return CoReFieldName.get(identifier);
}

From source file:cc.kave.commons.utils.json.legacy.GsonFileDeserializer.java

License:Open Source License

@Override
public File deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context)
        throws JsonParseException {
    final String path = json.getAsString();
    return new File(path);
}

From source file:cc.kave.commons.utils.json.legacy.GsonMethodNameDeserializer.java

License:Open Source License

@Override
public ICoReMethodName deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    String identifier = json.getAsString();
    return CoReMethodName.get(identifier);
}

From source file:cc.kave.commons.utils.json.legacy.GsonTypeNameDeserializer.java

License:Open Source License

@Override
public ICoReTypeName deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    String identifier = json.getAsString();
    return CoReTypeName.get(identifier);
}