2016-02-25 22:56:42 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
package com.google.gson.regression;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.Objects;
|
|
|
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
import com.google.gson.JsonParser;
|
|
|
|
import com.google.gson.JsonPrimitive;
|
|
|
|
import com.google.gson.JsonSyntaxException;
|
|
|
|
import com.google.gson.TypeAdapter;
|
|
|
|
import com.google.gson.TypeAdapterFactory;
|
|
|
|
import com.google.gson.annotations.JsonAdapter;
|
|
|
|
import com.google.gson.reflect.TypeToken;
|
|
|
|
import com.google.gson.stream.JsonReader;
|
|
|
|
import com.google.gson.stream.JsonWriter;
|
|
|
|
|
|
|
|
import junit.framework.TestCase;
|
|
|
|
|
|
|
|
public class JsonAdapterNullSafeTest extends TestCase {
|
2016-02-26 02:38:48 +01:00
|
|
|
// The recursiveCall in {@link Device.JsonAdapterFactory} is the source of this bug
|
|
|
|
// because it returns a null type adapter.
|
2016-02-25 22:56:42 +01:00
|
|
|
|
2016-02-26 02:38:48 +01:00
|
|
|
private final Gson gson = new Gson();
|
2016-02-25 22:56:42 +01:00
|
|
|
|
2016-02-26 02:38:48 +01:00
|
|
|
public void testNullSafeBugSerialize() throws Exception {
|
|
|
|
Device device = new Device("ec57803e", 2);
|
|
|
|
gson.toJson(device);
|
2016-02-25 22:56:42 +01:00
|
|
|
}
|
|
|
|
|
2016-02-26 02:38:48 +01:00
|
|
|
public void testNullSafeBugDeserialize() throws Exception {
|
|
|
|
String json = "\"{\\\"id\\\":\\\"ec57803e2\\\",\\\"category\\\":2}\"";
|
|
|
|
Device device = gson.fromJson(json, Device.class);
|
|
|
|
assertEquals("ec57803e2", device.id);
|
|
|
|
assertEquals(2, device.category);
|
2016-02-25 22:56:42 +01:00
|
|
|
}
|
|
|
|
|
2016-02-26 02:38:48 +01:00
|
|
|
@JsonAdapter(Device.JsonAdapterFactory.class)
|
|
|
|
private static final class Device {
|
2016-02-25 22:56:42 +01:00
|
|
|
String id;
|
|
|
|
int category;
|
2016-02-26 02:38:48 +01:00
|
|
|
Device(String id, int category) {
|
2016-02-25 22:56:42 +01:00
|
|
|
this.id = id;
|
|
|
|
this.category = category;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-02-26 02:38:48 +01:00
|
|
|
* Write the value as a String, not JSON.
|
2016-02-25 22:56:42 +01:00
|
|
|
*/
|
2016-02-26 02:38:48 +01:00
|
|
|
static final class JsonAdapterFactory implements TypeAdapterFactory {
|
2016-02-25 22:56:42 +01:00
|
|
|
private static final ThreadLocal<Boolean> recursiveCall = new ThreadLocal<Boolean>();
|
|
|
|
|
2016-02-26 02:38:48 +01:00
|
|
|
@SuppressWarnings({"unchecked", "rawtypes"})
|
|
|
|
@Override public <T> TypeAdapter<T> create(final Gson gson, TypeToken<T> type) {
|
|
|
|
if (type.getRawType() != Device.class || recursiveCall.get() != null) {
|
|
|
|
recursiveCall.set(null); // clear for subsequent use
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
recursiveCall.set(Boolean.TRUE);
|
|
|
|
final TypeAdapter<Device> delegate = (TypeAdapter) gson.getDelegateAdapter(this, type);
|
|
|
|
return (TypeAdapter) new TypeAdapter<Device>() {
|
|
|
|
@Override public void write(JsonWriter out, Device value) throws IOException {
|
2016-02-25 22:56:42 +01:00
|
|
|
delegate.write(out, value);
|
|
|
|
}
|
2016-02-26 02:38:48 +01:00
|
|
|
@Override public Device read(JsonReader in) throws IOException {
|
2016-02-25 22:56:42 +01:00
|
|
|
String json = in.nextString();
|
|
|
|
JsonParser parser = new JsonParser();
|
2016-02-26 02:38:48 +01:00
|
|
|
JsonElement root = parseString(parser, json, null);
|
2016-02-25 22:56:42 +01:00
|
|
|
return root == null ? null : delegate.fromJsonTree(root);
|
|
|
|
}
|
2016-02-26 02:38:48 +01:00
|
|
|
private JsonElement parseString(JsonParser parser, String json, String prevJson)
|
|
|
|
throws IOException { // called recursively
|
|
|
|
JsonElement root = parser.parse(json);
|
|
|
|
if (root instanceof JsonPrimitive) {
|
|
|
|
prevJson = json;
|
|
|
|
json = root.getAsString();
|
|
|
|
if (Objects.equals(json, prevJson)) {
|
|
|
|
throw new JsonSyntaxException("Unexpected Json: " + json);
|
|
|
|
}
|
|
|
|
return parseString(parser, json, prevJson);
|
|
|
|
}
|
|
|
|
return root;
|
2016-02-25 22:56:42 +01:00
|
|
|
}
|
2016-02-26 02:38:48 +01:00
|
|
|
};
|
|
|
|
}
|
2016-02-25 22:56:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|