Example usage for com.google.gson JsonSerializationContext serialize

List of usage examples for com.google.gson JsonSerializationContext serialize

Introduction

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

Prototype

public JsonElement serialize(Object src);

Source Link

Document

Invokes default serialization on the specified object.

Usage

From source file:allout58.mods.techtree.tree.TechNodeGSON.java

License:Open Source License

@Override
public JsonElement serialize(TechNode src, Type typeOfSrc, JsonSerializationContext context) {
    final JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("id", src.getId());
    jsonObject.addProperty("name", src.getName());
    jsonObject.addProperty("scienceRequired", src.getScienceRequired());
    jsonObject.addProperty("description", src.getDescription());

    final JsonArray jsonParents = new JsonArray();
    for (final TechNode parent : src.getParents()) {
        final JsonPrimitive jsonParent = new JsonPrimitive(parent.getId());
        jsonParents.add(jsonParent);//from www  .  j  a  v a 2s. co  m
    }

    jsonObject.add("parents", jsonParents);

    final JsonArray jsonItems = new JsonArray();
    for (final ItemStack item : src.getLockedItems()) {
        jsonItems.add(context.serialize(item));
    }

    jsonObject.add("lockedItems", jsonItems);

    return jsonObject;
}

From source file:br.com.sgejs.util.DateGsonSerializer.java

@Override
public JsonElement serialize(Date t, Type type, JsonSerializationContext jsc) {
    return jsc.serialize((new SimpleDateFormat("dd/MM/yyyy")).format(t));
}

From source file:ca.ualberta.CMPUT301W15T06.GsonAdapter.java

License:Apache License

@Override
public JsonElement serialize(T src, Type type, JsonSerializationContext context) {
    // TODO Auto-generated method stub
    JsonObject retValue = new JsonObject();
    String className = src.getClass().getCanonicalName();
    retValue.addProperty(CLASSNAME, className);
    JsonElement elem = context.serialize(src);
    retValue.add(INSTANCE, elem);// w  ww. j  a  v a  2 s .  c o m
    return retValue;
}

From source file:ch.ethz.inf.vs.hypermedia.corehal.FormListDeserializer.java

License:Open Source License

@Override
public JsonElement serialize(FormList src, Type typeOfSrc, JsonSerializationContext context) {
    Set<Form> values = new HashSet<>(src.values());
    if (values.isEmpty()) {
        return null;
    }//w w  w.jav  a  2 s.  c o  m
    if (values.size() == 1) {
        return OptionalListDeserializer.cleanup(context.serialize(values.iterator().next()));
    }
    return OptionalListDeserializer.cleanup(context.serialize(values));
}

From source file:ch.ethz.inf.vs.hypermedia.corehal.LinkListDeserializer.java

License:Open Source License

@Override
public JsonElement serialize(LinkList src, Type typeOfSrc, JsonSerializationContext context) {
    Set<Link> values = new HashSet<Link>();
    for (Link item : src.linkValues()) {
        values.add(item);//from   w  w  w .  j  av  a 2s  .co  m
    }
    if (values.isEmpty()) {
        return null;
    }
    if (values.size() == 1) {
        return OptionalListDeserializer.cleanup(context.serialize(values.iterator().next()));
    }
    return OptionalListDeserializer.cleanup(context.serialize(values));
}

From source file:ch.ethz.inf.vs.hypermedia.corehal.OptionalListDeserializer.java

License:Open Source License

@Override
public JsonElement serialize(OptionalList values, Type typeOfSrc, JsonSerializationContext context) {
    if (values.isEmpty()) {
        return null;
    }//from  www  .j a  v  a2  s  .co m
    if (values.size() == 1) {
        return cleanup(context.serialize(values.iterator().next()));
    }
    return cleanup(context.serialize(values, ArrayList.class));
}

From source file:cmput301.f13t01.elasticsearch.InterfaceAdapter.java

License:GNU General Public License

/**
 * This allows for the serialization of objects that use an interface to be
 * wrapped and retain memory of its type.
 * //from   w ww .j a  v a 2  s.  c o m
 * @return Returns the JsonElement to be used by Gson
 */
public JsonElement serialize(T object, Type interfaceType, JsonSerializationContext context) {
    final JsonObject wrapper = new JsonObject();
    wrapper.addProperty("type", object.getClass().getName());
    wrapper.add("data", context.serialize(object));
    return wrapper;
}

From source file:co.aurasphere.botmill.fb.internal.util.json.CalendarSerializer.java

License:Open Source License

/**
 * Serializes a Calendar using the Facebook date format (YYYY-MM-DDThh:mm).
 *
 * @param src/*  ww w  . j  av  a2 s .c  o m*/
 *            the src
 * @param typeOfSrc
 *            the type of src
 * @param context
 *            the context
 * @return the json element
 */
public JsonElement serialize(Calendar src, Type typeOfSrc, JsonSerializationContext context) {
    int year = src.get(Calendar.YEAR);
    String month = this.formatter.format(Double.valueOf(src.get(Calendar.MONTH) + 1));
    String day = this.formatter.format(Double.valueOf(src.get(Calendar.DAY_OF_MONTH)));
    String hour = this.formatter.format(Double.valueOf(src.get(Calendar.HOUR_OF_DAY)));
    String minute = this.formatter.format(Double.valueOf(src.get(Calendar.MINUTE)));
    String formattedDate = year + "-" + month + "-" + day + "T" + hour + ":" + minute;

    return context.serialize(formattedDate);
}

From source file:co.aurasphere.botmill.fb.internal.util.json.EnumLowercaseSerializer.java

License:Open Source License

/**
 * Serializes an Enum as its lowercase name.
 *
 * @param src//from  w  w w.java2s  .  c o  m
 *            the src
 * @param typeOfSrc
 *            the type of src
 * @param context
 *            the context
 * @return the json element
 */
public JsonElement serialize(Enum<?> src, Type typeOfSrc, JsonSerializationContext context) {

    // Ignore this serializer for enums of class PaymentType.
    if (src.getDeclaringClass().equals(PaymentType.class)) {
        return context.serialize(src.name());
    }

    return context.serialize(src.name().toLowerCase());
}

From source file:co.aurasphere.botmill.kik.util.json.EnumLowercaseSerializer.java

License:Open Source License

public JsonElement serialize(Enum<?> src, Type typeOfSrc, JsonSerializationContext context) {
    //lower case and convert "_" into '-';
    String source = src.name().replace('_', '-');
    return context.serialize(source.toLowerCase());
}