Added methods to convert to JsonElement in TypeAdapter.
Using lenient mode while working with Gson. Handling nulls while invoking legacy Gson type adapters.
This commit is contained in:
parent
566c27cf21
commit
d70fb90ef7
@ -28,6 +28,7 @@ import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.gson.internal.Streams;
|
||||
import com.google.gson.internal.bind.ArrayTypeAdapter;
|
||||
import com.google.gson.internal.bind.CollectionTypeAdapter;
|
||||
import com.google.gson.internal.bind.MiniGson;
|
||||
@ -264,13 +265,7 @@ public final class Gson {
|
||||
public JsonElement toJsonTree(Object src, Type typeOfSrc) {
|
||||
// Serialize 'src' to JSON, then deserialize that to a JSON tree.
|
||||
TypeAdapter adapter = miniGson.getAdapter(TypeToken.get(typeOfSrc));
|
||||
StringWriter writer = new StringWriter();
|
||||
try {
|
||||
adapter.write(writer, src);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return Streams.parse(new JsonReader(new StringReader(writer.toString())));
|
||||
return adapter.toJsonElement(src);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -21,6 +21,7 @@ import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import com.google.gson.internal.Streams;
|
||||
import com.google.gson.internal.bind.MiniGson;
|
||||
import com.google.gson.internal.bind.TypeAdapter;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
@ -61,6 +62,10 @@ final class GsonToMiniGsonTypeAdapter implements TypeAdapter.Factory {
|
||||
// TODO: handle if serializer is null
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
if (value == null) {
|
||||
writer.nullValue();
|
||||
return;
|
||||
}
|
||||
JsonElement element = serializer.serialize(value, typeOfT, createSerializationContext(miniGson));
|
||||
Streams.write(element, serializeNulls, writer);
|
||||
}
|
||||
@ -71,15 +76,8 @@ final class GsonToMiniGsonTypeAdapter implements TypeAdapter.Factory {
|
||||
return new JsonSerializationContext() {
|
||||
@Override
|
||||
JsonElement serialize(Object src, Type typeOfSrc, boolean preserveType, boolean defaultOnly) {
|
||||
try {
|
||||
TypeToken typeToken = TypeToken.get(typeOfSrc);
|
||||
String json = miniGson.getAdapter(typeToken).toJson(src);
|
||||
JsonReader jsonReader = new JsonReader(new StringReader(json));
|
||||
jsonReader.setLenient(true);
|
||||
return Streams.parse(jsonReader);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
TypeToken typeToken = TypeToken.get(typeOfSrc);
|
||||
return miniGson.getAdapter(typeToken).toJsonElement(src);
|
||||
}
|
||||
};
|
||||
}
|
||||
@ -87,17 +85,8 @@ final class GsonToMiniGsonTypeAdapter implements TypeAdapter.Factory {
|
||||
return new JsonDeserializationContext() {
|
||||
@Override
|
||||
public <T> T deserialize(JsonElement json, Type typeOfT) throws JsonParseException {
|
||||
try {
|
||||
TypeToken typeToken = TypeToken.get(typeOfT);
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
jsonWriter.setLenient(true);
|
||||
Streams.write(json, serializeNulls, jsonWriter);
|
||||
Object target = miniGson.getAdapter(typeToken).fromJson(stringWriter.toString());
|
||||
return (T) target;
|
||||
} catch (IOException e) {
|
||||
throw new JsonParseException(e);
|
||||
}
|
||||
TypeToken typeToken = TypeToken.get(typeOfT);
|
||||
return (T) miniGson.getAdapter(typeToken).fromJsonElement(json);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
package com.google.gson;
|
||||
|
||||
import com.google.gson.internal.Streams;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
|
@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.google.gson;
|
||||
|
||||
import com.google.gson.internal.Streams;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonToken;
|
||||
import com.google.gson.stream.MalformedJsonException;
|
||||
|
@ -17,6 +17,8 @@
|
||||
package com.google.gson;
|
||||
|
||||
import com.google.gson.internal.$Gson$Preconditions;
|
||||
import com.google.gson.internal.LazilyParsedNumber;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
|
||||
|
@ -22,6 +22,7 @@ import java.io.StringReader;
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import com.google.gson.internal.Streams;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonToken;
|
||||
import com.google.gson.stream.MalformedJsonException;
|
||||
|
@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.google.gson;
|
||||
package com.google.gson.internal;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
@ -23,10 +23,10 @@ import java.math.BigInteger;
|
||||
* @author Inderjeet Singh
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
final class LazilyParsedNumber extends Number {
|
||||
public final class LazilyParsedNumber extends Number {
|
||||
private final String value;
|
||||
|
||||
LazilyParsedNumber(String value) {
|
||||
public LazilyParsedNumber(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
@ -14,8 +14,16 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.google.gson;
|
||||
package com.google.gson.internal;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonIOException;
|
||||
import com.google.gson.JsonNull;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
import com.google.gson.stream.MalformedJsonException;
|
||||
@ -27,12 +35,12 @@ import java.util.Map;
|
||||
/**
|
||||
* Reads and writes GSON parse trees over streams.
|
||||
*/
|
||||
final class Streams {
|
||||
public final class Streams {
|
||||
|
||||
/**
|
||||
* Takes a reader in any state and returns the next value as a JsonElement.
|
||||
*/
|
||||
static JsonElement parse(JsonReader reader) throws JsonParseException {
|
||||
public static JsonElement parse(JsonReader reader) throws JsonParseException {
|
||||
boolean isEmpty = true;
|
||||
try {
|
||||
reader.peek();
|
||||
@ -96,7 +104,7 @@ final class Streams {
|
||||
/**
|
||||
* Writes the JSON element to the writer, recursively.
|
||||
*/
|
||||
static void write(JsonElement element, boolean serializeNulls, JsonWriter writer)
|
||||
public static void write(JsonElement element, boolean serializeNulls, JsonWriter writer)
|
||||
throws IOException {
|
||||
if (element == null || element.isJsonNull()) {
|
||||
if (serializeNulls) {
|
||||
@ -142,7 +150,7 @@ final class Streams {
|
||||
}
|
||||
}
|
||||
|
||||
static Writer writerForAppendable(Appendable appendable) {
|
||||
public static Writer writerForAppendable(Appendable appendable) {
|
||||
return appendable instanceof Writer ? (Writer) appendable : new AppendableWriter(appendable);
|
||||
}
|
||||
|
@ -16,15 +16,18 @@
|
||||
|
||||
package com.google.gson.internal.bind;
|
||||
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.internal.Streams;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
|
||||
public abstract class TypeAdapter<T> {
|
||||
public abstract T read(JsonReader reader) throws IOException;
|
||||
public abstract void write(JsonWriter writer, T value) throws IOException;
|
||||
@ -50,6 +53,34 @@ public abstract class TypeAdapter<T> {
|
||||
return read(reader);
|
||||
}
|
||||
|
||||
public JsonElement toJsonElement(T src) {
|
||||
try {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
jsonWriter.setLenient(true);
|
||||
write(jsonWriter, src);
|
||||
JsonReader reader = new JsonReader(new StringReader(stringWriter.toString()));
|
||||
reader.setLenient(true);
|
||||
return Streams.parse(reader);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public T fromJsonElement(JsonElement json) {
|
||||
try {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
jsonWriter.setLenient(true);
|
||||
Streams.write(json, false, jsonWriter);
|
||||
JsonReader jsonReader = new JsonReader(new StringReader(stringWriter.toString()));
|
||||
jsonReader.setLenient(true);
|
||||
return read(jsonReader);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public interface Factory {
|
||||
<T> TypeAdapter<T> create(MiniGson context, TypeToken<T> type);
|
||||
}
|
||||
|
@ -17,6 +17,7 @@
|
||||
package com.google.gson;
|
||||
|
||||
import com.google.gson.common.TestTypes.BagOfPrimitives;
|
||||
import com.google.gson.internal.Streams;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import java.io.CharArrayReader;
|
||||
import java.io.CharArrayWriter;
|
||||
|
Loading…
Reference in New Issue
Block a user