Created
June 8, 2017 12:29
-
-
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;
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
where we are using "AutoValueMoshi_MoshiFactory"
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
Usage:
Configure Moshi instance:
Use in POJO: