Skip to content

Instantly share code, notes, and snippets.

@OleksandrKucherenko
Created June 8, 2017 12:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OleksandrKucherenko/ffb2126d37778b88fca3774f1666ce66 to your computer and use it in GitHub Desktop.
Save OleksandrKucherenko/ffb2126d37778b88fca3774f1666ce66 to your computer and use it in GitHub Desktop.
Some Moshi adapters for solving common issues with JSON serialization: Null replace by specific value, exclude empty fields from json;
package com.artfulbits.json.moshi;
import android.support.annotation.*;
import com.ryanharter.auto.value.moshi.MoshiAdapterFactory;
import com.squareup.moshi.FromJson;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.JsonQualifier;
import com.squareup.moshi.JsonReader;
import com.squareup.moshi.JsonWriter;
import com.squareup.moshi.ToJson;
import java.io.IOException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@MoshiAdapterFactory
public abstract class MoshiFactory implements JsonAdapter.Factory {
/** Static factory method to access the package, private generated implementation */
@NonNull
public static JsonAdapter.Factory create() {
return new AutoValueMoshi_MoshiFactory();
}
@Retention(RetentionPolicy.RUNTIME)
@JsonQualifier
public @interface NullableLong {
long NONE = -1;
}
@Retention(RetentionPolicy.RUNTIME)
@JsonQualifier
public @interface NullableDouble {
double NONE = -1;
}
@Retention(RetentionPolicy.RUNTIME)
@JsonQualifier
public @interface ExcludeIfEmpty {
}
/** @see <a href="https://github.com/square/moshi/issues/114">Null for primitive types</a> */
@SuppressWarnings("unused")
public static class NullableAdapter {
@ToJson
public String toJsonLong(@NullableLong long value) {
return (NullableLong.NONE == value ? null : String.valueOf(value));
}
@FromJson
@NullableLong
public long fromJsonLong(@NonNull final JsonReader reader) throws IOException {
if (reader.peek() == JsonReader.Token.NUMBER) {
return reader.nextLong();
} else if (reader.peek() == JsonReader.Token.NULL) {
reader.nextNull();
}
return NullableLong.NONE;
}
@ToJson
public String toJsonDouble(@NullableDouble double value) {
return (NullableDouble.NONE == value ? null : String.valueOf(value));
}
@FromJson
@NullableDouble
public double fromJsonDouble(@NonNull final JsonReader reader) throws IOException {
if (reader.peek() == JsonReader.Token.NUMBER) {
return reader.nextDouble();
} else if (reader.peek() == JsonReader.Token.NULL) {
reader.nextNull();
}
return NullableDouble.NONE;
}
@ToJson
public void toJsonEmptyDouble(final JsonWriter writer, @ExcludeIfEmpty final double value) throws IOException {
if (value != 0.0) {
writer.value(String.valueOf(value));
} else {
writer.nullValue();
}
}
@FromJson
@ExcludeIfEmpty
public double fromJsonEmptyDouble(@NonNull final JsonReader reader) throws IOException {
return reader.nextDouble();
}
@ToJson
public void toJsonEmptyLong(final JsonWriter writer, @ExcludeIfEmpty final long value) throws IOException {
if (value != 0) {
writer.value(String.valueOf(value));
} else {
writer.nullValue();
}
}
@FromJson
@ExcludeIfEmpty
public long fromJsonEmptyLong(@NonNull final JsonReader reader) throws IOException {
return reader.nextLong();
}
}
}
@OleksandrKucherenko
Copy link
Author

OleksandrKucherenko commented Jun 8, 2017

Usage:

Configure Moshi instance:

    private final Moshi moshi = new Moshi.Builder()
            .add(MoshiFactory.create())
            .add(new MoshiFactory.NullableAdapter())
            .build();

Use in POJO:

public class POJO {
    /** Optional. Should not be used right now by app. */
    @Json(name = "startDate") @MoshiFactory.ExcludeIfEmpty public long startDate;
    /** Instead of NULL set -1.0d; */
    @Json(name = "topUpAmount") @MoshiFactory.NullableDouble public double amount;
    /** Instead of NULL set -1L; */
    @Json(name = "triggerValue") @MoshiFactory.NullableLong public long trigger;
}

@Sekar-Rntbci
Copy link

where we are using "AutoValueMoshi_MoshiFactory"

@OleksandrKucherenko
Copy link
Author

 private final Moshi moshi = new Moshi.Builder()
            .add(MoshiFactory.create())
            .add(new MoshiFactory.NullableAdapter())
            .build();

when we create instance of Moshi we register our Factory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment