Skip to content

Commit e8c4cab

Browse files
author
Yaniv Inbar
committed
http: allow no input with Void.class parsed type
https://codereview.appspot.com/8664050/
1 parent e3f9690 commit e8c4cab

File tree

10 files changed

+402
-2
lines changed

10 files changed

+402
-2
lines changed

google-http-client/src/main/java/com/google/api/client/json/JsonParser.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,6 @@ public final <T> T parse(Class<T> destinationClass) throws IOException {
308308
@Beta
309309
public final <T> T parse(Class<T> destinationClass, CustomizeJsonParser customizeParser)
310310
throws IOException {
311-
startParsing();
312311
@SuppressWarnings("unchecked")
313312
T result = (T) parse(destinationClass, false, customizeParser);
314313
return result;
@@ -353,7 +352,9 @@ public Object parse(Type dataType, boolean close) throws IOException {
353352
public Object parse(Type dataType, boolean close, CustomizeJsonParser customizeParser)
354353
throws IOException {
355354
try {
356-
startParsing();
355+
if (!Void.class.equals(dataType)) {
356+
startParsing();
357+
}
357358
return parseValue(null, dataType, new ArrayList<Type>(), null, customizeParser);
358359
} finally {
359360
if (close) {

google-http-client/src/main/java/com/google/api/client/testing/http/json/MockJsonFactory.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@
3636
*
3737
* @author [email protected] (Ravi Mistry)
3838
* @since 1.11
39+
* @deprecated (scheduled to be removed in 1.16) Use
40+
* {@link com.google.api.client.testing.json.MockJsonFactory}
3941
*/
42+
@Deprecated
4043
@Beta
4144
public class MockJsonFactory extends JsonFactory {
4245

google-http-client/src/main/java/com/google/api/client/testing/http/json/MockJsonGenerator.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@
3232
*
3333
* @author [email protected] (Ravi Mistry)
3434
* @since 1.11
35+
* @deprecated (scheduled to be removed in 1.16) Use
36+
* {@link com.google.api.client.testing.json.MockJsonGenerator}
3537
*/
38+
@Deprecated
3639
@Beta
3740
public class MockJsonGenerator extends JsonGenerator {
3841

google-http-client/src/main/java/com/google/api/client/testing/http/json/MockJsonParser.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@
3333
*
3434
* @author [email protected] (Ravi Mistry)
3535
* @since 1.11
36+
* @deprecated (scheduled to be removed in 1.16) Use
37+
* {@link com.google.api.client.testing.json.MockJsonParser}
3638
*/
39+
@Deprecated
3740
@Beta
3841
public class MockJsonParser extends JsonParser {
3942

google-http-client/src/main/java/com/google/api/client/testing/http/json/package-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* @since 1.11
2020
* @author [email protected] (Ravi Mistry)
2121
*/
22+
@Deprecated
2223
@com.google.api.client.util.Beta
2324
package com.google.api.client.testing.http.json;
2425

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright (c) 2013 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.api.client.testing.json;
16+
17+
import com.google.api.client.json.JsonFactory;
18+
import com.google.api.client.json.JsonGenerator;
19+
import com.google.api.client.json.JsonParser;
20+
import com.google.api.client.util.Beta;
21+
22+
import java.io.IOException;
23+
import java.io.InputStream;
24+
import java.io.OutputStream;
25+
import java.io.Reader;
26+
import java.io.Writer;
27+
import java.nio.charset.Charset;
28+
29+
/**
30+
* {@link Beta} <br/>
31+
* Mock for {@link JsonFactory}.
32+
*
33+
* <p>
34+
* Implementation is thread-safe.
35+
* </p>
36+
*
37+
* @author [email protected] (Ravi Mistry)
38+
* @since 1.15 (since 1.11 as com.google.api.client.testing.http.json.MockJsonFactory)
39+
*/
40+
@Beta
41+
public class MockJsonFactory extends JsonFactory {
42+
43+
@Override
44+
public JsonParser createJsonParser(InputStream in) throws IOException {
45+
return new MockJsonParser(this);
46+
}
47+
48+
@Override
49+
public JsonParser createJsonParser(InputStream in, Charset charset) throws IOException {
50+
return new MockJsonParser(this);
51+
}
52+
53+
@Override
54+
public JsonParser createJsonParser(String value) throws IOException {
55+
return new MockJsonParser(this);
56+
}
57+
58+
@Override
59+
public JsonParser createJsonParser(Reader reader) throws IOException {
60+
return new MockJsonParser(this);
61+
}
62+
63+
@Override
64+
public JsonGenerator createJsonGenerator(OutputStream out, Charset enc) throws IOException {
65+
return new MockJsonGenerator(this);
66+
}
67+
68+
@Override
69+
public JsonGenerator createJsonGenerator(Writer writer) throws IOException {
70+
return new MockJsonGenerator(this);
71+
}
72+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright (c) 2013 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.api.client.testing.json;
16+
17+
import com.google.api.client.json.JsonFactory;
18+
import com.google.api.client.json.JsonGenerator;
19+
import com.google.api.client.util.Beta;
20+
21+
import java.io.IOException;
22+
import java.math.BigDecimal;
23+
import java.math.BigInteger;
24+
25+
/**
26+
* {@link Beta} <br/>
27+
* Mock for {@link JsonGenerator}.
28+
*
29+
* <p>
30+
* Implementation is thread-safe.
31+
* </p>
32+
*
33+
* @author [email protected] (Ravi Mistry)
34+
* @since 1.15 (since 1.11 as com.google.api.client.testing.http.json.MockJsonGenerator)
35+
*/
36+
@Beta
37+
public class MockJsonGenerator extends JsonGenerator {
38+
39+
private final JsonFactory factory;
40+
41+
MockJsonGenerator(JsonFactory factory) {
42+
this.factory = factory;
43+
}
44+
45+
@Override
46+
public JsonFactory getFactory() {
47+
return factory;
48+
}
49+
50+
@Override
51+
public void flush() throws IOException {
52+
}
53+
54+
@Override
55+
public void close() throws IOException {
56+
}
57+
58+
@Override
59+
public void writeStartArray() throws IOException {
60+
}
61+
62+
@Override
63+
public void writeEndArray() throws IOException {
64+
}
65+
66+
@Override
67+
public void writeStartObject() throws IOException {
68+
}
69+
70+
@Override
71+
public void writeEndObject() throws IOException {
72+
}
73+
74+
@Override
75+
public void writeFieldName(String name) throws IOException {
76+
}
77+
78+
@Override
79+
public void writeNull() throws IOException {
80+
}
81+
82+
@Override
83+
public void writeString(String value) throws IOException {
84+
}
85+
86+
@Override
87+
public void writeBoolean(boolean state) throws IOException {
88+
}
89+
90+
@Override
91+
public void writeNumber(int v) throws IOException {
92+
}
93+
94+
@Override
95+
public void writeNumber(long v) throws IOException {
96+
}
97+
98+
@Override
99+
public void writeNumber(BigInteger v) throws IOException {
100+
}
101+
102+
@Override
103+
public void writeNumber(float v) throws IOException {
104+
}
105+
106+
@Override
107+
public void writeNumber(double v) throws IOException {
108+
}
109+
110+
@Override
111+
public void writeNumber(BigDecimal v) throws IOException {
112+
}
113+
114+
@Override
115+
public void writeNumber(String encodedValue) throws IOException {
116+
}
117+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* Copyright (c) 2013 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.api.client.testing.json;
16+
17+
import com.google.api.client.json.JsonFactory;
18+
import com.google.api.client.json.JsonParser;
19+
import com.google.api.client.json.JsonToken;
20+
import com.google.api.client.util.Beta;
21+
22+
import java.io.IOException;
23+
import java.math.BigDecimal;
24+
import java.math.BigInteger;
25+
26+
/**
27+
* {@link Beta} <br/>
28+
* Mock for {@link JsonParser}.
29+
*
30+
* <p>
31+
* Implementation is thread-safe.
32+
* </p>
33+
*
34+
* @author [email protected] (Ravi Mistry)
35+
* @since 1.15 (since 1.11 as com.google.api.client.testing.http.json.MockJsonParser)
36+
*/
37+
@Beta
38+
public class MockJsonParser extends JsonParser {
39+
40+
private boolean isClosed;
41+
42+
private final JsonFactory factory;
43+
44+
MockJsonParser(JsonFactory factory) {
45+
this.factory = factory;
46+
}
47+
48+
@Override
49+
public JsonFactory getFactory() {
50+
return factory;
51+
}
52+
53+
@Override
54+
public void close() throws IOException {
55+
isClosed = true;
56+
}
57+
58+
@Override
59+
public JsonToken nextToken() throws IOException {
60+
return null;
61+
}
62+
63+
@Override
64+
public JsonToken getCurrentToken() {
65+
return null;
66+
}
67+
68+
@Override
69+
public String getCurrentName() throws IOException {
70+
return null;
71+
}
72+
73+
@Override
74+
public JsonParser skipChildren() throws IOException {
75+
return null;
76+
}
77+
78+
@Override
79+
public String getText() throws IOException {
80+
return null;
81+
}
82+
83+
@Override
84+
public byte getByteValue() throws IOException {
85+
return 0;
86+
}
87+
88+
@Override
89+
public short getShortValue() throws IOException {
90+
return 0;
91+
}
92+
93+
@Override
94+
public int getIntValue() throws IOException {
95+
return 0;
96+
}
97+
98+
@Override
99+
public float getFloatValue() throws IOException {
100+
return 0;
101+
}
102+
103+
@Override
104+
public long getLongValue() throws IOException {
105+
return 0;
106+
}
107+
108+
@Override
109+
public double getDoubleValue() throws IOException {
110+
return 0;
111+
}
112+
113+
@Override
114+
public BigInteger getBigIntegerValue() throws IOException {
115+
return null;
116+
}
117+
118+
@Override
119+
public BigDecimal getDecimalValue() throws IOException {
120+
return null;
121+
}
122+
123+
/**
124+
* Returns whether {@link #close()} was called.
125+
*
126+
* @since 1.15
127+
*/
128+
public boolean isClosed() {
129+
return isClosed;
130+
}
131+
}

0 commit comments

Comments
 (0)