diff options
author | Kevin Sawicki | 2011-10-17 19:23:21 +0000 |
---|---|---|
committer | Kevin Sawicki | 2011-10-17 20:14:50 +0000 |
commit | e9517c94d1aec5c6520dc3fdf638f2baf917cc4e (patch) | |
tree | b76ea966ffc49d75aefda8881f9dbab12627c625 /org.eclipse.egit.github.core | |
parent | c927907616939d512446f06cc1d7876687935259 (diff) | |
download | egit-github-e9517c94d1aec5c6520dc3fdf638f2baf917cc4e.tar.gz egit-github-e9517c94d1aec5c6520dc3fdf638f2baf917cc4e.tar.xz egit-github-e9517c94d1aec5c6520dc3fdf638f2baf917cc4e.zip |
Add serialize null configuration to GitHubClient.
This allows applications to configure whether or not
null fields in model classes should be serialized when
transformed to JSON.
This setting can be used to update only specific resource
fields without first requiring a request for the entire resource
model to preserve existing field values.
Change-Id: I2677417dceba947deafdde9b949955140180c3f0
Signed-off-by: Kevin Sawicki <kevin@github.com>
Diffstat (limited to 'org.eclipse.egit.github.core')
-rw-r--r-- | org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/GitHubClient.java | 13 | ||||
-rw-r--r-- | org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/GsonUtils.java | 53 |
2 files changed, 58 insertions, 8 deletions
diff --git a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/GitHubClient.java b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/GitHubClient.java index 15f89446..3a5de6da 100644 --- a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/GitHubClient.java +++ b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/GitHubClient.java @@ -101,7 +101,7 @@ public class GitHubClient { private Header userAgent = USER_AGENT; - private final Gson gson = GsonUtils.getGson(); + private Gson gson = GsonUtils.getGson(); /** * Create default client @@ -154,6 +154,17 @@ public class GitHubClient { } /** + * Set whether or not serialized data should include fields that are null. + * + * @param serializeNulls + * @return this client + */ + public GitHubClient setSerializeNulls(boolean serializeNulls) { + gson = GsonUtils.getGson(serializeNulls); + return this; + } + + /** * Set the value to set as the user agent header on every request created. * Specifying a null or empty agent parameter will reset this client to use * the default user agent header value. diff --git a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/GsonUtils.java b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/GsonUtils.java index 5031c735..ad233077 100644 --- a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/GsonUtils.java +++ b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/GsonUtils.java @@ -24,7 +24,9 @@ import java.util.Date; */ public abstract class GsonUtils { - private static final Gson GSON = createGson(); + private static final Gson GSON = createGson(true); + + private static final Gson GSON_NO_NULLS = createGson(false); /** * Create the standard {@link Gson} configuration @@ -32,10 +34,24 @@ public abstract class GsonUtils { * @return created gson, never null */ public static final Gson createGson() { - return new GsonBuilder() - .registerTypeAdapter(Date.class, new DateFormatter()) - .setFieldNamingPolicy(LOWER_CASE_WITH_UNDERSCORES) - .serializeNulls().create(); + return createGson(true); + } + + /** + * Create the standard {@link Gson} configuration + * + * @param serializeNulls + * whether nulls should be serialized + * + * @return created gson, never null + */ + public static final Gson createGson(final boolean serializeNulls) { + final GsonBuilder builder = new GsonBuilder(); + builder.registerTypeAdapter(Date.class, new DateFormatter()); + builder.setFieldNamingPolicy(LOWER_CASE_WITH_UNDERSCORES); + if (serializeNulls) + builder.serializeNulls(); + return builder.create(); } /** @@ -48,13 +64,36 @@ public abstract class GsonUtils { } /** + * Get reusable pre-configured {@link Gson} instance + * + * @param serializeNulls + * @return Gson instance + */ + public static final Gson getGson(final boolean serializeNulls) { + return serializeNulls ? GSON : GSON_NO_NULLS; + } + + /** + * Convert object to json + * + * @param object + * @return json string + */ + public static final String toJson(final Object object) { + return toJson(object, true); + } + + /** * Convert object to json * * @param object + * @param includeNulls * @return json string */ - public static final String toJson(Object object) { - return GSON.toJson(object); + public static final String toJson(final Object object, + final boolean includeNulls) { + return includeNulls ? GSON.toJson(object) : GSON_NO_NULLS + .toJson(object); } /** |