Created
November 7, 2016 17:59
-
-
Save SergejIsbrecht/95aa503b599ded21710f06c03b2a3025 to your computer and use it in GitHub Desktop.
This file contains 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.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); | |
} | |
} | |
} |
This file contains 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
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