Example usage for com.google.gson GsonBuilder registerTypeAdapter

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

Introduction

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

Prototype

@SuppressWarnings({ "unchecked", "rawtypes" })
public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) 

Source Link

Document

Configures Gson for custom serialization or deserialization.

Usage

From source file:com.eclipsesource.connect.serialization.GsonFactory.java

License:Open Source License

public static Gson create(boolean prettyPrinting) {
    GsonBuilder builder = new GsonBuilder();
    if (prettyPrinting) {
        builder.setPrettyPrinting();//from   w w w .j a va  2  s.  c o  m
    }
    builder.registerTypeAdapter(Id.class, new IdTypeAdapter());
    Converters.registerAll(builder);
    addFieldNamingStrategy(builder);
    return builder.create();
}

From source file:com.ecwid.mailchimp.internal.gson.MailChimpGsonFactory.java

License:Apache License

/**
 * Creates a new {@link Gson} object.//w ww  .  j  av a 2 s  . co m
 */
public static Gson createGson() {
    GsonBuilder builder = new GsonBuilder();
    builder.setExclusionStrategies(exclusionStrategy);
    builder.registerTypeAdapter(Date.class, new DateTypeAdapter());
    builder.registerTypeAdapterFactory(MailChimpObjectTypeAdapter.FACTORY);
    return builder.create();
}

From source file:com.edduarte.argus.job.JobManager.java

License:Apache License

final boolean responseOk(final String documentUrl, final String clientUrl,
        final Set<DifferenceMatcher.Result> diffs) {
    Map<String, Object> jsonResponseMap = new LinkedHashMap<>();
    jsonResponseMap.put("status", "ok");
    jsonResponseMap.put("url", documentUrl);
    jsonResponseMap.put("diffs", diffs);
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Keyword.class, new KeywordSerializer());
    String input = gsonBuilder.create().toJson(jsonResponseMap);
    return sendResponse(clientUrl, input);
}

From source file:com.edduarte.argus.job.JobManager.java

License:Apache License

final boolean sendTimeoutResponse(final String documentUrl, final String clientUrl) {
    Map<String, Object> jsonResponseMap = new LinkedHashMap<>();
    jsonResponseMap.put("status", "timeout");
    jsonResponseMap.put("url", documentUrl);
    Set<DifferenceMatcher.Result> diffs = Collections.emptySet();
    jsonResponseMap.put("diffs", diffs);
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Keyword.class, new KeywordSerializer());
    String input = gsonBuilder.create().toJson(jsonResponseMap);
    return sendResponse(clientUrl, input);
}

From source file:com.edgenius.wiki.ext.calendar.web.CalendarAction.java

License:Open Source License

/**
 * Browser all events in calendar./*from   ww w  . j a  v  a  2s  . com*/
 */
public String execute() {
    CalendarJson json;
    try {
        Date[] scope = CalendarUtil.getCalendarScope(CalendarConstants.VIEW.valueOf(viewType.toUpperCase()),
                new Date(), weekStartDay);
        List<CalendarEvent> calEvents = calendarService.getEvents(calendarName, pageUuid, scope[0], scope[1]);
        json = CalendarJson.toJson(calEvents, scope[0], scope[1]);
    } catch (Exception e) {
        CalendarErrorJson error = new CalendarErrorJson("001", "Get calendar data failed");
        json = new CalendarJson();
        json.setError(error);
        log.error("Get calendar failed", e);
    }

    try {
        GsonBuilder gsonBuild = new GsonBuilder();
        gsonBuild.registerTypeAdapter(Date.class, new DateSerializer());
        String jsonstr = gsonBuild.create().toJson(json);

        //hack - remove "@1111@" to new Date(1111) format. 
        String[] list = StringUtil.splitWithoutEscaped(jsonstr, "\"@");
        jsonstr = StringUtil.join("new Date(", list);
        jsonstr = jsonstr.replaceAll("@\"", ")");

        getResponse().getOutputStream().write(jsonstr.getBytes(Constants.UTF8));
    } catch (IOException e) {
        log.error("Calendar view get data failed", e);
    }
    return null;
}

From source file:com.epickrram.romero.server.web.RunningJobsRequestHandler.java

License:Apache License

@Override
protected void registerTypeAdapters(final GsonBuilder gsonBuilder) {
    gsonBuilder.registerTypeAdapter(RunningJob.class, new RunningJobTypeAdapter());
}

From source file:com.ericsson.eiffel.remrem.generate.config.GsonHttpMessageConverterConfig.java

License:Apache License

private Gson gson() {
    final GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(Json.class, new SpringfoxJsonToGsonAdapter());
    return builder.create();
}

From source file:com.expensify.testframework.GetNextCommandResultContainer.java

License:Microsoft Public License

public static CommandBase parse(String json) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    CommandDeserializer deserializer = new CommandDeserializer();
    deserializer.registerCommonCommands();
    gsonBuilder.registerTypeAdapter(CommandBase.class, deserializer);
    Gson gson = gsonBuilder.create();/*from   w w  w . j  a  v  a2  s .  c o m*/
    GetNextCommandResultContainer commandContainer = gson.fromJson(json, GetNextCommandResultContainer.class);

    return commandContainer.GetNextCommandResult;
}

From source file:com.exsoloscript.challonge.gson.AdapterSuite.java

License:Apache License

private GsonBuilder createGsonBuilder() {
    GsonBuilder builder = new GsonBuilder();

    for (Map.Entry<Type, GsonAdapter> adapter : typeMappings().entrySet())
        builder.registerTypeAdapter(adapter.getKey(), adapter.getValue());

    return builder;
}

From source file:com.facebook.buck.intellij.plugin.ws.buckevents.BuckEventHandler.java

License:Apache License

public BuckEventHandler(BuckEventsQueueInterface queue, Runnable onConnectHandler,
        Runnable onDisconnectHandler) {

    mOnConnectHandler = onConnectHandler;
    mOnDisconnectHandler = onDisconnectHandler;
    mQueue = queue;// w ww  .j  a  v a  2s.  c o  m

    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(BuckEventInterface.class, new BuckEventAdapter());
    mGson = builder.create();
}