Example usage for com.google.gson GsonBuilder create

List of usage examples for com.google.gson GsonBuilder create

Introduction

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

Prototype

public Gson create() 

Source Link

Document

Creates a Gson instance based on the current configuration.

Usage

From source file:com.hkm.taxicallandroid.schema.ConfirmCall.java

public static ConfirmCall parse(final String data) {
    GsonBuilder gb = new GsonBuilder();
    Gson gs = gb.create();
    return gs.fromJson(data, ConfirmCall.class);
}

From source file:com.hkm.taxicallandroid.schema.ConfirmCall.java

public String consolidate() {
    final callconfirm cf = new callconfirm(_id);
    final GsonBuilder gsonb = new GsonBuilder();
    String request_body = "";
    Gson gson = gsonb.create();
    request_body = gson.toJson(cf);/* www  .  j a  va  2 s  .  c  o  m*/
    return request_body;
}

From source file:com.hkm.taxicallandroid.schema.Order_status.java

public static Order_status parse(final String data) {
    final GsonBuilder gsonb = new GsonBuilder();
    final Gson gson = gsonb.create();
    return gson.fromJson(data, Order_status.class);
}

From source file:com.hmwg.utils.GSONUtils.java

License:Apache License

/**
 *  {@code JSON} ??<strong>?? {@code JavaBean}
 * </strong>/*from   www  . j a  v a2  s.c  o  m*/
 * 
 * @param <T>
 *            ??
 * @param json
 *             {@code JSON} 
 * @param clazz
 *            ??
 * @param datePattern
 *            ??
 * @return  {@code JSON} 
 * @since 1.0
 */
public static <T> T fromJson(String json, Class<T> clazz, String datePattern) {
    if (isBlankString(json)) {
        return null;
    }
    GsonBuilder builder = new GsonBuilder();
    if (isBlankString(datePattern)) {
        datePattern = DEFAULT_DATE_PATTERN;
    }
    builder.setDateFormat(datePattern);
    Gson gson = builder.create();
    try {
        return gson.fromJson(json, clazz);
    } catch (Exception ex) {
        Log.i("ws", json + " ? " + clazz.getName() + " !" + ex.getMessage());
        return null;
    }
}

From source file:com.hmwg.utils.GSONUtils.java

License:Apache License

/**
 * ?{@code GsonBuilder} ???? {@code JSON} ?
 * <p />/*from  w  ww  .j  ava  2s.  c  o  m*/
 * ????{@code JavaBean}  <code>"{}"</code>
 * ? <code>"[]"</code> 
 * 
 * @param target
 *            
 * @param targetType
 *            
 * @param builder
 *            ?{@code Gson} 
 * @return  {@code JSON} ?
 * @since 1.1
 */
public static String toJson(Object target, Type targetType, GsonBuilder builder) {
    if (target == null) {
        return EMPTY_JSON;
    }
    Gson gson = null;
    if (builder == null) {
        gson = new Gson();
    } else {
        gson = builder.create();
    }
    String result = EMPTY_JSON;
    try {
        if (targetType == null) {
            result = gson.toJson(target);
        } else {
            result = gson.toJson(target, targetType);
        }
    } catch (Exception ex) {
        Log.i("ws", " " + target.getClass().getName()
                + " ? JSON ??" + ex.getMessage());
        if (target instanceof Collection<?> || target instanceof Iterator<?> || target instanceof Enumeration<?>
                || target.getClass().isArray()) {
            result = EMPTY_JSON_ARRAY;
        }
    }
    return result;
}

From source file:com.hybris.datahub.outbound.utils.CommonUtils.java

License:Open Source License

/**
 * @param whetherExposeAnnotation/* w ww  . j a v  a  2  s .c  o  m*/
 * @param dateFormat
 * @return Gson instance
 */
public static Gson getGsonByBuilder(final boolean whetherExposeAnnotation, String dateFormat) {
    final GsonBuilder gsonBuilder = new GsonBuilder();
    if (whetherExposeAnnotation) {
        gsonBuilder.excludeFieldsWithoutExposeAnnotation();
    }

    gsonBuilder.enableComplexMapKeySerialization();

    if (StringUtils.isEmpty(dateFormat)) {
        dateFormat = "yyyy-MM-dd HH:mm:ss";
    }
    gsonBuilder.serializeNulls().setDateFormat(dateFormat);
    gsonBuilder.setVersion(1.0);
    //      gsonBuilder.disableHtmlEscaping();
    return gsonBuilder.create();
}

From source file:com.hyperaware.conference.eventmobi.parser.gson.GsonParser.java

License:Open Source License

@Override
public T parse(InputStream is) throws ParseException {
    try {/*w w  w  . jav a  2  s  .c  om*/
        final GsonBuilder gb = new GsonBuilder();
        // Eventmobi serializes booleans as integer strings!  Yuck!
        final BooleanDeserializer bs = new BooleanDeserializer();
        gb.registerTypeAdapter(boolean.class, bs);
        gb.registerTypeAdapter(Boolean.class, bs);
        final Gson gson = gb.create();
        final T response = gson.fromJson(new InputStreamReader(is, GsonParserConstants.CHARSET), clazz);
        if (response != null) {
            return response;
        } else {
            throw new ParseException("Reader at EOF");
        }
    } catch (Exception e) {
        throw new ParseException(e);
    }
}

From source file:com.ibm.common.activitystreams.internal.GsonWrapper.java

License:Apache License

/**
 * Constructor for GsonWrapper./*from ww w  .  j a  v a 2s  . c om*/
 * @param builder Builder
 */
protected GsonWrapper(Builder builder) {
    Schema schema = builder.schema != null ? builder.schema : Schema.make().get();
    ASObjectAdapter base = new ASObjectAdapter(schema);
    GsonBuilder b = initGsonBuilder(builder, schema, base, builder.adapters.build());
    if (builder.pretty)
        b.setPrettyPrinting();
    this.gson = b.create();
    this.charset = builder.charset;
}

From source file:com.ibm.csync.internals.websocket.CSTransport.java

License:Open Source License

public CSTransport(WebSocketConnection webSocketConnection) {
    this.socketConnection = webSocketConnection;

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(CSValue.class, new CSValueDeserializer());
    gson = gsonBuilder.create();
}

From source file:com.ibm.g11n.pipeline.client.impl.ServiceClientImpl.java

License:Open Source License

/**
 * Creates a new Gson object/*  w  w w.  ja  v a  2 s.  c om*/
 * 
 * @param className A class name used for serialization/deserialization.
 *                  <p>Note: This implementation does not use this argument
 *                  for now. If we need different kinds of type adapters
 *                  depending on class, the implementation might be updated
 *                  to set up appropriate set of type adapters.
 * @return  A Gson object
 */
private static Gson createGson(String className) {
    GsonBuilder builder = new GsonBuilder();

    // ISO8601 date format support
    builder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");

    builder.registerTypeAdapter(TranslationStatus.class, new TranslationStatusAdapter());

    builder.registerTypeAdapter(new TypeToken<EnumMap<TranslationStatus, Integer>>() {
    }.getType(), new EnumMapInstanceCreator<TranslationStatus, Integer>(TranslationStatus.class));

    builder.registerTypeAdapterFactory(new NullMapValueTypeAdapterFactory());

    return builder.create();
}