2010-09-02 02:15:23 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 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;
|
|
|
|
|
2023-01-17 16:59:10 +01:00
|
|
|
import static com.google.common.truth.Truth.assertThat;
|
2022-12-22 15:04:16 +01:00
|
|
|
import static org.junit.Assert.fail;
|
|
|
|
|
2010-09-03 09:24:59 +02:00
|
|
|
import com.google.gson.reflect.TypeToken;
|
2010-09-02 02:15:23 +02:00
|
|
|
import com.google.gson.stream.JsonReader;
|
|
|
|
import com.google.gson.stream.JsonWriter;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.StringReader;
|
|
|
|
import java.io.StringWriter;
|
2010-09-03 09:24:59 +02:00
|
|
|
import java.lang.reflect.Type;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
2022-12-22 15:04:16 +01:00
|
|
|
import org.junit.Test;
|
2010-09-02 02:15:23 +02:00
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
public final class MixedStreamTest {
|
2010-09-02 02:15:23 +02:00
|
|
|
|
|
|
|
private static final Car BLUE_MUSTANG = new Car("mustang", 0x0000FF);
|
|
|
|
private static final Car BLACK_BMW = new Car("bmw", 0x000000);
|
|
|
|
private static final Car RED_MIATA = new Car("miata", 0xFF0000);
|
|
|
|
private static final String CARS_JSON = "[\n"
|
|
|
|
+ " {\n"
|
|
|
|
+ " \"name\": \"mustang\",\n"
|
|
|
|
+ " \"color\": 255\n"
|
|
|
|
+ " },\n"
|
|
|
|
+ " {\n"
|
|
|
|
+ " \"name\": \"bmw\",\n"
|
|
|
|
+ " \"color\": 0\n"
|
|
|
|
+ " },\n"
|
|
|
|
+ " {\n"
|
|
|
|
+ " \"name\": \"miata\",\n"
|
|
|
|
+ " \"color\": 16711680\n"
|
|
|
|
+ " }\n"
|
|
|
|
+ "]";
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-09-02 02:15:23 +02:00
|
|
|
public void testWriteMixedStreamed() throws IOException {
|
|
|
|
Gson gson = new Gson();
|
|
|
|
StringWriter stringWriter = new StringWriter();
|
|
|
|
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
|
|
|
|
|
|
|
jsonWriter.beginArray();
|
|
|
|
jsonWriter.setIndent(" ");
|
|
|
|
gson.toJson(BLUE_MUSTANG, Car.class, jsonWriter);
|
|
|
|
gson.toJson(BLACK_BMW, Car.class, jsonWriter);
|
|
|
|
gson.toJson(RED_MIATA, Car.class, jsonWriter);
|
|
|
|
jsonWriter.endArray();
|
|
|
|
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(stringWriter.toString()).isEqualTo(CARS_JSON);
|
2010-09-02 02:15:23 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-09-02 02:15:23 +02:00
|
|
|
public void testReadMixedStreamed() throws IOException {
|
|
|
|
Gson gson = new Gson();
|
|
|
|
StringReader stringReader = new StringReader(CARS_JSON);
|
|
|
|
JsonReader jsonReader = new JsonReader(stringReader);
|
|
|
|
|
|
|
|
jsonReader.beginArray();
|
2023-01-17 16:59:10 +01:00
|
|
|
// actual and expected object are inverted in the test.
|
|
|
|
// gson.fromJson(jsonReader, Car.class) as arg of assertThat() cause an ambiguous method call
|
|
|
|
assertThat(BLUE_MUSTANG).isEqualTo(gson.fromJson(jsonReader, Car.class));
|
|
|
|
assertThat(BLACK_BMW).isEqualTo(gson.fromJson(jsonReader, Car.class));
|
|
|
|
assertThat(RED_MIATA).isEqualTo(gson.fromJson(jsonReader, Car.class));
|
2010-09-02 02:15:23 +02:00
|
|
|
jsonReader.endArray();
|
|
|
|
}
|
2011-09-11 09:04:56 +02:00
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-09-03 09:24:59 +02:00
|
|
|
public void testReaderDoesNotMutateState() throws IOException {
|
|
|
|
Gson gson = new Gson();
|
|
|
|
JsonReader jsonReader = new JsonReader(new StringReader(CARS_JSON));
|
|
|
|
jsonReader.beginArray();
|
|
|
|
|
|
|
|
jsonReader.setLenient(false);
|
|
|
|
gson.fromJson(jsonReader, Car.class);
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(jsonReader.isLenient()).isFalse();
|
2010-09-03 09:24:59 +02:00
|
|
|
|
|
|
|
jsonReader.setLenient(true);
|
|
|
|
gson.fromJson(jsonReader, Car.class);
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(jsonReader.isLenient()).isTrue();
|
2010-09-03 09:24:59 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-09-03 09:24:59 +02:00
|
|
|
public void testWriteDoesNotMutateState() throws IOException {
|
|
|
|
Gson gson = new Gson();
|
|
|
|
JsonWriter jsonWriter = new JsonWriter(new StringWriter());
|
|
|
|
jsonWriter.beginArray();
|
|
|
|
|
|
|
|
jsonWriter.setHtmlSafe(true);
|
|
|
|
jsonWriter.setLenient(true);
|
|
|
|
gson.toJson(BLUE_MUSTANG, Car.class, jsonWriter);
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(jsonWriter.isHtmlSafe()).isTrue();
|
|
|
|
assertThat(jsonWriter.isLenient()).isTrue();
|
2010-09-03 09:24:59 +02:00
|
|
|
|
|
|
|
jsonWriter.setHtmlSafe(false);
|
|
|
|
jsonWriter.setLenient(false);
|
|
|
|
gson.toJson(BLUE_MUSTANG, Car.class, jsonWriter);
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(jsonWriter.isHtmlSafe()).isFalse();
|
|
|
|
assertThat(jsonWriter.isLenient()).isFalse();
|
2010-09-03 09:24:59 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-09-03 09:24:59 +02:00
|
|
|
public void testReadInvalidState() throws IOException {
|
|
|
|
Gson gson = new Gson();
|
|
|
|
JsonReader jsonReader = new JsonReader(new StringReader(CARS_JSON));
|
|
|
|
jsonReader.beginArray();
|
|
|
|
jsonReader.beginObject();
|
|
|
|
try {
|
|
|
|
gson.fromJson(jsonReader, String.class);
|
|
|
|
fail();
|
2011-10-24 07:03:06 +02:00
|
|
|
} catch (JsonParseException expected) {
|
2010-09-03 09:24:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-09-03 09:24:59 +02:00
|
|
|
public void testReadClosed() throws IOException {
|
|
|
|
Gson gson = new Gson();
|
|
|
|
JsonReader jsonReader = new JsonReader(new StringReader(CARS_JSON));
|
|
|
|
jsonReader.close();
|
|
|
|
try {
|
|
|
|
gson.fromJson(jsonReader, new TypeToken<List<Car>>() {}.getType());
|
|
|
|
fail();
|
2011-10-24 07:03:06 +02:00
|
|
|
} catch (JsonParseException expected) {
|
2010-09-03 09:24:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-09-03 09:24:59 +02:00
|
|
|
public void testWriteInvalidState() throws IOException {
|
|
|
|
Gson gson = new Gson();
|
|
|
|
JsonWriter jsonWriter = new JsonWriter(new StringWriter());
|
|
|
|
jsonWriter.beginObject();
|
|
|
|
try {
|
|
|
|
gson.toJson(BLUE_MUSTANG, Car.class, jsonWriter);
|
|
|
|
fail();
|
|
|
|
} catch (IllegalStateException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-09-03 09:24:59 +02:00
|
|
|
public void testWriteClosed() throws IOException {
|
|
|
|
Gson gson = new Gson();
|
|
|
|
JsonWriter jsonWriter = new JsonWriter(new StringWriter());
|
|
|
|
jsonWriter.beginArray();
|
|
|
|
jsonWriter.endArray();
|
|
|
|
jsonWriter.close();
|
|
|
|
try {
|
|
|
|
gson.toJson(BLUE_MUSTANG, Car.class, jsonWriter);
|
|
|
|
fail();
|
|
|
|
} catch (IllegalStateException expected) {
|
|
|
|
}
|
|
|
|
}
|
2011-09-11 09:04:56 +02:00
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-09-03 09:24:59 +02:00
|
|
|
public void testWriteNulls() {
|
|
|
|
Gson gson = new Gson();
|
|
|
|
try {
|
|
|
|
gson.toJson(new JsonPrimitive("hello"), (JsonWriter) null);
|
|
|
|
fail();
|
|
|
|
} catch (NullPointerException expected) {
|
|
|
|
}
|
|
|
|
|
|
|
|
StringWriter stringWriter = new StringWriter();
|
|
|
|
gson.toJson(null, new JsonWriter(stringWriter));
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(stringWriter.toString()).isEqualTo("null");
|
2010-09-03 09:24:59 +02:00
|
|
|
}
|
2011-09-11 09:04:56 +02:00
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-09-03 09:24:59 +02:00
|
|
|
public void testReadNulls() {
|
|
|
|
Gson gson = new Gson();
|
|
|
|
try {
|
|
|
|
gson.fromJson((JsonReader) null, Integer.class);
|
|
|
|
fail();
|
|
|
|
} catch (NullPointerException expected) {
|
|
|
|
}
|
|
|
|
try {
|
Add Gson.fromJson(..., TypeToken) overloads (#1700)
* Add Gson.fromJson(..., TypeToken) overloads
Previously only Gson.fromJson(..., Type) existed which is however not
type-safe since the generic type parameter T used for the return type is
not bound.
Since these methods are often used in the form
gson.fromJson(..., new TypeToken<...>(){}.getType())
this commit now adds overloads which accept a TypeToken and are therefore
more type-safe.
Additional changes:
- Fixed some grammar mistakes
- Added javadoc @see tags
- Consistently write "JSON" in uppercase
- More precise placement of @SuppressWarnings("unchecked")
* Add to Gson.fromJson javadoc that JSON is fully consumed
The newly added documentation deliberately does not state which exception
is thrown because Gson.assertFullConsumption could throw either a
JsonIOException or a JsonSyntaxException.
* Remove unnecessary wrapping and unwrapping as TypeToken in Gson.fromJson
Since the actual implementation of Gson.fromJson is TypeToken based, the
TypeToken variant overloads are now the "main" implementation and the other
overloads delegate to them.
Previously the Type variant overloads were the "main" implementation which
caused `TypeToken.getType()` followed by `TypeToken.get(...)` when the
TypeToken variant overloads were used.
* Trim source code whitespaces
* Fix Gson.fromJson(JsonReader, Class) not casting read Object
To be consistent with the other Gson.fromJson(..., Class) overloads the
method should cast the result.
* Replace User Guide link in Gson documentation
* Remove more references to fromJson(..., Type)
* Extend documentation for fromJson(JsonReader, ...)
* Replace some TypeToken.getType() usages
* Address feedback; improve documentation
* Remove fromJson(JsonReader, Class) again
As noticed during review adding this method is source incompatible.
2022-09-19 15:47:11 +02:00
|
|
|
gson.fromJson(new JsonReader(new StringReader("true")), (Type) null);
|
2010-09-03 09:24:59 +02:00
|
|
|
fail();
|
2011-09-11 09:04:56 +02:00
|
|
|
} catch (NullPointerException expected) {
|
2010-09-03 09:24:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-09-03 09:24:59 +02:00
|
|
|
public void testWriteHtmlSafe() {
|
|
|
|
List<String> contents = Arrays.asList("<", ">", "&", "=", "'");
|
|
|
|
Type type = new TypeToken<List<String>>() {}.getType();
|
|
|
|
|
|
|
|
StringWriter writer = new StringWriter();
|
|
|
|
new Gson().toJson(contents, type, new JsonWriter(writer));
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(writer.toString())
|
|
|
|
.isEqualTo("[\"\\u003c\",\"\\u003e\",\"\\u0026\",\"\\u003d\",\"\\u0027\"]");
|
2010-09-03 09:24:59 +02:00
|
|
|
|
|
|
|
writer = new StringWriter();
|
|
|
|
new GsonBuilder().disableHtmlEscaping().create()
|
|
|
|
.toJson(contents, type, new JsonWriter(writer));
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(writer.toString())
|
|
|
|
.isEqualTo("[\"<\",\">\",\"&\",\"=\",\"'\"]");
|
2010-09-03 09:24:59 +02:00
|
|
|
}
|
2011-09-11 09:04:56 +02:00
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-09-03 09:24:59 +02:00
|
|
|
public void testWriteLenient() {
|
|
|
|
List<Double> doubles = Arrays.asList(Double.NaN, Double.NEGATIVE_INFINITY,
|
|
|
|
Double.POSITIVE_INFINITY, -0.0d, 0.5d, 0.0d);
|
|
|
|
Type type = new TypeToken<List<Double>>() {}.getType();
|
|
|
|
|
|
|
|
StringWriter writer = new StringWriter();
|
|
|
|
JsonWriter jsonWriter = new JsonWriter(writer);
|
2022-05-17 21:20:10 +02:00
|
|
|
new GsonBuilder().setLenient().serializeSpecialFloatingPointValues().create()
|
2010-09-03 09:24:59 +02:00
|
|
|
.toJson(doubles, type, jsonWriter);
|
2023-01-17 16:59:10 +01:00
|
|
|
assertThat(writer.toString()).isEqualTo("[NaN,-Infinity,Infinity,-0.0,0.5,0.0]");
|
2010-09-03 09:24:59 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
new Gson().toJson(doubles, type, new JsonWriter(new StringWriter()));
|
|
|
|
fail();
|
|
|
|
} catch (IllegalArgumentException expected) {
|
|
|
|
}
|
|
|
|
}
|
2010-09-02 02:15:23 +02:00
|
|
|
|
|
|
|
static final class Car {
|
|
|
|
String name;
|
|
|
|
int color;
|
|
|
|
|
|
|
|
Car(String name, int color) {
|
|
|
|
this.name = name;
|
|
|
|
this.color = color;
|
|
|
|
}
|
|
|
|
|
2010-09-28 15:42:43 +02:00
|
|
|
// used by Gson
|
2010-09-03 09:24:59 +02:00
|
|
|
Car() {}
|
2010-09-02 02:15:23 +02:00
|
|
|
|
|
|
|
@Override public int hashCode() {
|
|
|
|
return name.hashCode() ^ color;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override public boolean equals(Object o) {
|
|
|
|
return o instanceof Car
|
|
|
|
&& ((Car) o).name.equals(name)
|
|
|
|
&& ((Car) o).color == color;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|