Skip to content

Instantly share code, notes, and snippets.

@SergejIsbrecht
Created November 7, 2016 17:59
Show Gist options
  • Save SergejIsbrecht/95aa503b599ded21710f06c03b2a3025 to your computer and use it in GitHub Desktop.
Save SergejIsbrecht/95aa503b599ded21710f06c03b2a3025 to your computer and use it in GitHub Desktop.
package com.example.sergej.rxjavatestapp;
import com.google.gson.JsonSyntaxException;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
public class DoubleTypeAdapter extends TypeAdapter<Number> {
@Override
public void write(JsonWriter out, Number value)
throws IOException {
out.value(value);
}
@Override
public Number read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;
}
try {
String result = in.nextString();
if ("".equals(result) || " ".equals(result)) {
return null;
}
return Double.parseDouble(result);
} catch (NumberFormatException e) {
throw new JsonSyntaxException(e);
}
}
}
Gson gson = new GsonBuilder()
.registerTypeAdapter(double.class, new DoubleTypeAdapter())
.registerTypeAdapter(Double.class, new DoubleTypeAdapter()).create();
GsonConverterFactory gsonConverterFactory = GsonConverterFactory.create(gson);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment