2010-08-26 10:44:02 +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.stream;
|
|
|
|
|
2012-08-26 21:31:06 +02:00
|
|
|
import static com.google.gson.stream.JsonToken.BEGIN_ARRAY;
|
|
|
|
import static com.google.gson.stream.JsonToken.BEGIN_OBJECT;
|
|
|
|
import static com.google.gson.stream.JsonToken.BOOLEAN;
|
|
|
|
import static com.google.gson.stream.JsonToken.END_ARRAY;
|
|
|
|
import static com.google.gson.stream.JsonToken.END_OBJECT;
|
|
|
|
import static com.google.gson.stream.JsonToken.NAME;
|
|
|
|
import static com.google.gson.stream.JsonToken.NULL;
|
|
|
|
import static com.google.gson.stream.JsonToken.NUMBER;
|
|
|
|
import static com.google.gson.stream.JsonToken.STRING;
|
2023-01-31 20:20:54 +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-08-26 10:44:02 +02:00
|
|
|
|
2022-10-10 16:51:36 +02:00
|
|
|
import java.io.EOFException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.Reader;
|
|
|
|
import java.io.StringReader;
|
|
|
|
import java.util.Arrays;
|
2023-04-10 19:50:25 +02:00
|
|
|
import com.google.gson.stream.JsonToken;
|
2022-12-22 15:04:16 +01:00
|
|
|
import org.junit.Ignore;
|
|
|
|
import org.junit.Test;
|
2022-10-10 16:51:36 +02:00
|
|
|
|
2013-04-12 22:12:12 +02:00
|
|
|
@SuppressWarnings("resource")
|
2022-12-22 15:04:16 +01:00
|
|
|
public final class JsonReaderTest {
|
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testReadArray() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[true, true]"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextBoolean()).isTrue();
|
|
|
|
assertThat(reader.nextBoolean()).isTrue();
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.endArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
2010-08-26 10:44:02 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testReadEmptyArray() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[]"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.hasNext()).isFalse();
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.endArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
2010-08-26 10:44:02 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testReadObject() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader(
|
2010-08-26 10:44:02 +02:00
|
|
|
"{\"a\": \"android\", \"b\": \"banana\"}"));
|
|
|
|
reader.beginObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextName()).isEqualTo("a");
|
|
|
|
assertThat(reader.nextString()).isEqualTo("android");
|
|
|
|
assertThat(reader.nextName()).isEqualTo("b");
|
|
|
|
assertThat(reader.nextString()).isEqualTo("banana");
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.endObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
2010-08-26 10:44:02 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testReadEmptyObject() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("{}"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.beginObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.hasNext()).isFalse();
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.endObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
2010-08-26 10:44:02 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2022-01-28 00:59:50 +01:00
|
|
|
public void testHasNextEndOfDocument() throws IOException {
|
|
|
|
JsonReader reader = new JsonReader(reader("{}"));
|
|
|
|
reader.beginObject();
|
|
|
|
reader.endObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.hasNext()).isFalse();
|
2022-01-28 00:59:50 +01:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2013-01-14 19:38:46 +01:00
|
|
|
public void testSkipArray() throws IOException {
|
|
|
|
JsonReader reader = new JsonReader(reader(
|
|
|
|
"{\"a\": [\"one\", \"two\", \"three\"], \"b\": 123}"));
|
|
|
|
reader.beginObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextName()).isEqualTo("a");
|
2013-01-14 19:38:46 +01:00
|
|
|
reader.skipValue();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextName()).isEqualTo("b");
|
|
|
|
assertThat(reader.nextInt()).isEqualTo(123);
|
2013-01-14 19:38:46 +01:00
|
|
|
reader.endObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
2013-01-14 19:38:46 +01:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2013-01-14 19:38:46 +01:00
|
|
|
public void testSkipArrayAfterPeek() throws Exception {
|
|
|
|
JsonReader reader = new JsonReader(reader(
|
|
|
|
"{\"a\": [\"one\", \"two\", \"three\"], \"b\": 123}"));
|
|
|
|
reader.beginObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextName()).isEqualTo("a");
|
|
|
|
assertThat(reader.peek()).isEqualTo(BEGIN_ARRAY);
|
2013-01-14 19:38:46 +01:00
|
|
|
reader.skipValue();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextName()).isEqualTo("b");
|
|
|
|
assertThat(reader.nextInt()).isEqualTo(123);
|
2013-01-14 19:38:46 +01:00
|
|
|
reader.endObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
2013-01-14 19:38:46 +01:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2013-01-14 19:38:46 +01:00
|
|
|
public void testSkipTopLevelObject() throws Exception {
|
|
|
|
JsonReader reader = new JsonReader(reader(
|
|
|
|
"{\"a\": [\"one\", \"two\", \"three\"], \"b\": 123}"));
|
|
|
|
reader.skipValue();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
2013-01-14 19:38:46 +01:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testSkipObject() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader(
|
2010-08-26 10:44:02 +02:00
|
|
|
"{\"a\": { \"c\": [], \"d\": [true, true, {}] }, \"b\": \"banana\"}"));
|
|
|
|
reader.beginObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextName()).isEqualTo("a");
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.skipValue();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextName()).isEqualTo("b");
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.skipValue();
|
|
|
|
reader.endObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
2010-08-26 10:44:02 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2013-01-14 19:38:46 +01:00
|
|
|
public void testSkipObjectAfterPeek() throws Exception {
|
|
|
|
String json = "{" + " \"one\": { \"num\": 1 }"
|
|
|
|
+ ", \"two\": { \"num\": 2 }" + ", \"three\": { \"num\": 3 }" + "}";
|
|
|
|
JsonReader reader = new JsonReader(reader(json));
|
|
|
|
reader.beginObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextName()).isEqualTo("one");
|
|
|
|
assertThat(reader.peek()).isEqualTo(BEGIN_OBJECT);
|
2013-01-14 19:38:46 +01:00
|
|
|
reader.skipValue();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextName()).isEqualTo("two");
|
|
|
|
assertThat(reader.peek()).isEqualTo(BEGIN_OBJECT);
|
2013-01-14 19:38:46 +01:00
|
|
|
reader.skipValue();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextName()).isEqualTo("three");
|
2013-01-14 19:38:46 +01:00
|
|
|
reader.skipValue();
|
|
|
|
reader.endObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
2013-01-14 19:38:46 +01:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2022-10-10 16:51:36 +02:00
|
|
|
public void testSkipObjectName() throws IOException {
|
|
|
|
JsonReader reader = new JsonReader(reader("{\"a\": 1}"));
|
|
|
|
reader.beginObject();
|
|
|
|
reader.skipValue();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.NUMBER);
|
|
|
|
assertThat(reader.getPath()).isEqualTo("$.<skipped>");
|
|
|
|
assertThat(reader.nextInt()).isEqualTo(1);
|
2022-10-10 16:51:36 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2022-10-10 16:51:36 +02:00
|
|
|
public void testSkipObjectNameSingleQuoted() throws IOException {
|
|
|
|
JsonReader reader = new JsonReader(reader("{'a': 1}"));
|
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginObject();
|
|
|
|
reader.skipValue();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.NUMBER);
|
|
|
|
assertThat(reader.getPath()).isEqualTo("$.<skipped>");
|
|
|
|
assertThat(reader.nextInt()).isEqualTo(1);
|
2022-10-10 16:51:36 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2022-10-10 16:51:36 +02:00
|
|
|
public void testSkipObjectNameUnquoted() throws IOException {
|
|
|
|
JsonReader reader = new JsonReader(reader("{a: 1}"));
|
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginObject();
|
|
|
|
reader.skipValue();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.NUMBER);
|
|
|
|
assertThat(reader.getPath()).isEqualTo("$.<skipped>");
|
|
|
|
assertThat(reader.nextInt()).isEqualTo(1);
|
2022-10-10 16:51:36 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-08-25 06:31:56 +02:00
|
|
|
public void testSkipInteger() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader(
|
2012-08-25 06:31:56 +02:00
|
|
|
"{\"a\":123456789,\"b\":-123456789}"));
|
|
|
|
reader.beginObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextName()).isEqualTo("a");
|
2012-08-25 06:31:56 +02:00
|
|
|
reader.skipValue();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextName()).isEqualTo("b");
|
2012-08-25 06:31:56 +02:00
|
|
|
reader.skipValue();
|
|
|
|
reader.endObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
2012-08-25 06:31:56 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-08-25 06:31:56 +02:00
|
|
|
public void testSkipDouble() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader(
|
2012-08-25 06:31:56 +02:00
|
|
|
"{\"a\":-123.456e-789,\"b\":123456789.0}"));
|
|
|
|
reader.beginObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextName()).isEqualTo("a");
|
2012-08-25 06:31:56 +02:00
|
|
|
reader.skipValue();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextName()).isEqualTo("b");
|
2012-08-25 06:31:56 +02:00
|
|
|
reader.skipValue();
|
|
|
|
reader.endObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
2012-08-25 06:31:56 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2022-10-10 16:51:36 +02:00
|
|
|
public void testSkipValueAfterEndOfDocument() throws IOException {
|
|
|
|
JsonReader reader = new JsonReader(reader("{}"));
|
|
|
|
reader.beginObject();
|
|
|
|
reader.endObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
2022-10-10 16:51:36 +02:00
|
|
|
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.getPath()).isEqualTo("$");
|
2022-10-10 16:51:36 +02:00
|
|
|
reader.skipValue();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
|
|
|
assertThat(reader.getPath()).isEqualTo("$");
|
2022-10-10 16:51:36 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2022-10-10 16:51:36 +02:00
|
|
|
public void testSkipValueAtArrayEnd() throws IOException {
|
|
|
|
JsonReader reader = new JsonReader(reader("[]"));
|
|
|
|
reader.beginArray();
|
|
|
|
reader.skipValue();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
|
|
|
assertThat(reader.getPath()).isEqualTo("$");
|
2022-10-10 16:51:36 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2022-10-10 16:51:36 +02:00
|
|
|
public void testSkipValueAtObjectEnd() throws IOException {
|
|
|
|
JsonReader reader = new JsonReader(reader("{}"));
|
|
|
|
reader.beginObject();
|
|
|
|
reader.skipValue();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
|
|
|
assertThat(reader.getPath()).isEqualTo("$");
|
2022-10-10 16:51:36 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testHelloWorld() throws IOException {
|
|
|
|
String json = "{\n" +
|
|
|
|
" \"hello\": true,\n" +
|
|
|
|
" \"foo\": [\"world\"]\n" +
|
|
|
|
"}";
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader(json));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.beginObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextName()).isEqualTo("hello");
|
|
|
|
assertThat(reader.nextBoolean()).isTrue();
|
|
|
|
assertThat(reader.nextName()).isEqualTo("foo");
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextString()).isEqualTo("world");
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.endArray();
|
|
|
|
reader.endObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
2010-08-26 10:44:02 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2016-05-23 09:47:02 +02:00
|
|
|
public void testInvalidJsonInput() throws IOException {
|
|
|
|
String json = "{\n"
|
|
|
|
+ " \"h\\ello\": true,\n"
|
|
|
|
+ " \"foo\": [\"world\"]\n"
|
|
|
|
+ "}";
|
|
|
|
|
|
|
|
JsonReader reader = new JsonReader(reader(json));
|
|
|
|
reader.beginObject();
|
|
|
|
try {
|
|
|
|
reader.nextName();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
}
|
2022-01-28 20:26:28 +01:00
|
|
|
|
2019-10-04 00:49:25 +02:00
|
|
|
@SuppressWarnings("unused")
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testNulls() {
|
|
|
|
try {
|
|
|
|
new JsonReader(null);
|
|
|
|
fail();
|
|
|
|
} catch (NullPointerException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-11-04 00:14:42 +01:00
|
|
|
public void testEmptyString() {
|
2010-08-26 10:44:02 +02:00
|
|
|
try {
|
2012-08-28 03:48:25 +02:00
|
|
|
new JsonReader(reader("")).beginArray();
|
2014-08-12 23:06:56 +02:00
|
|
|
fail();
|
2010-08-26 10:44:02 +02:00
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
try {
|
2012-08-28 03:48:25 +02:00
|
|
|
new JsonReader(reader("")).beginObject();
|
2014-08-12 23:06:56 +02:00
|
|
|
fail();
|
2010-08-26 10:44:02 +02:00
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testCharacterUnescaping() throws IOException {
|
|
|
|
String json = "[\"a\","
|
|
|
|
+ "\"a\\\"\","
|
|
|
|
+ "\"\\\"\","
|
|
|
|
+ "\":\","
|
|
|
|
+ "\",\","
|
|
|
|
+ "\"\\b\","
|
|
|
|
+ "\"\\f\","
|
|
|
|
+ "\"\\n\","
|
|
|
|
+ "\"\\r\","
|
|
|
|
+ "\"\\t\","
|
|
|
|
+ "\" \","
|
|
|
|
+ "\"\\\\\","
|
|
|
|
+ "\"{\","
|
|
|
|
+ "\"}\","
|
|
|
|
+ "\"[\","
|
|
|
|
+ "\"]\","
|
|
|
|
+ "\"\\u0000\","
|
|
|
|
+ "\"\\u0019\","
|
|
|
|
+ "\"\\u20AC\""
|
|
|
|
+ "]";
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader(json));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextString()).isEqualTo("a");
|
|
|
|
assertThat(reader.nextString()).isEqualTo("a\"");
|
|
|
|
assertThat(reader.nextString()).isEqualTo("\"");
|
|
|
|
assertThat(reader.nextString()).isEqualTo(":");
|
|
|
|
assertThat(reader.nextString()).isEqualTo(",");
|
|
|
|
assertThat(reader.nextString()).isEqualTo("\b");
|
|
|
|
assertThat(reader.nextString()).isEqualTo("\f");
|
|
|
|
assertThat(reader.nextString()).isEqualTo("\n");
|
|
|
|
assertThat(reader.nextString()).isEqualTo("\r");
|
|
|
|
assertThat(reader.nextString()).isEqualTo("\t");
|
|
|
|
assertThat(reader.nextString()).isEqualTo(" ");
|
|
|
|
assertThat(reader.nextString()).isEqualTo("\\");
|
|
|
|
assertThat(reader.nextString()).isEqualTo("{");
|
|
|
|
assertThat(reader.nextString()).isEqualTo("}");
|
|
|
|
assertThat(reader.nextString()).isEqualTo("[");
|
|
|
|
assertThat(reader.nextString()).isEqualTo("]");
|
|
|
|
assertThat(reader.nextString()).isEqualTo("\0");
|
|
|
|
assertThat(reader.nextString()).isEqualTo("\u0019");
|
|
|
|
assertThat(reader.nextString()).isEqualTo("\u20AC");
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.endArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
2010-08-26 10:44:02 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2011-12-25 08:09:46 +01:00
|
|
|
public void testUnescapingInvalidCharacters() throws IOException {
|
|
|
|
String json = "[\"\\u000g\"]";
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader(json));
|
2011-12-25 08:09:46 +01:00
|
|
|
reader.beginArray();
|
|
|
|
try {
|
|
|
|
reader.nextString();
|
|
|
|
fail();
|
2023-03-06 17:24:09 +01:00
|
|
|
} catch (MalformedJsonException expected) {
|
2011-12-25 08:09:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2011-12-25 08:09:46 +01:00
|
|
|
public void testUnescapingTruncatedCharacters() throws IOException {
|
|
|
|
String json = "[\"\\u000";
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader(json));
|
2011-12-25 08:09:46 +01:00
|
|
|
reader.beginArray();
|
|
|
|
try {
|
|
|
|
reader.nextString();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-02-11 21:15:39 +01:00
|
|
|
public void testUnescapingTruncatedSequence() throws IOException {
|
|
|
|
String json = "[\"\\";
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader(json));
|
2012-02-11 21:15:39 +01:00
|
|
|
reader.beginArray();
|
|
|
|
try {
|
|
|
|
reader.nextString();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testIntegersWithFractionalPartSpecified() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[1.0,1.0,1.0]"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextDouble()).isEqualTo(1.0);
|
|
|
|
assertThat(reader.nextInt()).isEqualTo(1);
|
|
|
|
assertThat(reader.nextLong()).isEqualTo(1L);
|
2010-08-26 10:44:02 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testDoubles() throws IOException {
|
|
|
|
String json = "[-0.0,"
|
|
|
|
+ "1.0,"
|
|
|
|
+ "1.7976931348623157E308,"
|
|
|
|
+ "4.9E-324,"
|
|
|
|
+ "0.0,"
|
2022-01-28 20:26:28 +01:00
|
|
|
+ "0.00,"
|
2010-08-26 10:44:02 +02:00
|
|
|
+ "-0.5,"
|
|
|
|
+ "2.2250738585072014E-308,"
|
|
|
|
+ "3.141592653589793,"
|
2022-01-28 20:26:28 +01:00
|
|
|
+ "2.718281828459045,"
|
|
|
|
+ "0,"
|
|
|
|
+ "0.01,"
|
|
|
|
+ "0e0,"
|
|
|
|
+ "1e+0,"
|
|
|
|
+ "1e-0,"
|
|
|
|
+ "1e0000," // leading 0 is allowed for exponent
|
|
|
|
+ "1e00001,"
|
|
|
|
+ "1e+1]";
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader(json));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextDouble()).isEqualTo(-0.0);
|
|
|
|
assertThat(reader.nextDouble()).isEqualTo(1.0);
|
|
|
|
assertThat(reader.nextDouble()).isEqualTo(1.7976931348623157E308);
|
|
|
|
assertThat(reader.nextDouble()).isEqualTo(4.9E-324);
|
|
|
|
assertThat(reader.nextDouble()).isEqualTo(0.0);
|
|
|
|
assertThat(reader.nextDouble()).isEqualTo(0.0);
|
|
|
|
assertThat(reader.nextDouble()).isEqualTo(-0.5);
|
|
|
|
assertThat(reader.nextDouble()).isEqualTo(2.2250738585072014E-308);
|
|
|
|
assertThat(reader.nextDouble()).isEqualTo(3.141592653589793);
|
|
|
|
assertThat(reader.nextDouble()).isEqualTo(2.718281828459045);
|
|
|
|
assertThat(reader.nextDouble()).isEqualTo(0.0);
|
|
|
|
assertThat(reader.nextDouble()).isEqualTo(0.01);
|
|
|
|
assertThat(reader.nextDouble()).isEqualTo(0.0);
|
|
|
|
assertThat(reader.nextDouble()).isEqualTo(1.0);
|
|
|
|
assertThat(reader.nextDouble()).isEqualTo(1.0);
|
|
|
|
assertThat(reader.nextDouble()).isEqualTo(1.0);
|
|
|
|
assertThat(reader.nextDouble()).isEqualTo(10.0);
|
|
|
|
assertThat(reader.nextDouble()).isEqualTo(10.0);
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.endArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
2010-08-26 10:44:02 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testStrictNonFiniteDoubles() throws IOException {
|
|
|
|
String json = "[NaN]";
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader(json));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.beginArray();
|
|
|
|
try {
|
|
|
|
reader.nextDouble();
|
|
|
|
fail();
|
2011-11-27 17:50:45 +01:00
|
|
|
} catch (MalformedJsonException expected) {
|
2010-08-26 10:44:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-02-11 21:15:39 +01:00
|
|
|
public void testStrictQuotedNonFiniteDoubles() throws IOException {
|
|
|
|
String json = "[\"NaN\"]";
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader(json));
|
2012-02-11 21:15:39 +01:00
|
|
|
reader.beginArray();
|
|
|
|
try {
|
|
|
|
reader.nextDouble();
|
|
|
|
fail();
|
|
|
|
} catch (MalformedJsonException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testLenientNonFiniteDoubles() throws IOException {
|
|
|
|
String json = "[NaN, -Infinity, Infinity]";
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader(json));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(Double.isNaN(reader.nextDouble())).isTrue();
|
|
|
|
assertThat(reader.nextDouble()).isEqualTo(Double.NEGATIVE_INFINITY);
|
|
|
|
assertThat(reader.nextDouble()).isEqualTo(Double.POSITIVE_INFINITY);
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.endArray();
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-02-11 21:15:39 +01:00
|
|
|
public void testLenientQuotedNonFiniteDoubles() throws IOException {
|
|
|
|
String json = "[\"NaN\", \"-Infinity\", \"Infinity\"]";
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader(json));
|
2012-02-11 21:15:39 +01:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(Double.isNaN(reader.nextDouble())).isTrue();
|
|
|
|
assertThat(reader.nextDouble()).isEqualTo(Double.NEGATIVE_INFINITY);
|
|
|
|
assertThat(reader.nextDouble()).isEqualTo(Double.POSITIVE_INFINITY);
|
2012-02-11 21:15:39 +01:00
|
|
|
reader.endArray();
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2011-12-21 22:43:55 +01:00
|
|
|
public void testStrictNonFiniteDoublesWithSkipValue() throws IOException {
|
|
|
|
String json = "[NaN]";
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader(json));
|
2011-12-21 22:43:55 +01:00
|
|
|
reader.beginArray();
|
|
|
|
try {
|
|
|
|
reader.skipValue();
|
|
|
|
fail();
|
|
|
|
} catch (MalformedJsonException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testLongs() throws IOException {
|
|
|
|
String json = "[0,0,0,"
|
|
|
|
+ "1,1,1,"
|
|
|
|
+ "-1,-1,-1,"
|
|
|
|
+ "-9223372036854775808,"
|
|
|
|
+ "9223372036854775807]";
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader(json));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextLong()).isEqualTo(0L);
|
|
|
|
assertThat(reader.nextInt()).isEqualTo(0);
|
|
|
|
assertThat(reader.nextDouble()).isEqualTo(0.0);
|
|
|
|
assertThat(reader.nextLong()).isEqualTo(1L);
|
|
|
|
assertThat(reader.nextInt()).isEqualTo(1);
|
|
|
|
assertThat(reader.nextDouble()).isEqualTo(1.0);
|
|
|
|
assertThat(reader.nextLong()).isEqualTo(-1L);
|
|
|
|
assertThat(reader.nextInt()).isEqualTo(-1);
|
|
|
|
assertThat(reader.nextDouble()).isEqualTo(-1.0);
|
2010-08-26 10:44:02 +02:00
|
|
|
try {
|
|
|
|
reader.nextInt();
|
|
|
|
fail();
|
|
|
|
} catch (NumberFormatException expected) {
|
|
|
|
}
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextLong()).isEqualTo(Long.MIN_VALUE);
|
2010-08-26 10:44:02 +02:00
|
|
|
try {
|
|
|
|
reader.nextInt();
|
|
|
|
fail();
|
|
|
|
} catch (NumberFormatException expected) {
|
|
|
|
}
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextLong()).isEqualTo(Long.MAX_VALUE);
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.endArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
2010-08-26 10:44:02 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
|
|
|
@Ignore
|
2012-09-02 22:12:19 +02:00
|
|
|
public void disabled_testNumberWithOctalPrefix() throws IOException {
|
2010-08-26 10:44:02 +02:00
|
|
|
String json = "[01]";
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader(json));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.beginArray();
|
2011-11-27 17:50:45 +01:00
|
|
|
try {
|
|
|
|
reader.peek();
|
|
|
|
fail();
|
|
|
|
} catch (MalformedJsonException expected) {
|
|
|
|
}
|
2010-08-26 10:44:02 +02:00
|
|
|
try {
|
|
|
|
reader.nextInt();
|
|
|
|
fail();
|
2011-11-27 17:50:45 +01:00
|
|
|
} catch (MalformedJsonException expected) {
|
2010-08-26 10:44:02 +02:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
reader.nextLong();
|
|
|
|
fail();
|
2011-11-27 17:50:45 +01:00
|
|
|
} catch (MalformedJsonException expected) {
|
2010-08-26 10:44:02 +02:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
reader.nextDouble();
|
|
|
|
fail();
|
2011-11-27 17:50:45 +01:00
|
|
|
} catch (MalformedJsonException expected) {
|
2010-08-26 10:44:02 +02:00
|
|
|
}
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextString()).isEqualTo("01");
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.endArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
2010-08-26 10:44:02 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testBooleans() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[true,false]"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextBoolean()).isTrue();
|
|
|
|
assertThat(reader.nextBoolean()).isFalse();
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.endArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
2010-08-26 10:44:02 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-08-25 06:31:56 +02:00
|
|
|
public void testPeekingUnquotedStringsPrefixedWithBooleans() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[truey]"));
|
2012-08-25 06:31:56 +02:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(STRING);
|
2012-08-25 06:31:56 +02:00
|
|
|
try {
|
|
|
|
reader.nextBoolean();
|
|
|
|
fail();
|
|
|
|
} catch (IllegalStateException expected) {
|
|
|
|
}
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextString()).isEqualTo("truey");
|
2012-08-25 06:31:56 +02:00
|
|
|
reader.endArray();
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-09-02 22:12:19 +02:00
|
|
|
public void testMalformedNumbers() throws IOException {
|
|
|
|
assertNotANumber("-");
|
|
|
|
assertNotANumber(".");
|
|
|
|
|
2022-01-28 20:26:28 +01:00
|
|
|
// plus sign is not allowed for integer part
|
|
|
|
assertNotANumber("+1");
|
|
|
|
|
|
|
|
// leading 0 is not allowed for integer part
|
|
|
|
assertNotANumber("00");
|
|
|
|
assertNotANumber("01");
|
|
|
|
|
2012-09-02 22:12:19 +02:00
|
|
|
// exponent lacks digit
|
|
|
|
assertNotANumber("e");
|
|
|
|
assertNotANumber("0e");
|
|
|
|
assertNotANumber(".e");
|
|
|
|
assertNotANumber("0.e");
|
|
|
|
assertNotANumber("-.0e");
|
|
|
|
|
|
|
|
// no integer
|
|
|
|
assertNotANumber("e1");
|
|
|
|
assertNotANumber(".e1");
|
|
|
|
assertNotANumber("-e1");
|
|
|
|
|
|
|
|
// trailing characters
|
|
|
|
assertNotANumber("1x");
|
|
|
|
assertNotANumber("1.1x");
|
|
|
|
assertNotANumber("1e1x");
|
|
|
|
assertNotANumber("1ex");
|
|
|
|
assertNotANumber("1.1ex");
|
|
|
|
assertNotANumber("1.1e1x");
|
|
|
|
|
|
|
|
// fraction has no digit
|
|
|
|
assertNotANumber("0.");
|
|
|
|
assertNotANumber("-0.");
|
|
|
|
assertNotANumber("0.e1");
|
|
|
|
assertNotANumber("-0.e1");
|
|
|
|
|
|
|
|
// no leading digit
|
|
|
|
assertNotANumber(".0");
|
|
|
|
assertNotANumber("-.0");
|
|
|
|
assertNotANumber(".0e1");
|
|
|
|
assertNotANumber("-.0e1");
|
|
|
|
}
|
|
|
|
|
|
|
|
private void assertNotANumber(String s) throws IOException {
|
2022-01-28 20:26:28 +01:00
|
|
|
JsonReader reader = new JsonReader(reader(s));
|
2012-09-02 22:12:19 +02:00
|
|
|
reader.setLenient(true);
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.STRING);
|
|
|
|
assertThat(reader.nextString()).isEqualTo(s);
|
2022-01-28 20:26:28 +01:00
|
|
|
|
|
|
|
JsonReader strictReader = new JsonReader(reader(s));
|
|
|
|
try {
|
|
|
|
strictReader.nextDouble();
|
|
|
|
fail("Should have failed reading " + s + " as double");
|
|
|
|
} catch (MalformedJsonException e) {
|
2023-03-01 23:23:27 +01:00
|
|
|
assertThat(e).hasMessageThat().startsWith("Use JsonReader.setLenient(true) to accept malformed JSON");
|
2022-01-28 20:26:28 +01:00
|
|
|
}
|
2012-09-02 22:12:19 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-08-25 06:31:56 +02:00
|
|
|
public void testPeekingUnquotedStringsPrefixedWithIntegers() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[12.34e5x]"));
|
2012-08-25 06:31:56 +02:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(STRING);
|
2012-08-25 06:31:56 +02:00
|
|
|
try {
|
|
|
|
reader.nextInt();
|
|
|
|
fail();
|
2016-03-12 19:52:13 +01:00
|
|
|
} catch (NumberFormatException expected) {
|
2012-08-25 06:31:56 +02:00
|
|
|
}
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextString()).isEqualTo("12.34e5x");
|
2012-08-25 06:31:56 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-08-25 06:31:56 +02:00
|
|
|
public void testPeekLongMinValue() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[-9223372036854775808]"));
|
2012-08-25 06:31:56 +02:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(NUMBER);
|
|
|
|
assertThat(reader.nextLong()).isEqualTo(-9223372036854775808L);
|
2012-08-25 06:31:56 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-08-27 04:34:52 +02:00
|
|
|
public void testPeekLongMaxValue() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[9223372036854775807]"));
|
2012-08-27 04:34:52 +02:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(NUMBER);
|
|
|
|
assertThat(reader.nextLong()).isEqualTo(9223372036854775807L);
|
2012-08-27 04:34:52 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-08-27 04:34:52 +02:00
|
|
|
public void testLongLargerThanMaxLongThatWrapsAround() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[22233720368547758070]"));
|
2012-08-27 04:34:52 +02:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(NUMBER);
|
2012-08-27 04:34:52 +02:00
|
|
|
try {
|
|
|
|
reader.nextLong();
|
|
|
|
fail();
|
|
|
|
} catch (NumberFormatException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-08-27 04:34:52 +02:00
|
|
|
public void testLongLargerThanMinLongThatWrapsAround() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[-22233720368547758070]"));
|
2012-08-27 04:34:52 +02:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(NUMBER);
|
2012-08-27 04:34:52 +02:00
|
|
|
try {
|
|
|
|
reader.nextLong();
|
|
|
|
fail();
|
|
|
|
} catch (NumberFormatException expected) {
|
|
|
|
}
|
|
|
|
}
|
2022-01-28 20:26:28 +01:00
|
|
|
|
2017-04-19 23:26:36 +02:00
|
|
|
/**
|
|
|
|
* Issue 1053, negative zero.
|
|
|
|
*/
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2017-04-19 23:26:36 +02:00
|
|
|
public void testNegativeZero() throws Exception {
|
2022-01-28 20:26:28 +01:00
|
|
|
JsonReader reader = new JsonReader(reader("[-0]"));
|
|
|
|
reader.setLenient(false);
|
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(NUMBER);
|
|
|
|
assertThat(reader.nextString()).isEqualTo("-0");
|
2017-04-19 23:26:36 +02:00
|
|
|
}
|
2012-08-27 04:34:52 +02:00
|
|
|
|
2012-08-27 05:17:41 +02:00
|
|
|
/**
|
|
|
|
* This test fails because there's no double for 9223372036854775808, and our
|
|
|
|
* long parsing uses Double.parseDouble() for fractional values.
|
|
|
|
*/
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
|
|
|
@Ignore
|
2012-09-02 22:12:19 +02:00
|
|
|
public void disabled_testPeekLargerThanLongMaxValue() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[9223372036854775808]"));
|
2012-08-27 05:17:41 +02:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(NUMBER);
|
2012-08-27 05:17:41 +02:00
|
|
|
try {
|
|
|
|
reader.nextLong();
|
|
|
|
fail();
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-26 06:02:09 +02:00
|
|
|
/**
|
|
|
|
* This test fails because there's no double for -9223372036854775809, and our
|
|
|
|
* long parsing uses Double.parseDouble() for fractional values.
|
|
|
|
*/
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
|
|
|
@Ignore
|
2012-09-02 22:12:19 +02:00
|
|
|
public void disabled_testPeekLargerThanLongMinValue() throws IOException {
|
2023-03-01 23:23:27 +01:00
|
|
|
@SuppressWarnings("FloatingPointLiteralPrecision")
|
|
|
|
double d = -9223372036854775809d;
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[-9223372036854775809]"));
|
2012-08-26 06:02:09 +02:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(NUMBER);
|
2012-08-26 06:02:09 +02:00
|
|
|
try {
|
|
|
|
reader.nextLong();
|
|
|
|
fail();
|
|
|
|
} catch (NumberFormatException expected) {
|
|
|
|
}
|
2023-03-01 23:23:27 +01:00
|
|
|
assertThat(reader.nextDouble()).isEqualTo(d);
|
2012-08-26 06:02:09 +02:00
|
|
|
}
|
|
|
|
|
2012-08-27 00:06:57 +02:00
|
|
|
/**
|
|
|
|
* This test fails because there's no double for 9223372036854775806, and
|
|
|
|
* our long parsing uses Double.parseDouble() for fractional values.
|
|
|
|
*/
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
|
|
|
@Ignore
|
2012-08-27 00:06:57 +02:00
|
|
|
public void disabled_testHighPrecisionLong() throws IOException {
|
|
|
|
String json = "[9223372036854775806.000]";
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader(json));
|
2012-08-27 00:06:57 +02:00
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextLong()).isEqualTo(9223372036854775806L);
|
2012-08-27 00:06:57 +02:00
|
|
|
reader.endArray();
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-08-26 06:02:09 +02:00
|
|
|
public void testPeekMuchLargerThanLongMinValue() throws IOException {
|
2023-03-01 23:23:27 +01:00
|
|
|
@SuppressWarnings("FloatingPointLiteralPrecision")
|
|
|
|
double d = -92233720368547758080d;
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[-92233720368547758080]"));
|
2012-08-25 06:31:56 +02:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(NUMBER);
|
2012-08-25 06:31:56 +02:00
|
|
|
try {
|
|
|
|
reader.nextLong();
|
|
|
|
fail();
|
|
|
|
} catch (NumberFormatException expected) {
|
|
|
|
}
|
2023-03-01 23:23:27 +01:00
|
|
|
assertThat(reader.nextDouble()).isEqualTo(d);
|
2012-08-25 06:31:56 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-08-25 06:31:56 +02:00
|
|
|
public void testQuotedNumberWithEscape() throws IOException {
|
2023-03-01 23:23:27 +01:00
|
|
|
JsonReader reader = new JsonReader(reader("[\"12\\u00334\"]"));
|
2012-08-25 06:31:56 +02:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(STRING);
|
|
|
|
assertThat(reader.nextInt()).isEqualTo(1234);
|
2012-08-25 06:31:56 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testMixedCaseLiterals() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[True,TruE,False,FALSE,NULL,nulL]"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextBoolean()).isTrue();
|
|
|
|
assertThat(reader.nextBoolean()).isTrue();
|
|
|
|
assertThat(reader.nextBoolean()).isFalse();
|
|
|
|
assertThat(reader.nextBoolean()).isFalse();
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.nextNull();
|
|
|
|
reader.nextNull();
|
|
|
|
reader.endArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
2010-08-26 10:44:02 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testMissingValue() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("{\"a\":}"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.beginObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextName()).isEqualTo("a");
|
2010-08-26 10:44:02 +02:00
|
|
|
try {
|
|
|
|
reader.nextString();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testPrematureEndOfInput() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("{\"a\":true,"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.beginObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextName()).isEqualTo("a");
|
|
|
|
assertThat(reader.nextBoolean()).isTrue();
|
2010-08-26 10:44:02 +02:00
|
|
|
try {
|
|
|
|
reader.nextName();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testPrematurelyClosed() throws IOException {
|
|
|
|
try {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("{\"a\":[]}"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.beginObject();
|
|
|
|
reader.close();
|
|
|
|
reader.nextName();
|
|
|
|
fail();
|
|
|
|
} catch (IllegalStateException expected) {
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("{\"a\":[]}"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.close();
|
|
|
|
reader.beginObject();
|
|
|
|
fail();
|
|
|
|
} catch (IllegalStateException expected) {
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("{\"a\":true}"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.beginObject();
|
2023-04-10 19:50:25 +02:00
|
|
|
String unused1 = reader.nextName();
|
|
|
|
JsonToken unused2 = reader.peek();
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.close();
|
|
|
|
reader.nextBoolean();
|
|
|
|
fail();
|
|
|
|
} catch (IllegalStateException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testNextFailuresDoNotAdvance() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("{\"a\":true}"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.beginObject();
|
|
|
|
try {
|
2023-04-10 19:50:25 +02:00
|
|
|
String unused = reader.nextString();
|
2010-08-26 10:44:02 +02:00
|
|
|
fail();
|
|
|
|
} catch (IllegalStateException expected) {
|
|
|
|
}
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextName()).isEqualTo("a");
|
2010-08-26 10:44:02 +02:00
|
|
|
try {
|
2023-04-10 19:50:25 +02:00
|
|
|
String unused = reader.nextName();
|
2010-08-26 10:44:02 +02:00
|
|
|
fail();
|
|
|
|
} catch (IllegalStateException expected) {
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
reader.beginArray();
|
|
|
|
fail();
|
|
|
|
} catch (IllegalStateException expected) {
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
reader.endArray();
|
|
|
|
fail();
|
|
|
|
} catch (IllegalStateException expected) {
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
reader.beginObject();
|
|
|
|
fail();
|
|
|
|
} catch (IllegalStateException expected) {
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
reader.endObject();
|
|
|
|
fail();
|
|
|
|
} catch (IllegalStateException expected) {
|
|
|
|
}
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextBoolean()).isTrue();
|
2010-08-26 10:44:02 +02:00
|
|
|
try {
|
|
|
|
reader.nextString();
|
|
|
|
fail();
|
|
|
|
} catch (IllegalStateException expected) {
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
reader.nextName();
|
|
|
|
fail();
|
|
|
|
} catch (IllegalStateException expected) {
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
reader.beginArray();
|
|
|
|
fail();
|
|
|
|
} catch (IllegalStateException expected) {
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
reader.endArray();
|
|
|
|
fail();
|
|
|
|
} catch (IllegalStateException expected) {
|
|
|
|
}
|
|
|
|
reader.endObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.close();
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-08-26 06:02:09 +02:00
|
|
|
public void testIntegerMismatchFailuresDoNotAdvance() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[1.5]"));
|
2012-08-26 06:02:09 +02:00
|
|
|
reader.beginArray();
|
|
|
|
try {
|
|
|
|
reader.nextInt();
|
|
|
|
fail();
|
|
|
|
} catch (NumberFormatException expected) {
|
|
|
|
}
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextDouble()).isEqualTo(1.5d);
|
2012-08-26 06:02:09 +02:00
|
|
|
reader.endArray();
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testStringNullIsNotNull() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[\"null\"]"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.beginArray();
|
|
|
|
try {
|
|
|
|
reader.nextNull();
|
|
|
|
fail();
|
|
|
|
} catch (IllegalStateException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testNullLiteralIsNotAString() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[null]"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.beginArray();
|
|
|
|
try {
|
|
|
|
reader.nextString();
|
|
|
|
fail();
|
|
|
|
} catch (IllegalStateException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testStrictNameValueSeparator() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("{\"a\"=true}"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.beginObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextName()).isEqualTo("a");
|
2010-08-26 10:44:02 +02:00
|
|
|
try {
|
|
|
|
reader.nextBoolean();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
|
2012-08-28 03:48:25 +02:00
|
|
|
reader = new JsonReader(reader("{\"a\"=>true}"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.beginObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextName()).isEqualTo("a");
|
2010-08-26 10:44:02 +02:00
|
|
|
try {
|
|
|
|
reader.nextBoolean();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testLenientNameValueSeparator() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("{\"a\"=true}"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextName()).isEqualTo("a");
|
|
|
|
assertThat(reader.nextBoolean()).isTrue();
|
2010-08-26 10:44:02 +02:00
|
|
|
|
2012-08-28 03:48:25 +02:00
|
|
|
reader = new JsonReader(reader("{\"a\"=>true}"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextName()).isEqualTo("a");
|
|
|
|
assertThat(reader.nextBoolean()).isTrue();
|
2010-08-26 10:44:02 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2011-12-21 22:43:55 +01:00
|
|
|
public void testStrictNameValueSeparatorWithSkipValue() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("{\"a\"=true}"));
|
2011-12-21 22:43:55 +01:00
|
|
|
reader.beginObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextName()).isEqualTo("a");
|
2011-12-21 22:43:55 +01:00
|
|
|
try {
|
|
|
|
reader.skipValue();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
|
2012-08-28 03:48:25 +02:00
|
|
|
reader = new JsonReader(reader("{\"a\"=>true}"));
|
2011-12-21 22:43:55 +01:00
|
|
|
reader.beginObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextName()).isEqualTo("a");
|
2011-12-21 22:43:55 +01:00
|
|
|
try {
|
|
|
|
reader.skipValue();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2013-04-14 23:36:47 +02:00
|
|
|
public void testCommentsInStringValue() throws Exception {
|
|
|
|
JsonReader reader = new JsonReader(reader("[\"// comment\"]"));
|
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextString()).isEqualTo("// comment");
|
2013-04-14 23:36:47 +02:00
|
|
|
reader.endArray();
|
|
|
|
|
|
|
|
reader = new JsonReader(reader("{\"a\":\"#someComment\"}"));
|
|
|
|
reader.beginObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextName()).isEqualTo("a");
|
|
|
|
assertThat(reader.nextString()).isEqualTo("#someComment");
|
2013-04-14 23:36:47 +02:00
|
|
|
reader.endObject();
|
|
|
|
|
|
|
|
reader = new JsonReader(reader("{\"#//a\":\"#some //Comment\"}"));
|
|
|
|
reader.beginObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextName()).isEqualTo("#//a");
|
|
|
|
assertThat(reader.nextString()).isEqualTo("#some //Comment");
|
2013-04-14 23:36:47 +02:00
|
|
|
reader.endObject();
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testStrictComments() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[// comment \n true]"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.beginArray();
|
|
|
|
try {
|
|
|
|
reader.nextBoolean();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
|
2012-08-28 03:48:25 +02:00
|
|
|
reader = new JsonReader(reader("[# comment \n true]"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.beginArray();
|
|
|
|
try {
|
|
|
|
reader.nextBoolean();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
|
2012-08-28 03:48:25 +02:00
|
|
|
reader = new JsonReader(reader("[/* comment */ true]"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.beginArray();
|
|
|
|
try {
|
|
|
|
reader.nextBoolean();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testLenientComments() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[// comment \n true]"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextBoolean()).isTrue();
|
2010-08-26 10:44:02 +02:00
|
|
|
|
2012-08-28 03:48:25 +02:00
|
|
|
reader = new JsonReader(reader("[# comment \n true]"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextBoolean()).isTrue();
|
2010-08-26 10:44:02 +02:00
|
|
|
|
2012-08-28 03:48:25 +02:00
|
|
|
reader = new JsonReader(reader("[/* comment */ true]"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextBoolean()).isTrue();
|
2010-08-26 10:44:02 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2011-12-21 22:43:55 +01:00
|
|
|
public void testStrictCommentsWithSkipValue() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[// comment \n true]"));
|
2011-12-21 22:43:55 +01:00
|
|
|
reader.beginArray();
|
|
|
|
try {
|
|
|
|
reader.skipValue();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
|
2012-08-28 03:48:25 +02:00
|
|
|
reader = new JsonReader(reader("[# comment \n true]"));
|
2011-12-21 22:43:55 +01:00
|
|
|
reader.beginArray();
|
|
|
|
try {
|
|
|
|
reader.skipValue();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
|
2012-08-28 03:48:25 +02:00
|
|
|
reader = new JsonReader(reader("[/* comment */ true]"));
|
2011-12-21 22:43:55 +01:00
|
|
|
reader.beginArray();
|
|
|
|
try {
|
|
|
|
reader.skipValue();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testStrictUnquotedNames() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("{a:true}"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.beginObject();
|
|
|
|
try {
|
|
|
|
reader.nextName();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testLenientUnquotedNames() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("{a:true}"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextName()).isEqualTo("a");
|
2010-08-26 10:44:02 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2011-12-21 22:43:55 +01:00
|
|
|
public void testStrictUnquotedNamesWithSkipValue() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("{a:true}"));
|
2011-12-21 22:43:55 +01:00
|
|
|
reader.beginObject();
|
|
|
|
try {
|
|
|
|
reader.skipValue();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testStrictSingleQuotedNames() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("{'a':true}"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.beginObject();
|
|
|
|
try {
|
|
|
|
reader.nextName();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testLenientSingleQuotedNames() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("{'a':true}"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextName()).isEqualTo("a");
|
2010-08-26 10:44:02 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2011-12-21 22:43:55 +01:00
|
|
|
public void testStrictSingleQuotedNamesWithSkipValue() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("{'a':true}"));
|
2011-12-21 22:43:55 +01:00
|
|
|
reader.beginObject();
|
|
|
|
try {
|
|
|
|
reader.skipValue();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testStrictUnquotedStrings() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[a]"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.beginArray();
|
|
|
|
try {
|
|
|
|
reader.nextString();
|
|
|
|
fail();
|
2010-11-16 23:14:40 +01:00
|
|
|
} catch (MalformedJsonException expected) {
|
2010-08-26 10:44:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2011-12-21 21:40:33 +01:00
|
|
|
public void testStrictUnquotedStringsWithSkipValue() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[a]"));
|
2011-12-21 21:40:33 +01:00
|
|
|
reader.beginArray();
|
|
|
|
try {
|
|
|
|
reader.skipValue();
|
|
|
|
fail();
|
|
|
|
} catch (MalformedJsonException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testLenientUnquotedStrings() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[a]"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextString()).isEqualTo("a");
|
2010-08-26 10:44:02 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testStrictSingleQuotedStrings() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("['a']"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.beginArray();
|
|
|
|
try {
|
|
|
|
reader.nextString();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testLenientSingleQuotedStrings() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("['a']"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextString()).isEqualTo("a");
|
2010-08-26 10:44:02 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2011-12-21 22:43:55 +01:00
|
|
|
public void testStrictSingleQuotedStringsWithSkipValue() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("['a']"));
|
2011-12-21 22:43:55 +01:00
|
|
|
reader.beginArray();
|
|
|
|
try {
|
|
|
|
reader.skipValue();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testStrictSemicolonDelimitedArray() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[true;true]"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.beginArray();
|
|
|
|
try {
|
2023-04-10 19:50:25 +02:00
|
|
|
boolean unused1 = reader.nextBoolean();
|
|
|
|
boolean unused2 = reader.nextBoolean();
|
2010-08-26 10:44:02 +02:00
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testLenientSemicolonDelimitedArray() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[true;true]"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextBoolean()).isTrue();
|
|
|
|
assertThat(reader.nextBoolean()).isTrue();
|
2010-08-26 10:44:02 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2011-12-21 22:43:55 +01:00
|
|
|
public void testStrictSemicolonDelimitedArrayWithSkipValue() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[true;true]"));
|
2011-12-21 22:43:55 +01:00
|
|
|
reader.beginArray();
|
|
|
|
try {
|
|
|
|
reader.skipValue();
|
|
|
|
reader.skipValue();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testStrictSemicolonDelimitedNameValuePair() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("{\"a\":true;\"b\":true}"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.beginObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextName()).isEqualTo("a");
|
2010-08-26 10:44:02 +02:00
|
|
|
try {
|
2023-04-10 19:50:25 +02:00
|
|
|
boolean unused1 = reader.nextBoolean();
|
|
|
|
String unused2 = reader.nextName();
|
2010-08-26 10:44:02 +02:00
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testLenientSemicolonDelimitedNameValuePair() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("{\"a\":true;\"b\":true}"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextName()).isEqualTo("a");
|
|
|
|
assertThat(reader.nextBoolean()).isTrue();
|
|
|
|
assertThat(reader.nextName()).isEqualTo("b");
|
2010-08-26 10:44:02 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2011-12-21 22:43:55 +01:00
|
|
|
public void testStrictSemicolonDelimitedNameValuePairWithSkipValue() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("{\"a\":true;\"b\":true}"));
|
2011-12-21 22:43:55 +01:00
|
|
|
reader.beginObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextName()).isEqualTo("a");
|
2011-12-21 22:43:55 +01:00
|
|
|
try {
|
|
|
|
reader.skipValue();
|
|
|
|
reader.skipValue();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testStrictUnnecessaryArraySeparators() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[true,,true]"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextBoolean()).isTrue();
|
2010-08-26 10:44:02 +02:00
|
|
|
try {
|
|
|
|
reader.nextNull();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
|
2012-08-28 03:48:25 +02:00
|
|
|
reader = new JsonReader(reader("[,true]"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.beginArray();
|
|
|
|
try {
|
|
|
|
reader.nextNull();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
|
2012-08-28 03:48:25 +02:00
|
|
|
reader = new JsonReader(reader("[true,]"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextBoolean()).isTrue();
|
2010-08-26 10:44:02 +02:00
|
|
|
try {
|
|
|
|
reader.nextNull();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
|
2012-08-28 03:48:25 +02:00
|
|
|
reader = new JsonReader(reader("[,]"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.beginArray();
|
|
|
|
try {
|
|
|
|
reader.nextNull();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testLenientUnnecessaryArraySeparators() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[true,,true]"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextBoolean()).isTrue();
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.nextNull();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextBoolean()).isTrue();
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.endArray();
|
|
|
|
|
2012-08-28 03:48:25 +02:00
|
|
|
reader = new JsonReader(reader("[,true]"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginArray();
|
|
|
|
reader.nextNull();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextBoolean()).isTrue();
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.endArray();
|
|
|
|
|
2012-08-28 03:48:25 +02:00
|
|
|
reader = new JsonReader(reader("[true,]"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextBoolean()).isTrue();
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.nextNull();
|
|
|
|
reader.endArray();
|
|
|
|
|
2012-08-28 03:48:25 +02:00
|
|
|
reader = new JsonReader(reader("[,]"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginArray();
|
|
|
|
reader.nextNull();
|
|
|
|
reader.nextNull();
|
|
|
|
reader.endArray();
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2011-12-21 22:43:55 +01:00
|
|
|
public void testStrictUnnecessaryArraySeparatorsWithSkipValue() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[true,,true]"));
|
2011-12-21 22:43:55 +01:00
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextBoolean()).isTrue();
|
2011-12-21 22:43:55 +01:00
|
|
|
try {
|
|
|
|
reader.skipValue();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
|
2012-08-28 03:48:25 +02:00
|
|
|
reader = new JsonReader(reader("[,true]"));
|
2011-12-21 22:43:55 +01:00
|
|
|
reader.beginArray();
|
|
|
|
try {
|
|
|
|
reader.skipValue();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
|
2012-08-28 03:48:25 +02:00
|
|
|
reader = new JsonReader(reader("[true,]"));
|
2011-12-21 22:43:55 +01:00
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextBoolean()).isTrue();
|
2011-12-21 22:43:55 +01:00
|
|
|
try {
|
|
|
|
reader.skipValue();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
|
2012-08-28 03:48:25 +02:00
|
|
|
reader = new JsonReader(reader("[,]"));
|
2011-12-21 22:43:55 +01:00
|
|
|
reader.beginArray();
|
|
|
|
try {
|
|
|
|
reader.skipValue();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testStrictMultipleTopLevelValues() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[] []"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.beginArray();
|
|
|
|
reader.endArray();
|
|
|
|
try {
|
|
|
|
reader.peek();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testLenientMultipleTopLevelValues() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[] true {}"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginArray();
|
|
|
|
reader.endArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextBoolean()).isTrue();
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.beginObject();
|
|
|
|
reader.endObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
2010-08-26 10:44:02 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2011-12-21 22:43:55 +01:00
|
|
|
public void testStrictMultipleTopLevelValuesWithSkipValue() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[] []"));
|
2011-12-21 22:43:55 +01:00
|
|
|
reader.beginArray();
|
|
|
|
reader.endArray();
|
|
|
|
try {
|
|
|
|
reader.skipValue();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2016-01-18 21:07:33 +01:00
|
|
|
public void testTopLevelValueTypes() throws IOException {
|
|
|
|
JsonReader reader1 = new JsonReader(reader("true"));
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader1.nextBoolean()).isTrue();
|
|
|
|
assertThat(reader1.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
2012-08-26 06:02:09 +02:00
|
|
|
|
2016-01-18 21:07:33 +01:00
|
|
|
JsonReader reader2 = new JsonReader(reader("false"));
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader2.nextBoolean()).isFalse();
|
|
|
|
assertThat(reader2.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
2012-08-26 06:02:09 +02:00
|
|
|
|
2016-01-18 21:07:33 +01:00
|
|
|
JsonReader reader3 = new JsonReader(reader("null"));
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader3.peek()).isEqualTo(JsonToken.NULL);
|
2016-01-18 21:07:33 +01:00
|
|
|
reader3.nextNull();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader3.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
2010-08-26 10:44:02 +02:00
|
|
|
|
2016-01-18 21:07:33 +01:00
|
|
|
JsonReader reader4 = new JsonReader(reader("123"));
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader4.nextInt()).isEqualTo(123);
|
|
|
|
assertThat(reader4.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
2016-01-18 21:07:33 +01:00
|
|
|
|
|
|
|
JsonReader reader5 = new JsonReader(reader("123.4"));
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader5.nextDouble()).isEqualTo(123.4);
|
|
|
|
assertThat(reader5.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
2016-01-18 21:07:33 +01:00
|
|
|
|
|
|
|
JsonReader reader6 = new JsonReader(reader("\"a\""));
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader6.nextString()).isEqualTo("a");
|
|
|
|
assertThat(reader6.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
2010-08-26 10:44:02 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2016-01-18 21:07:33 +01:00
|
|
|
public void testTopLevelValueTypeWithSkipValue() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("true"));
|
2016-01-18 21:07:33 +01:00
|
|
|
reader.skipValue();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
2011-12-21 22:43:55 +01:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testStrictNonExecutePrefix() {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader(")]}'\n []"));
|
2010-08-26 10:44:02 +02:00
|
|
|
try {
|
|
|
|
reader.beginArray();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2011-12-21 22:43:55 +01:00
|
|
|
public void testStrictNonExecutePrefixWithSkipValue() {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader(")]}'\n []"));
|
2011-12-21 22:43:55 +01:00
|
|
|
try {
|
|
|
|
reader.skipValue();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testLenientNonExecutePrefix() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader(")]}'\n []"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginArray();
|
|
|
|
reader.endArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
2010-08-26 10:44:02 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-08-26 10:44:02 +02:00
|
|
|
public void testLenientNonExecutePrefixWithLeadingWhitespace() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("\r\n \t)]}'\n []"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginArray();
|
|
|
|
reader.endArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
2010-08-26 10:44:02 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-12-06 20:47:41 +01:00
|
|
|
public void testLenientPartialNonExecutePrefix() {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader(")]}' []"));
|
2010-08-26 10:44:02 +02:00
|
|
|
reader.setLenient(true);
|
|
|
|
try {
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextString()).isEqualTo(")");
|
2011-02-10 02:36:27 +01:00
|
|
|
reader.nextString();
|
2010-08-26 10:44:02 +02:00
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
}
|
2011-07-18 21:26:02 +02:00
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2011-07-20 20:57:30 +02:00
|
|
|
public void testBomIgnoredAsFirstCharacterOfDocument() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("\ufeff[]"));
|
2011-07-20 20:57:30 +02:00
|
|
|
reader.beginArray();
|
|
|
|
reader.endArray();
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2011-07-20 20:57:30 +02:00
|
|
|
public void testBomForbiddenAsOtherCharacterInDocument() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[\ufeff]"));
|
2011-07-18 21:26:02 +02:00
|
|
|
reader.beginArray();
|
|
|
|
try {
|
2011-07-20 20:57:30 +02:00
|
|
|
reader.endArray();
|
2011-07-18 21:26:02 +02:00
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2011-07-20 20:57:30 +02:00
|
|
|
public void testFailWithPosition() throws IOException {
|
2014-07-31 07:17:54 +02:00
|
|
|
testFailWithPosition("Expected value at line 6 column 5 path $[1]",
|
2012-08-28 05:52:18 +02:00
|
|
|
"[\n\n\n\n\n\"a\",}]");
|
2011-07-20 20:57:30 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2011-07-18 21:26:02 +02:00
|
|
|
public void testFailWithPositionGreaterThanBufferSize() throws IOException {
|
|
|
|
String spaces = repeat(' ', 8192);
|
2014-07-31 07:17:54 +02:00
|
|
|
testFailWithPosition("Expected value at line 6 column 5 path $[1]",
|
2012-08-28 05:52:18 +02:00
|
|
|
"[\n\n" + spaces + "\n\n\n\"a\",}]");
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-08-28 05:52:18 +02:00
|
|
|
public void testFailWithPositionOverSlashSlashEndOfLineComment() throws IOException {
|
2014-07-31 07:17:54 +02:00
|
|
|
testFailWithPosition("Expected value at line 5 column 6 path $[1]",
|
2012-08-28 05:52:18 +02:00
|
|
|
"\n// foo\n\n//bar\r\n[\"a\",}");
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-08-28 05:52:18 +02:00
|
|
|
public void testFailWithPositionOverHashEndOfLineComment() throws IOException {
|
2014-07-31 07:17:54 +02:00
|
|
|
testFailWithPosition("Expected value at line 5 column 6 path $[1]",
|
2012-08-28 05:52:18 +02:00
|
|
|
"\n# foo\n\n#bar\r\n[\"a\",}");
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-08-28 05:52:18 +02:00
|
|
|
public void testFailWithPositionOverCStyleComment() throws IOException {
|
2014-07-31 07:17:54 +02:00
|
|
|
testFailWithPosition("Expected value at line 6 column 12 path $[1]",
|
2012-08-28 05:52:18 +02:00
|
|
|
"\n\n/* foo\n*\n*\r\nbar */[\"a\",}");
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-08-28 05:52:18 +02:00
|
|
|
public void testFailWithPositionOverQuotedString() throws IOException {
|
2014-07-31 07:17:54 +02:00
|
|
|
testFailWithPosition("Expected value at line 5 column 3 path $[1]",
|
|
|
|
"[\"foo\nbar\r\nbaz\n\",\n }");
|
2012-08-28 05:52:18 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-08-28 05:52:18 +02:00
|
|
|
public void testFailWithPositionOverUnquotedString() throws IOException {
|
2014-07-31 07:17:54 +02:00
|
|
|
testFailWithPosition("Expected value at line 5 column 2 path $[1]", "[\n\nabcd\n\n,}");
|
2012-08-28 05:52:18 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-08-28 05:52:18 +02:00
|
|
|
public void testFailWithEscapedNewlineCharacter() throws IOException {
|
2014-07-31 07:17:54 +02:00
|
|
|
testFailWithPosition("Expected value at line 5 column 3 path $[1]", "[\n\n\"\\\n\n\",}");
|
2012-08-28 05:52:18 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-08-28 05:52:18 +02:00
|
|
|
public void testFailWithPositionIsOffsetByBom() throws IOException {
|
2014-07-31 07:17:54 +02:00
|
|
|
testFailWithPosition("Expected value at line 1 column 6 path $[1]",
|
2012-08-28 05:52:18 +02:00
|
|
|
"\ufeff[\"a\",}]");
|
|
|
|
}
|
|
|
|
|
|
|
|
private void testFailWithPosition(String message, String json) throws IOException {
|
|
|
|
// Validate that it works reading the string normally.
|
|
|
|
JsonReader reader1 = new JsonReader(reader(json));
|
|
|
|
reader1.setLenient(true);
|
|
|
|
reader1.beginArray();
|
2023-04-10 19:50:25 +02:00
|
|
|
String unused1 = reader1.nextString();
|
2012-08-28 05:52:18 +02:00
|
|
|
try {
|
2023-04-10 19:50:25 +02:00
|
|
|
JsonToken unused2 = reader1.peek();
|
2012-08-28 05:52:18 +02:00
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(expected.getMessage()).isEqualTo(message);
|
2012-08-28 05:52:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Also validate that it works when skipping.
|
|
|
|
JsonReader reader2 = new JsonReader(reader(json));
|
|
|
|
reader2.setLenient(true);
|
|
|
|
reader2.beginArray();
|
|
|
|
reader2.skipValue();
|
|
|
|
try {
|
2023-04-10 19:50:25 +02:00
|
|
|
JsonToken unused3 = reader2.peek();
|
2012-08-28 05:52:18 +02:00
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(expected.getMessage()).isEqualTo(message);
|
2012-08-28 05:52:18 +02:00
|
|
|
}
|
2011-07-20 20:57:30 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2014-07-31 07:17:54 +02:00
|
|
|
public void testFailWithPositionDeepPath() throws IOException {
|
|
|
|
JsonReader reader = new JsonReader(reader("[1,{\"a\":[2,3,}"));
|
|
|
|
reader.beginArray();
|
2023-04-10 19:50:25 +02:00
|
|
|
int unused1 = reader.nextInt();
|
2014-07-31 07:17:54 +02:00
|
|
|
reader.beginObject();
|
2023-04-10 19:50:25 +02:00
|
|
|
String unused2 = reader.nextName();
|
2014-07-31 07:17:54 +02:00
|
|
|
reader.beginArray();
|
2023-04-10 19:50:25 +02:00
|
|
|
int unused3 = reader.nextInt();
|
|
|
|
int unused4 = reader.nextInt();
|
2014-07-31 07:17:54 +02:00
|
|
|
try {
|
2023-04-10 19:50:25 +02:00
|
|
|
JsonToken unused5 = reader.peek();
|
2014-07-31 07:17:54 +02:00
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(expected.getMessage()).isEqualTo("Expected value at line 1 column 14 path $[1].a[2]");
|
2014-07-31 07:17:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-09-02 22:12:19 +02:00
|
|
|
public void testStrictVeryLongNumber() throws IOException {
|
|
|
|
JsonReader reader = new JsonReader(reader("[0." + repeat('9', 8192) + "]"));
|
|
|
|
reader.beginArray();
|
|
|
|
try {
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextDouble()).isEqualTo(1d);
|
2012-09-02 22:12:19 +02:00
|
|
|
fail();
|
|
|
|
} catch (MalformedJsonException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-09-02 22:12:19 +02:00
|
|
|
public void testLenientVeryLongNumber() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[0." + repeat('9', 8192) + "]"));
|
2012-09-02 22:12:19 +02:00
|
|
|
reader.setLenient(true);
|
2012-08-27 03:17:50 +02:00
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.STRING);
|
|
|
|
assertThat(reader.nextDouble()).isEqualTo(1d);
|
2012-08-27 03:17:50 +02:00
|
|
|
reader.endArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
2012-08-27 03:17:50 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-08-27 03:17:50 +02:00
|
|
|
public void testVeryLongUnquotedLiteral() throws IOException {
|
|
|
|
String literal = "a" + repeat('b', 8192) + "c";
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[" + literal + "]"));
|
2012-08-27 03:17:50 +02:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextString()).isEqualTo(literal);
|
2012-08-27 03:17:50 +02:00
|
|
|
reader.endArray();
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-02-11 21:15:39 +01:00
|
|
|
public void testDeeplyNestedArrays() throws IOException {
|
|
|
|
// this is nested 40 levels deep; Gson is tuned for nesting is 30 levels deep or fewer
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader(
|
2012-02-11 21:15:39 +01:00
|
|
|
"[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]"));
|
|
|
|
for (int i = 0; i < 40; i++) {
|
|
|
|
reader.beginArray();
|
|
|
|
}
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.getPath()).isEqualTo("$[0][0][0][0][0][0][0][0][0][0][0][0][0][0][0]"
|
|
|
|
+ "[0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0]");
|
2012-02-11 21:15:39 +01:00
|
|
|
for (int i = 0; i < 40; i++) {
|
|
|
|
reader.endArray();
|
|
|
|
}
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
2012-02-11 21:15:39 +01:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-02-11 21:15:39 +01:00
|
|
|
public void testDeeplyNestedObjects() throws IOException {
|
|
|
|
// Build a JSON document structured like {"a":{"a":{"a":{"a":true}}}}, but 40 levels deep
|
|
|
|
String array = "{\"a\":%s}";
|
|
|
|
String json = "true";
|
|
|
|
for (int i = 0; i < 40; i++) {
|
|
|
|
json = String.format(array, json);
|
|
|
|
}
|
|
|
|
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader(json));
|
2012-02-11 21:15:39 +01:00
|
|
|
for (int i = 0; i < 40; i++) {
|
|
|
|
reader.beginObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextName()).isEqualTo("a");
|
2012-02-11 21:15:39 +01:00
|
|
|
}
|
2023-02-06 15:13:28 +01:00
|
|
|
assertThat(reader.getPath()).isEqualTo("$.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a"
|
|
|
|
+ ".a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a");
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextBoolean()).isTrue();
|
2012-02-11 21:15:39 +01:00
|
|
|
for (int i = 0; i < 40; i++) {
|
|
|
|
reader.endObject();
|
|
|
|
}
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
2012-02-11 21:15:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// http://code.google.com/p/google-gson/issues/detail?id=409
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-02-11 21:15:39 +01:00
|
|
|
public void testStringEndingInSlash() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("/"));
|
2012-02-11 21:15:39 +01:00
|
|
|
reader.setLenient(true);
|
2012-02-16 23:49:53 +01:00
|
|
|
try {
|
|
|
|
reader.peek();
|
|
|
|
fail();
|
|
|
|
} catch (MalformedJsonException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-02-16 23:49:53 +01:00
|
|
|
public void testDocumentWithCommentEndingInSlash() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("/* foo *//"));
|
2012-02-16 23:49:53 +01:00
|
|
|
reader.setLenient(true);
|
|
|
|
try {
|
|
|
|
reader.peek();
|
|
|
|
fail();
|
|
|
|
} catch (MalformedJsonException expected) {
|
|
|
|
}
|
2012-02-11 21:15:39 +01:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-02-11 21:15:39 +01:00
|
|
|
public void testStringWithLeadingSlash() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("/x"));
|
2012-02-11 21:15:39 +01:00
|
|
|
reader.setLenient(true);
|
|
|
|
try {
|
|
|
|
reader.peek();
|
|
|
|
fail();
|
|
|
|
} catch (MalformedJsonException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-02-11 21:15:39 +01:00
|
|
|
public void testUnterminatedObject() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("{\"a\":\"android\"x"));
|
2012-02-11 21:15:39 +01:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextName()).isEqualTo("a");
|
|
|
|
assertThat(reader.nextString()).isEqualTo("android");
|
2012-02-11 21:15:39 +01:00
|
|
|
try {
|
|
|
|
reader.peek();
|
|
|
|
fail();
|
|
|
|
} catch (MalformedJsonException expected) {
|
|
|
|
}
|
|
|
|
}
|
2016-01-18 21:07:33 +01:00
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-02-11 21:15:39 +01:00
|
|
|
public void testVeryLongQuotedString() throws IOException {
|
|
|
|
char[] stringChars = new char[1024 * 16];
|
|
|
|
Arrays.fill(stringChars, 'x');
|
|
|
|
String string = new String(stringChars);
|
|
|
|
String json = "[\"" + string + "\"]";
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader(json));
|
2012-02-11 21:15:39 +01:00
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextString()).isEqualTo(string);
|
2012-02-11 21:15:39 +01:00
|
|
|
reader.endArray();
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-02-11 21:15:39 +01:00
|
|
|
public void testVeryLongUnquotedString() throws IOException {
|
|
|
|
char[] stringChars = new char[1024 * 16];
|
|
|
|
Arrays.fill(stringChars, 'x');
|
|
|
|
String string = new String(stringChars);
|
|
|
|
String json = "[" + string + "]";
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader(json));
|
2012-02-11 21:15:39 +01:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextString()).isEqualTo(string);
|
2012-02-11 21:15:39 +01:00
|
|
|
reader.endArray();
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-02-11 21:15:39 +01:00
|
|
|
public void testVeryLongUnterminatedString() throws IOException {
|
|
|
|
char[] stringChars = new char[1024 * 16];
|
|
|
|
Arrays.fill(stringChars, 'x');
|
|
|
|
String string = new String(stringChars);
|
|
|
|
String json = "[" + string;
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader(json));
|
2012-02-11 21:15:39 +01:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextString()).isEqualTo(string);
|
2012-02-11 21:15:39 +01:00
|
|
|
try {
|
|
|
|
reader.peek();
|
|
|
|
fail();
|
|
|
|
} catch (EOFException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-02-11 21:15:39 +01:00
|
|
|
public void testSkipVeryLongUnquotedString() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[" + repeat('x', 8192) + "]"));
|
2012-02-11 21:15:39 +01:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginArray();
|
|
|
|
reader.skipValue();
|
|
|
|
reader.endArray();
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-08-27 03:17:50 +02:00
|
|
|
public void testSkipTopLevelUnquotedString() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader(repeat('x', 8192)));
|
2012-08-27 03:17:50 +02:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.skipValue();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
2012-08-27 03:17:50 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-08-27 03:17:50 +02:00
|
|
|
public void testSkipVeryLongQuotedString() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[\"" + repeat('x', 8192) + "\"]"));
|
2012-08-27 03:17:50 +02:00
|
|
|
reader.beginArray();
|
|
|
|
reader.skipValue();
|
|
|
|
reader.endArray();
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-08-27 03:17:50 +02:00
|
|
|
public void testSkipTopLevelQuotedString() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("\"" + repeat('x', 8192) + "\""));
|
2012-08-27 03:17:50 +02:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.skipValue();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
2012-08-27 03:17:50 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-02-11 21:15:39 +01:00
|
|
|
public void testStringAsNumberWithTruncatedExponent() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[123e]"));
|
2012-02-11 21:15:39 +01:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(STRING);
|
2012-02-11 21:15:39 +01:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-02-11 21:15:39 +01:00
|
|
|
public void testStringAsNumberWithDigitAndNonDigitExponent() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[123e4b]"));
|
2012-02-11 21:15:39 +01:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(STRING);
|
2012-02-11 21:15:39 +01:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-02-11 21:15:39 +01:00
|
|
|
public void testStringAsNumberWithNonDigitExponent() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[123eb]"));
|
2012-02-11 21:15:39 +01:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(STRING);
|
2012-02-11 21:15:39 +01:00
|
|
|
}
|
2011-07-18 21:26:02 +02:00
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-08-26 06:02:09 +02:00
|
|
|
public void testEmptyStringName() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("{\"\":true}"));
|
2012-08-26 06:02:09 +02:00
|
|
|
reader.setLenient(true);
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(BEGIN_OBJECT);
|
2012-08-26 06:02:09 +02:00
|
|
|
reader.beginObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(NAME);
|
|
|
|
assertThat(reader.nextName()).isEqualTo("");
|
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.BOOLEAN);
|
|
|
|
assertThat(reader.nextBoolean()).isTrue();
|
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.END_OBJECT);
|
2012-08-26 06:02:09 +02:00
|
|
|
reader.endObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.END_DOCUMENT);
|
2012-08-26 06:02:09 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-08-26 06:02:09 +02:00
|
|
|
public void testStrictExtraCommasInMaps() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("{\"a\":\"b\",}"));
|
2012-08-26 06:02:09 +02:00
|
|
|
reader.beginObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextName()).isEqualTo("a");
|
|
|
|
assertThat(reader.nextString()).isEqualTo("b");
|
2012-08-26 06:02:09 +02:00
|
|
|
try {
|
|
|
|
reader.peek();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-08-26 06:02:09 +02:00
|
|
|
public void testLenientExtraCommasInMaps() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("{\"a\":\"b\",}"));
|
2012-08-26 06:02:09 +02:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginObject();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextName()).isEqualTo("a");
|
|
|
|
assertThat(reader.nextString()).isEqualTo("b");
|
2012-08-26 06:02:09 +02:00
|
|
|
try {
|
|
|
|
reader.peek();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-18 21:26:02 +02:00
|
|
|
private String repeat(char c, int count) {
|
|
|
|
char[] array = new char[count];
|
|
|
|
Arrays.fill(array, c);
|
|
|
|
return new String(array);
|
|
|
|
}
|
2012-08-26 21:31:06 +02:00
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-08-26 21:31:06 +02:00
|
|
|
public void testMalformedDocuments() throws IOException {
|
|
|
|
assertDocument("{]", BEGIN_OBJECT, IOException.class);
|
|
|
|
assertDocument("{,", BEGIN_OBJECT, IOException.class);
|
|
|
|
assertDocument("{{", BEGIN_OBJECT, IOException.class);
|
|
|
|
assertDocument("{[", BEGIN_OBJECT, IOException.class);
|
|
|
|
assertDocument("{:", BEGIN_OBJECT, IOException.class);
|
|
|
|
assertDocument("{\"name\",", BEGIN_OBJECT, NAME, IOException.class);
|
|
|
|
assertDocument("{\"name\",", BEGIN_OBJECT, NAME, IOException.class);
|
|
|
|
assertDocument("{\"name\":}", BEGIN_OBJECT, NAME, IOException.class);
|
|
|
|
assertDocument("{\"name\"::", BEGIN_OBJECT, NAME, IOException.class);
|
|
|
|
assertDocument("{\"name\":,", BEGIN_OBJECT, NAME, IOException.class);
|
|
|
|
assertDocument("{\"name\"=}", BEGIN_OBJECT, NAME, IOException.class);
|
|
|
|
assertDocument("{\"name\"=>}", BEGIN_OBJECT, NAME, IOException.class);
|
|
|
|
assertDocument("{\"name\"=>\"string\":", BEGIN_OBJECT, NAME, STRING, IOException.class);
|
|
|
|
assertDocument("{\"name\"=>\"string\"=", BEGIN_OBJECT, NAME, STRING, IOException.class);
|
|
|
|
assertDocument("{\"name\"=>\"string\"=>", BEGIN_OBJECT, NAME, STRING, IOException.class);
|
|
|
|
assertDocument("{\"name\"=>\"string\",", BEGIN_OBJECT, NAME, STRING, IOException.class);
|
|
|
|
assertDocument("{\"name\"=>\"string\",\"name\"", BEGIN_OBJECT, NAME, STRING, NAME);
|
|
|
|
assertDocument("[}", BEGIN_ARRAY, IOException.class);
|
|
|
|
assertDocument("[,]", BEGIN_ARRAY, NULL, NULL, END_ARRAY);
|
|
|
|
assertDocument("{", BEGIN_OBJECT, IOException.class);
|
|
|
|
assertDocument("{\"name\"", BEGIN_OBJECT, NAME, IOException.class);
|
|
|
|
assertDocument("{\"name\",", BEGIN_OBJECT, NAME, IOException.class);
|
|
|
|
assertDocument("{'name'", BEGIN_OBJECT, NAME, IOException.class);
|
|
|
|
assertDocument("{'name',", BEGIN_OBJECT, NAME, IOException.class);
|
|
|
|
assertDocument("{name", BEGIN_OBJECT, NAME, IOException.class);
|
|
|
|
assertDocument("[", BEGIN_ARRAY, IOException.class);
|
|
|
|
assertDocument("[string", BEGIN_ARRAY, STRING, IOException.class);
|
|
|
|
assertDocument("[\"string\"", BEGIN_ARRAY, STRING, IOException.class);
|
|
|
|
assertDocument("['string'", BEGIN_ARRAY, STRING, IOException.class);
|
|
|
|
assertDocument("[123", BEGIN_ARRAY, NUMBER, IOException.class);
|
|
|
|
assertDocument("[123,", BEGIN_ARRAY, NUMBER, IOException.class);
|
|
|
|
assertDocument("{\"name\":123", BEGIN_OBJECT, NAME, NUMBER, IOException.class);
|
|
|
|
assertDocument("{\"name\":123,", BEGIN_OBJECT, NAME, NUMBER, IOException.class);
|
|
|
|
assertDocument("{\"name\":\"string\"", BEGIN_OBJECT, NAME, STRING, IOException.class);
|
|
|
|
assertDocument("{\"name\":\"string\",", BEGIN_OBJECT, NAME, STRING, IOException.class);
|
|
|
|
assertDocument("{\"name\":'string'", BEGIN_OBJECT, NAME, STRING, IOException.class);
|
|
|
|
assertDocument("{\"name\":'string',", BEGIN_OBJECT, NAME, STRING, IOException.class);
|
|
|
|
assertDocument("{\"name\":false", BEGIN_OBJECT, NAME, BOOLEAN, IOException.class);
|
|
|
|
assertDocument("{\"name\":false,,", BEGIN_OBJECT, NAME, BOOLEAN, IOException.class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This test behave slightly differently in Gson 2.2 and earlier. It fails
|
|
|
|
* during peek rather than during nextString().
|
|
|
|
*/
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-08-26 21:31:06 +02:00
|
|
|
public void testUnterminatedStringFailure() throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader("[\"string"));
|
2012-08-26 21:31:06 +02:00
|
|
|
reader.setLenient(true);
|
|
|
|
reader.beginArray();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.peek()).isEqualTo(JsonToken.STRING);
|
2012-08-26 21:31:06 +02:00
|
|
|
try {
|
|
|
|
reader.nextString();
|
|
|
|
fail();
|
|
|
|
} catch (MalformedJsonException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-09 19:08:21 +01:00
|
|
|
/**
|
|
|
|
* Regression test for an issue with buffer filling and consumeNonExecutePrefix.
|
|
|
|
*/
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2021-11-09 19:08:21 +01:00
|
|
|
public void testReadAcrossBuffers() throws IOException {
|
2022-01-12 17:58:30 +01:00
|
|
|
StringBuilder sb = new StringBuilder("#");
|
2021-11-09 19:08:21 +01:00
|
|
|
for (int i = 0; i < JsonReader.BUFFER_SIZE - 3; i++) {
|
|
|
|
sb.append(' ');
|
|
|
|
}
|
|
|
|
sb.append("\n)]}'\n3");
|
|
|
|
JsonReader reader = new JsonReader(reader(sb.toString()));
|
|
|
|
reader.setLenient(true);
|
|
|
|
JsonToken token = reader.peek();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(token).isEqualTo(JsonToken.NUMBER);
|
2021-11-09 19:08:21 +01:00
|
|
|
}
|
|
|
|
|
2012-08-26 21:31:06 +02:00
|
|
|
private void assertDocument(String document, Object... expectations) throws IOException {
|
2012-08-28 03:48:25 +02:00
|
|
|
JsonReader reader = new JsonReader(reader(document));
|
2012-08-26 21:31:06 +02:00
|
|
|
reader.setLenient(true);
|
|
|
|
for (Object expectation : expectations) {
|
|
|
|
if (expectation == BEGIN_OBJECT) {
|
|
|
|
reader.beginObject();
|
|
|
|
} else if (expectation == BEGIN_ARRAY) {
|
|
|
|
reader.beginArray();
|
|
|
|
} else if (expectation == END_OBJECT) {
|
|
|
|
reader.endObject();
|
|
|
|
} else if (expectation == END_ARRAY) {
|
|
|
|
reader.endArray();
|
|
|
|
} else if (expectation == NAME) {
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextName()).isEqualTo("name");
|
2012-08-26 21:31:06 +02:00
|
|
|
} else if (expectation == BOOLEAN) {
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextBoolean()).isFalse();
|
2012-08-26 21:31:06 +02:00
|
|
|
} else if (expectation == STRING) {
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextString()).isEqualTo("string");
|
2012-08-26 21:31:06 +02:00
|
|
|
} else if (expectation == NUMBER) {
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(reader.nextInt()).isEqualTo(123);
|
2012-08-26 21:31:06 +02:00
|
|
|
} else if (expectation == NULL) {
|
|
|
|
reader.nextNull();
|
|
|
|
} else if (expectation == IOException.class) {
|
|
|
|
try {
|
|
|
|
reader.peek();
|
|
|
|
fail();
|
|
|
|
} catch (IOException expected) {
|
2023-03-01 23:23:27 +01:00
|
|
|
// OK: Should fail
|
2012-08-26 21:31:06 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new AssertionError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-28 03:48:25 +02:00
|
|
|
/**
|
|
|
|
* Returns a reader that returns one character at a time.
|
|
|
|
*/
|
|
|
|
private Reader reader(final String s) {
|
2014-11-16 21:45:01 +01:00
|
|
|
/* if (true) */ return new StringReader(s);
|
|
|
|
/* return new Reader() {
|
2012-08-28 03:48:25 +02:00
|
|
|
int position = 0;
|
|
|
|
@Override public int read(char[] buffer, int offset, int count) throws IOException {
|
|
|
|
if (position == s.length()) {
|
|
|
|
return -1;
|
|
|
|
} else if (count > 0) {
|
|
|
|
buffer[offset] = s.charAt(position++);
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
throw new IllegalArgumentException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@Override public void close() throws IOException {
|
|
|
|
}
|
2014-11-16 21:45:01 +01:00
|
|
|
}; */
|
2012-08-28 03:48:25 +02:00
|
|
|
}
|
2010-08-26 10:44:02 +02:00
|
|
|
}
|