Example usage for com.google.gson JsonPrimitive JsonPrimitive

List of usage examples for com.google.gson JsonPrimitive JsonPrimitive

Introduction

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

Prototype

public JsonPrimitive(Character c) 

Source Link

Document

Create a primitive containing a character.

Usage

From source file:ca.cmput301w14t09.elasticSearch.JsonBitmapConverter.java

License:GNU General Public License

/**
 * serialize turns a bitmap into a Json primivite type
 *//*from  www.j  a  v  a  2s .  c om*/

@Override
public JsonElement serialize(Bitmap src, Type typeOfSrc, JsonSerializationContext context) {

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    src.compress(Bitmap.CompressFormat.JPEG, 80, stream);
    String base64Encoded = Base64.encodeToString(stream.toByteArray(), Base64.NO_WRAP);
    return new JsonPrimitive(base64Encoded);
}

From source file:ca.cmput301w14t09.FileManaging.SerializableBitmap.java

License:GNU General Public License

/**
 * serialize turns a bitmap into a Json primitive type
 * @param src/* w  ww  .  j av  a 2  s.c o m*/
 * @param typeOfSrc
 * @param context
 * @return 
 */
@Override
public JsonElement serialize(Bitmap src, Type typeOfSrc, JsonSerializationContext context) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    src.compress(Bitmap.CompressFormat.JPEG, 80, stream);
    String base64Encoded = Base64.encodeToString(stream.toByteArray(), Base64.NO_WRAP);
    return new JsonPrimitive(base64Encoded);
}

From source file:ca.cs.ualberta.localpost.controller.BitmapJsonConverter.java

License:Open Source License

@Override
public JsonElement serialize(Bitmap src, Type typeOfSrc, JsonSerializationContext context) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    src.compress(Bitmap.CompressFormat.JPEG, 80, stream);
    String base64Encoded = Base64.encodeToString(stream.toByteArray(), Base64.NO_WRAP);
    return new JsonPrimitive(base64Encoded);
}

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

License:Apache License

@Override
public JsonElement serialize(Uri src, Type typeOfSrc, JsonSerializationContext context) {
    return new JsonPrimitive(src.toString());
}

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 ww  w  . j av  a2  s .  c  o 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 JsonElement serialize(Duration duration, Type typeOfSrc, JsonSerializationContext context) {
    LocalTime plus = LocalTime.MIDNIGHT.plus(duration);
    String middle = plus.format(DateTimeFormatter.ofPattern("HH:mm:ss"));
    long days = duration.toDays();
    // 1 tick = 100 ns
    long ticks = ((duration.toNanos() + 50) % 1000000000) / 100;

    String left = days == 0 ? "" : f("%d.", days);
    String right = ticks == 0 ? "" : f(".%07d", ticks);
    return new JsonPrimitive(f("%s%s%s", left, middle, right));
}

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

License:Apache License

public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context) {
    int index = -1;
    for (int i = 0; i < values.length; i++) {
        if (values[i] == src) {
            index = i;//from w  w w  .j  a v  a 2 s.c  o  m
        }
    }

    return new JsonPrimitive(index);
}

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

License:Apache License

@Override
public JsonElement serialize(IName src, Type typeOfSrc, JsonSerializationContext context) {
    return new JsonPrimitive(NameSerialization.serialize(src));
}

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

License:Open Source License

@Override
public JsonElement serialize(final File src, final Type typeOfSrc, final JsonSerializationContext context) {
    return new JsonPrimitive(src.toString());
}

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

License:Open Source License

@Override
public JsonElement serialize(final ICoReName src, final Type typeOfSrc,
        final JsonSerializationContext context) {
    return new JsonPrimitive(src.getIdentifier());
}