Created
August 25, 2017 03:31
-
-
Save genderquery/a3628b70a20ead67bf637475446dd6ab 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.github.genderquery; | |
import android.util.JsonReader; | |
import android.util.JsonToken; | |
import java.io.Closeable; | |
import java.io.IOException; | |
import java.io.Reader; | |
/** | |
* A thin wrapper around {@link JsonReader} that adds null-tolerant versions of nextX() will return | |
* null if the JSON token is null. | |
*/ | |
public class NullTolerantJsonReader implements Closeable { | |
private JsonReader mJsonReader; | |
public NullTolerantJsonReader(Reader in) { | |
mJsonReader = new JsonReader(in); | |
} | |
public void setLenient(boolean lenient) { | |
mJsonReader.setLenient(lenient); | |
} | |
public boolean isLenient() { | |
return mJsonReader.isLenient(); | |
} | |
public void beginArray() throws IOException { | |
mJsonReader.beginArray(); | |
} | |
public void endArray() throws IOException { | |
mJsonReader.endArray(); | |
} | |
public void beginObject() throws IOException { | |
mJsonReader.beginObject(); | |
} | |
public void endObject() throws IOException { | |
mJsonReader.endObject(); | |
} | |
public boolean hasNext() throws IOException { | |
return mJsonReader.hasNext(); | |
} | |
public JsonToken peek() throws IOException { | |
return mJsonReader.peek(); | |
} | |
public String nextName() throws IOException { | |
return mJsonReader.nextName(); | |
} | |
public String nextString() throws IOException { | |
return mJsonReader.nextString(); | |
} | |
public String nextStringOrNull() throws IOException { | |
if (mJsonReader.peek() == JsonToken.NULL) { | |
return null; | |
} | |
return mJsonReader.nextString(); | |
} | |
public boolean nextBoolean() throws IOException { | |
return mJsonReader.nextBoolean(); | |
} | |
public Boolean nextBooleanOrNull() throws IOException { | |
if (mJsonReader.peek() == JsonToken.NULL) { | |
return null; | |
} | |
return mJsonReader.nextBoolean(); | |
} | |
public void nextNull() throws IOException { | |
mJsonReader.nextNull(); | |
} | |
public double nextDouble() throws IOException { | |
return mJsonReader.nextDouble(); | |
} | |
public Double nextDoubleOrNull() throws IOException { | |
if (mJsonReader.peek() == JsonToken.NULL) { | |
return null; | |
} | |
return mJsonReader.nextDouble(); | |
} | |
public long nextLong() throws IOException { | |
return mJsonReader.nextLong(); | |
} | |
public Long nextLongOrNull() throws IOException { | |
if (mJsonReader.peek() == JsonToken.NULL) { | |
return null; | |
} | |
return mJsonReader.nextLong(); | |
} | |
public int nextInt() throws IOException { | |
return mJsonReader.nextInt(); | |
} | |
public Integer nextIntOrNull() throws IOException { | |
if (mJsonReader.peek() == JsonToken.NULL) { | |
return null; | |
} | |
return mJsonReader.nextInt(); | |
} | |
public void close() throws IOException { | |
mJsonReader.close(); | |
} | |
public void skipValue() throws IOException { | |
mJsonReader.skipValue(); | |
} | |
public String toString() { | |
return mJsonReader.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment