Skip to content

Commit ab4f24d

Browse files
committed
Switch to our own classes for JSON parsing
We leave in support for handling GSON types to keep this diff as small as possible.
1 parent fac4503 commit ab4f24d

File tree

9 files changed

+474
-136
lines changed

9 files changed

+474
-136
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.json;
19+
20+
import java.io.IOException;
21+
import java.io.UncheckedIOException;
22+
import java.nio.CharBuffer;
23+
import java.util.Objects;
24+
25+
/**
26+
* Similar to a {@link Readable} but with the ability to peek a single character ahead.
27+
* <p>
28+
* For the sake of providing a useful {@link #toString()} implementation, keeps a small circular
29+
* buffer of the most recently read characters.
30+
*/
31+
class Input {
32+
33+
public static final char EOF = (char) -1;
34+
private final Readable source;
35+
private boolean read;
36+
private char peekedChar;
37+
private char[] lastRead = new char[128];
38+
private int insertAt = 0;
39+
private boolean filled = false;
40+
41+
public Input(Readable source) {
42+
this.source = Objects.requireNonNull(source);
43+
}
44+
45+
public char peek() {
46+
init();
47+
return peekedChar;
48+
}
49+
50+
public char read() {
51+
init();
52+
read = false;
53+
append();
54+
return peekedChar;
55+
}
56+
57+
@Override
58+
public String toString() {
59+
String preamble = "Last " + (filled ? lastRead.length : insertAt) + " characters read: ";
60+
if (!filled) {
61+
return preamble + new String(lastRead, 0, insertAt);
62+
}
63+
64+
// We filled the array. The insertion point would overwrite the first thing we should read.
65+
char[] buf = new char[lastRead.length];
66+
int lengthToRead = lastRead.length - insertAt;
67+
System.arraycopy(lastRead, insertAt, buf, 0, lengthToRead);
68+
System.arraycopy(lastRead, 0, buf, lengthToRead, insertAt);
69+
70+
return preamble + new String(buf);
71+
}
72+
73+
private void init() {
74+
if (read) {
75+
return;
76+
}
77+
78+
CharBuffer buf = CharBuffer.allocate(1);
79+
int charsRead;
80+
try {
81+
charsRead = source.read(buf);
82+
} catch (IOException e) {
83+
throw new UncheckedIOException(e.getMessage(), e);
84+
}
85+
if (charsRead != 1) {
86+
peekedChar = EOF;
87+
} else {
88+
peekedChar = buf.array()[0];
89+
}
90+
read = true;
91+
}
92+
93+
private void append() {
94+
if (peekedChar == Input.EOF) {
95+
return;
96+
}
97+
lastRead[insertAt] = peekedChar;
98+
insertAt = ++insertAt % lastRead.length;
99+
if (insertAt == 0) {
100+
filled = true;
101+
}
102+
}
103+
}

java/client/src/org/openqa/selenium/json/Json.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
package org.openqa.selenium.json;
1919

2020
import com.google.common.reflect.TypeToken;
21-
import com.google.gson.Gson;
22-
import com.google.gson.GsonBuilder;
2321

2422
import java.io.IOException;
2523
import java.io.Reader;
@@ -32,12 +30,6 @@
3230
import java.util.Map;
3331

3432
public class Json {
35-
static final Gson GSON = new GsonBuilder()
36-
.setLenient()
37-
.serializeNulls()
38-
.disableHtmlEscaping()
39-
.create();
40-
4133
public static final Type LIST_OF_MAPS_TYPE = new TypeToken<List<Map<String, Object>>>() {}.getType();
4234
public static final Type MAP_TYPE = new TypeToken<Map<String, Object>>() {}.getType();
4335
public static final Type OBJECT_TYPE = new TypeToken<Object>() {}.getType();

0 commit comments

Comments
 (0)