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.jobchat.action.MobileAction.java

private void addCompanylicensetype(JSONObject data) throws JSONException {
    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.create();
    String s = data.toString();//  ww  w  . ja  v a 2 s . c  o  m
    JCompanylicensetype en = gson.fromJson(s, new TypeToken<JCompanylicensetype>() {
    }.getType());
    en.setCompanylicensetypeStatus((short) 0);
    String ptime = TextUtility.formatDate(new Date());
    en.setCompanylicensetypeTime(ptime);
    boolean b = companylicensetypeService.addJCompanylicensetype(en);
    Map<String, Object> map = new HashMap<String, Object>();
    int status = 1;
    if (b) {
        status = 0;
        map.put("msg", "");
    } else {
        status = 1;
        map.put("msg", "");
    }
    map.put("ret", status);
    try {
        ResultUtils.toJson(ServletActionContext.getResponse(), map);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.jobchat.action.MobileAction.java

private void upCompanylicensetype(JSONObject data) throws JSONException {
    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.create();
    String s = data.toString();/*w  ww. j ava  2 s .c o m*/
    JCompanylicensetype en = gson.fromJson(s, new TypeToken<JCompanylicensetype>() {
    }.getType());

    String ptime = TextUtility.formatDate(new Date());
    en.setCompanylicensetypeTime(ptime);
    boolean b = companylicensetypeService.updateJCompanylicensetype(en);
    Map<String, Object> map = new HashMap<String, Object>();
    int status = 1;
    if (b) {
        status = 0;
    } else {
        status = 1;
    }
    map.put("ret", status);
    map.put("msg", "");
    try {
        ResultUtils.toJson(ServletActionContext.getResponse(), map);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.jobchat.action.MobileAction.java

private void addKeyword(JSONObject data) throws JSONException {
    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.create();
    String s = data.toString();/*  ww w. j a  v  a2 s  .com*/
    JKeyword en = gson.fromJson(s, new TypeToken<JKeyword>() {
    }.getType());
    en.setStatus(false);
    String ptime = TextUtility.formatDate(new Date());
    en.setPtime(ptime);
    boolean b = keywordService.addJKeyword(en);
    Map<String, Object> map = new HashMap<String, Object>();
    int status = 1;
    if (b) {
        status = 0;
        map.put("msg", "");
    } else {
        status = 1;
        map.put("msg", "");
    }
    map.put("ret", status);
    try {
        ResultUtils.toJson(ServletActionContext.getResponse(), map);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.jobchat.action.MobileAction.java

private void upKeyword(JSONObject data) throws JSONException {
    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.create();
    String s = data.toString();//from   ww  w .j  a  v a 2 s  . co  m
    JKeyword en = gson.fromJson(s, new TypeToken<JKeyword>() {
    }.getType());

    String ptime = TextUtility.formatDate(new Date());
    en.setPtime(ptime);
    boolean b = keywordService.updateJKeyword(en);
    Map<String, Object> map = new HashMap<String, Object>();
    int status = 1;
    if (b) {
        status = 0;
    } else {
        status = 1;
    }
    map.put("ret", status);
    map.put("msg", "");
    try {
        ResultUtils.toJson(ServletActionContext.getResponse(), map);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.jolira.st4j.impl.ServerTrackerImpl.java

License:Open Source License

@Override
public Collection<Map<String, Object>> proxyEvent(final Map<String, Object> eventInfo,
        final InputStream content) throws IllegalArgumentException {
    final GsonBuilder gsonBuilder = new GsonBuilder();

    gsonBuilder.registerTypeAdapter(Object.class, new NaturalDeserializer());

    final Gson parser = gsonBuilder.create();
    final Object _payload = parser.fromJson(new InputStreamReader(content), Object.class);

    LOG.info("proxying {}", _payload);

    if (_payload == null || !(_payload instanceof Map)) {
        throw new IllegalArgumentException("not a map: " + _payload);
    }/*ww w  . j a va2s . c  o  m*/

    @SuppressWarnings("unchecked")
    final Map<String, Object> payload = (Map<String, Object>) _payload;

    @SuppressWarnings("unchecked")
    final Collection<Map<String, Object>> _events = (Collection<Map<String, Object>>) payload.get("events");
    @SuppressWarnings("unchecked")
    final Collection<Map<String, Object>> _logs = (Collection<Map<String, Object>>) payload.get("logs");

    payload.remove("events");
    payload.remove("logs");
    merge(eventInfo, payload);

    if (_logs != null) {
        for (final Map<String, Object> log : _logs) {
            merge(payload, log);
            addLog(log);
        }
    }

    final Collection<Map<String, Object>> result = new ArrayList<Map<String, Object>>(1);

    if (_events != null) {
        for (final Map<String, Object> event : _events) {
            merge(payload, event);
            addEvent(event);
            result.add(event);
        }
    }

    return result;
}

From source file:com.julauncher.workers.InstanceInstaller.java

License:Open Source License

public InstanceInstaller(String instanceName, Pack pack, PackVersion version, boolean isReinstall,
        boolean isServer, String shareCode, boolean showModsChooser) {
    this.instanceName = instanceName;
    this.pack = pack;
    this.version = version;
    this.isReinstall = isReinstall;
    this.isServer = isServer;
    this.shareCode = shareCode;
    this.showModsChooser = showModsChooser;
    if (isServer) {
        serverLibraries = new ArrayList<File>();
    }/*from  www  . j av a 2s .c  om*/
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapterFactory(new EnumTypeAdapterFactory());
    builder.registerTypeAdapter(Date.class, new DateTypeAdapter());
    builder.registerTypeAdapter(File.class, new FileTypeAdapter());
    builder.setPrettyPrinting();
    this.gson = builder.create();
}

From source file:com.k1ngdr3w.supercontacts.DriversLicense.java

/**
 * Get current object representation in JSON string
 *
 * @return Serialized string in JSON/*from w  ww .  j  a  v a2 s  .  c om*/
 */
public String toJson() {
    String json = "";
    GsonBuilder gsonb = new GsonBuilder();
    Gson gson = gsonb.create();

    HashMap jsonHash = new HashMap();
    jsonHash.put("first_name", getFirstName());
    jsonHash.put("last_name", getLastName());
    jsonHash.put("address", getAddress());
    jsonHash.put("city", getCity());
    jsonHash.put("state", getState());
    jsonHash.put("zipcode", getZipCode());
    jsonHash.put("height", getHeight());
    jsonHash.put("dob", formatDate(getDOBCal()));
    json = gson.toJson(jsonHash);
    return json;
}

From source file:com.karlstein.tools.serializer.JsonSerializer.java

License:Apache License

@Override
public <T> T read(Class<T> cls, Reader reader) {
    Gson gson = new Gson();
    GsonBuilder builder = new GsonBuilder();
    builder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
    return builder.create().fromJson(reader, cls);
}

From source file:com.katsu.gson.GsonImpl.java

License:Open Source License

private void createGson() {
    GsonBuilder gsonBuilder = new GsonBuilder().addSerializationExclusionStrategy(new MvcExclusionStrategy());
    if (properties != null) {
        if (properties.containsKey(JsonProperty.DATE_FORMAT.getValue())) {
            gsonBuilder = gsonBuilder// w w w  .  j a  va2  s .c o  m
                    .setDateFormat(properties.getProperty(JsonProperty.DATE_FORMAT.getValue()));
        } else {
            gsonBuilder = gsonBuilder.setDateFormat(Constants.DATE_PATTERN);
        }
        if (!Constants.TRUE.equals(properties.getProperty(JsonProperty.HTML_ESCAPE.getValue()))) {
            gsonBuilder = gsonBuilder.disableHtmlEscaping();
        }
    } else {
        gsonBuilder = gsonBuilder.setDateFormat(Constants.DATE_PATTERN);
    }
    this.gson = gsonBuilder.create();
}

From source file:com.keydap.sparrow.SparrowClient.java

License:Apache License

/**
 * Creates an instance of the client//from  w  w w .j  av  a  2  s .  c o m
 * 
 * @param baseApiUrl the API URL of the SCIM server
 * @param authenticator authenticator instance, optional
 * @param sslCtx the SSL context, mandatory only when the service is accessible over HTTPS
 */
public SparrowClient(String baseApiUrl, String baseOauthUrl, Authenticator authenticator, SSLContext sslCtx) {
    this.baseApiUrl = baseApiUrl;
    this.baseOauthUrl = baseOauthUrl;

    // if authenticator is not given then use a null authenticator
    if (authenticator == null) {
        authenticator = new Authenticator() {
            public void saveHeaders(HttpResponse resp) {
            }

            public void authenticate(String baseUrl, CloseableHttpClient client) throws Exception {
            }

            public void addHeaders(HttpUriRequest req) {
            }
        };
    }

    this.authenticator = authenticator;

    boolean isHttps = baseApiUrl.toLowerCase().startsWith("https");

    builder = HttpClientBuilder.create().useSystemProperties();

    if (isHttps) {
        if (sslCtx == null) {
            LOG.warn(
                    "********************** No SSLContext instance is provided, creating a cstom SSLContext that trusts all certificates **********************");
            try {
                sslCtx = SSLContext.getInstance("TLS");
                sslCtx.init(null, new X509TrustManager[] { new AllowAllTrustManager() }, null);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

            builder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE);
        }

        builder.setSSLContext(sslCtx);
    }

    client = builder.build();

    GsonBuilder gb = new GsonBuilder();

    Type dt = new TypeToken<Date>() {
    }.getType();
    gb.registerTypeAdapter(dt, new DateTimeSerializer());

    serializer = gb.create();
}