mirror of
https://github.com/jlengrand/vert.x.git
synced 2026-03-10 08:51:19 +00:00
Parsing a JSON value should fail when there is extra content in the parsed input - see #3260
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
package io.vertx.core.json.jackson;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonToken;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
@@ -129,13 +130,18 @@ public class DatabindCodec extends JacksonCodec {
|
||||
|
||||
public static <T> T fromParser(JsonParser parser, Class<T> type) throws DecodeException {
|
||||
T value;
|
||||
JsonToken remaining;
|
||||
try {
|
||||
value = DatabindCodec.mapper.readValue(parser, type);
|
||||
remaining = parser.nextToken();
|
||||
} catch (Exception e) {
|
||||
throw new DecodeException("Failed to decode:" + e.getMessage(), e);
|
||||
} finally {
|
||||
close(parser);
|
||||
}
|
||||
if (remaining != null) {
|
||||
throw new DecodeException("Unexpected trailing token");
|
||||
}
|
||||
if (type == Object.class) {
|
||||
value = (T) adapt(value);
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ package io.vertx.core.json.jackson;
|
||||
import com.fasterxml.jackson.core.JsonFactory;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonToken;
|
||||
import com.fasterxml.jackson.core.JsonTokenId;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
@@ -165,15 +166,21 @@ public class JacksonCodec implements JsonCodec {
|
||||
}
|
||||
|
||||
public static <T> T fromParser(JsonParser parser, Class<T> type) throws DecodeException {
|
||||
Object res;
|
||||
JsonToken remaining;
|
||||
try {
|
||||
parser.nextToken();
|
||||
Object res = parseAny(parser);
|
||||
return cast(res, type);
|
||||
res = parseAny(parser);
|
||||
remaining = parser.nextToken();
|
||||
} catch (IOException e) {
|
||||
throw new DecodeException(e.getMessage(), e);
|
||||
} finally {
|
||||
close(parser);
|
||||
}
|
||||
if (remaining != null) {
|
||||
throw new DecodeException("Unexpected trailing token");
|
||||
}
|
||||
return cast(res, type);
|
||||
}
|
||||
|
||||
private static Object parseAny(JsonParser parser) throws IOException, DecodeException {
|
||||
|
||||
@@ -290,7 +290,7 @@ public class JsonCodecTest {
|
||||
" line comment this time inside the JSON object itself\n" +
|
||||
"*/\n" +
|
||||
"}";
|
||||
JsonObject json = new JsonObject(jsonWithComments);
|
||||
JsonObject json = new JsonObject(mapper.fromString(jsonWithComments, Map.class));
|
||||
assertEquals("{\"foo\":\"bar\"}", mapper.toString(json));
|
||||
}
|
||||
|
||||
@@ -311,20 +311,20 @@ public class JsonCodecTest {
|
||||
" line comment this time inside the JSON array itself\n" +
|
||||
"*/\n" +
|
||||
"]";
|
||||
JsonArray json = new JsonArray(jsonWithComments);
|
||||
JsonArray json = new JsonArray(mapper.fromString(jsonWithComments, List.class));
|
||||
assertEquals("[\"foo\",\"bar\"]", mapper.toString(json));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecodeJsonObjectWithInvalidJson() {
|
||||
for (String test : new String[] { "null", "3", "\"3", "qiwjdoiqwjdiqwjd" }) {
|
||||
for (String test : new String[] { "3", "\"3", "qiwjdoiqwjdiqwjd", "{\"foo\":1},{\"bar\":2}", "{\"foo\":1} 1234" }) {
|
||||
try {
|
||||
new JsonObject(test);
|
||||
mapper.fromString(test, Map.class);
|
||||
fail();
|
||||
} catch (DecodeException ignore) {
|
||||
}
|
||||
try {
|
||||
new JsonObject(Buffer.buffer(test));
|
||||
mapper.fromBuffer(Buffer.buffer(test), Map.class);
|
||||
fail();
|
||||
} catch (DecodeException ignore) {
|
||||
}
|
||||
@@ -333,14 +333,14 @@ public class JsonCodecTest {
|
||||
|
||||
@Test
|
||||
public void testDecodeJsonArrayWithInvalidJson() {
|
||||
for (String test : new String[] { "null", "3", "\"3", "qiwjdoiqwjdiqwjd" }) {
|
||||
for (String test : new String[] { "3", "\"3", "qiwjdoiqwjdiqwjd", "[1],[2]", "[] 1234" }) {
|
||||
try {
|
||||
new JsonArray(test);
|
||||
mapper.fromString(test, List.class);
|
||||
fail();
|
||||
} catch (DecodeException ignore) {
|
||||
}
|
||||
try {
|
||||
new JsonArray(Buffer.buffer(test));
|
||||
mapper.fromBuffer(Buffer.buffer(test), List.class);
|
||||
fail();
|
||||
} catch (DecodeException ignore) {
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user