mirror of
https://github.com/jlengrand/vert.x.git
synced 2026-03-10 08:51:19 +00:00
Move jackson usage from Json to JacksonJsonFactory
This commit is contained in:
@@ -11,50 +11,14 @@
|
||||
|
||||
package io.vertx.core.json;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
import io.vertx.core.buffer.Buffer;
|
||||
import io.vertx.core.json.impl.JacksonCodec;
|
||||
import io.vertx.core.spi.json.JsonCodec;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
/**
|
||||
* @author <a href="http://tfox.org">Tim Fox</a>
|
||||
*/
|
||||
public class Json {
|
||||
|
||||
public static ObjectMapper mapper = new ObjectMapper();
|
||||
public static ObjectMapper prettyMapper = new ObjectMapper();
|
||||
|
||||
static {
|
||||
initialize();
|
||||
}
|
||||
|
||||
private static void initialize() {
|
||||
// Non-standard JSON but we allow C style comments in our JSON
|
||||
mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
|
||||
|
||||
prettyMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
|
||||
prettyMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
|
||||
|
||||
SimpleModule module = new SimpleModule();
|
||||
// custom types
|
||||
module.addSerializer(JsonObject.class, new JsonObjectSerializer());
|
||||
module.addSerializer(JsonArray.class, new JsonArraySerializer());
|
||||
// he have 2 extensions: RFC-7493
|
||||
module.addSerializer(Instant.class, new InstantSerializer());
|
||||
module.addDeserializer(Instant.class, new InstantDeserializer());
|
||||
module.addSerializer(byte[].class, new ByteArraySerializer());
|
||||
module.addDeserializer(byte[].class, new ByteArrayDeserializer());
|
||||
|
||||
mapper.registerModule(module);
|
||||
prettyMapper.registerModule(module);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode a POJO to JSON using the underlying Jackson mapper.
|
||||
*
|
||||
@@ -112,18 +76,6 @@ public class Json {
|
||||
return decodeValue(str, Object.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode a given JSON string to a POJO of the given type.
|
||||
* @param str the JSON string.
|
||||
* @param type the type to map to.
|
||||
* @param <T> the generic type.
|
||||
* @return an instance of T
|
||||
* @throws DecodeException when there is a parsing or invalid mapping.
|
||||
*/
|
||||
public static <T> T decodeValue(String str, TypeReference<T> type) throws DecodeException {
|
||||
return JacksonCodec.fromString(str, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode a given JSON buffer.
|
||||
*
|
||||
@@ -136,18 +88,6 @@ public class Json {
|
||||
return decodeValue(buf, Object.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode a given JSON buffer to a POJO of the given class type.
|
||||
* @param buf the JSON buffer.
|
||||
* @param type the type to map to.
|
||||
* @param <T> the generic type.
|
||||
* @return an instance of T
|
||||
* @throws DecodeException when there is a parsing or invalid mapping.
|
||||
*/
|
||||
public static <T> T decodeValue(Buffer buf, TypeReference<T> type) throws DecodeException {
|
||||
return JacksonCodec.fromBuffer(buf, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode a given JSON buffer to a POJO of the given class type.
|
||||
* @param buf the JSON buffer.
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
|
||||
*/
|
||||
package io.vertx.core.json;
|
||||
package io.vertx.core.json.impl;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
@@ -8,7 +8,7 @@
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
|
||||
*/
|
||||
package io.vertx.core.json;
|
||||
package io.vertx.core.json.impl;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
@@ -8,7 +8,7 @@
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
|
||||
*/
|
||||
package io.vertx.core.json;
|
||||
package io.vertx.core.json.impl;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
@@ -8,7 +8,7 @@
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
|
||||
*/
|
||||
package io.vertx.core.json;
|
||||
package io.vertx.core.json.impl;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
@@ -14,17 +14,19 @@ package io.vertx.core.json.impl;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
import io.netty.buffer.ByteBufInputStream;
|
||||
import io.vertx.core.buffer.Buffer;
|
||||
import io.vertx.core.json.DecodeException;
|
||||
import io.vertx.core.json.EncodeException;
|
||||
import io.vertx.core.json.Json;
|
||||
import io.vertx.core.json.JsonArray;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
import io.vertx.core.spi.json.JsonCodec;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -33,9 +35,37 @@ import java.util.Map;
|
||||
*/
|
||||
public class JacksonCodec implements JsonCodec {
|
||||
|
||||
public static ObjectMapper mapper = new ObjectMapper();
|
||||
public static ObjectMapper prettyMapper = new ObjectMapper();
|
||||
|
||||
static {
|
||||
initialize();
|
||||
}
|
||||
|
||||
private static void initialize() {
|
||||
// Non-standard JSON but we allow C style comments in our JSON
|
||||
mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
|
||||
|
||||
prettyMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
|
||||
prettyMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
|
||||
|
||||
SimpleModule module = new SimpleModule();
|
||||
// custom types
|
||||
module.addSerializer(JsonObject.class, new JsonObjectSerializer());
|
||||
module.addSerializer(JsonArray.class, new JsonArraySerializer());
|
||||
// he have 2 extensions: RFC-7493
|
||||
module.addSerializer(Instant.class, new InstantSerializer());
|
||||
module.addDeserializer(Instant.class, new InstantDeserializer());
|
||||
module.addSerializer(byte[].class, new ByteArraySerializer());
|
||||
module.addDeserializer(byte[].class, new ByteArrayDeserializer());
|
||||
|
||||
mapper.registerModule(module);
|
||||
prettyMapper.registerModule(module);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T fromValue(Object json, Class<T> clazz) {
|
||||
T value = Json.mapper.convertValue(json, clazz);
|
||||
T value = JacksonCodec.mapper.convertValue(json, clazz);
|
||||
if (clazz == Object.class) {
|
||||
value = (T) adapt(value);
|
||||
}
|
||||
@@ -43,7 +73,7 @@ public class JacksonCodec implements JsonCodec {
|
||||
}
|
||||
|
||||
public static <T> T fromValue(Object json, TypeReference<T> type) {
|
||||
T value = Json.mapper.convertValue(json, type);
|
||||
T value = JacksonCodec.mapper.convertValue(json, type);
|
||||
if (type.getType() == Object.class) {
|
||||
value = (T) adapt(value);
|
||||
}
|
||||
@@ -70,7 +100,7 @@ public class JacksonCodec implements JsonCodec {
|
||||
|
||||
private static JsonParser createParser(Buffer buf) {
|
||||
try {
|
||||
return Json.mapper.getFactory().createParser((InputStream) new ByteBufInputStream(buf.getByteBuf()));
|
||||
return JacksonCodec.mapper.getFactory().createParser((InputStream) new ByteBufInputStream(buf.getByteBuf()));
|
||||
} catch (IOException e) {
|
||||
throw new DecodeException("Failed to decode:" + e.getMessage(), e);
|
||||
}
|
||||
@@ -78,7 +108,7 @@ public class JacksonCodec implements JsonCodec {
|
||||
|
||||
private static JsonParser createParser(String str) {
|
||||
try {
|
||||
return Json.mapper.getFactory().createParser(str);
|
||||
return JacksonCodec.mapper.getFactory().createParser(str);
|
||||
} catch (IOException e) {
|
||||
throw new DecodeException("Failed to decode:" + e.getMessage(), e);
|
||||
}
|
||||
@@ -87,7 +117,7 @@ public class JacksonCodec implements JsonCodec {
|
||||
private static <T> T fromParser(JsonParser parser, Class<T> type) throws DecodeException {
|
||||
T value;
|
||||
try {
|
||||
value = Json.mapper.readValue(parser, type);
|
||||
value = JacksonCodec.mapper.readValue(parser, type);
|
||||
} catch (Exception e) {
|
||||
throw new DecodeException("Failed to decode:" + e.getMessage(), e);
|
||||
} finally {
|
||||
@@ -102,7 +132,7 @@ public class JacksonCodec implements JsonCodec {
|
||||
private static <T> T fromParser(JsonParser parser, TypeReference<T> type) throws DecodeException {
|
||||
T value;
|
||||
try {
|
||||
value = Json.mapper.readValue(parser, type);
|
||||
value = JacksonCodec.mapper.readValue(parser, type);
|
||||
} catch (Exception e) {
|
||||
throw new DecodeException("Failed to decode:" + e.getMessage(), e);
|
||||
} finally {
|
||||
@@ -141,7 +171,7 @@ public class JacksonCodec implements JsonCodec {
|
||||
@Override
|
||||
public String toString(Object object, boolean pretty) throws EncodeException {
|
||||
try {
|
||||
ObjectMapper mapper = pretty ? Json.prettyMapper : Json.mapper;
|
||||
ObjectMapper mapper = pretty ? JacksonCodec.prettyMapper : JacksonCodec.mapper;
|
||||
return mapper.writeValueAsString(object);
|
||||
} catch (Exception e) {
|
||||
throw new EncodeException("Failed to encode as JSON: " + e.getMessage());
|
||||
@@ -151,7 +181,7 @@ public class JacksonCodec implements JsonCodec {
|
||||
@Override
|
||||
public Buffer toBuffer(Object object, boolean pretty) throws EncodeException {
|
||||
try {
|
||||
ObjectMapper mapper = pretty ? Json.prettyMapper : Json.mapper;
|
||||
ObjectMapper mapper = pretty ? JacksonCodec.prettyMapper : JacksonCodec.mapper;
|
||||
return Buffer.buffer(mapper.writeValueAsBytes(object));
|
||||
} catch (Exception e) {
|
||||
throw new EncodeException("Failed to encode as JSON: " + e.getMessage());
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
package io.vertx.core.json;
|
||||
package io.vertx.core.json.impl;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import io.vertx.core.json.JsonArray;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@@ -8,11 +8,12 @@
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
|
||||
*/
|
||||
package io.vertx.core.json;
|
||||
package io.vertx.core.json.impl;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@@ -22,9 +22,9 @@ import io.vertx.core.VertxException;
|
||||
import io.vertx.core.buffer.Buffer;
|
||||
import io.vertx.core.impl.Arguments;
|
||||
import io.vertx.core.json.DecodeException;
|
||||
import io.vertx.core.json.Json;
|
||||
import io.vertx.core.json.JsonArray;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
import io.vertx.core.json.impl.JacksonCodec;
|
||||
import io.vertx.core.parsetools.JsonEvent;
|
||||
import io.vertx.core.parsetools.JsonEventType;
|
||||
import io.vertx.core.parsetools.JsonParser;
|
||||
@@ -445,7 +445,7 @@ public class JsonParserImpl implements JsonParser {
|
||||
|
||||
<T> T convert(Class<T> type) {
|
||||
try {
|
||||
return Json.mapper.readValue(buffer, type);
|
||||
return JacksonCodec.mapper.readValue(buffer, type);
|
||||
} catch (Exception e) {
|
||||
throw new DecodeException(e.getMessage(), e);
|
||||
}
|
||||
|
||||
@@ -13,8 +13,8 @@ package io.vertx.benchmarks;
|
||||
|
||||
import io.netty.util.CharsetUtil;
|
||||
import io.vertx.core.buffer.Buffer;
|
||||
import io.vertx.core.json.Json;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
import io.vertx.core.json.impl.JacksonCodec;
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.CompilerControl;
|
||||
import org.openjdk.jmh.annotations.Scope;
|
||||
@@ -47,7 +47,7 @@ public class JsonDecodeBenchmark extends BenchmarkBase {
|
||||
|
||||
private Buffer loadJsonAsBuffer(URL url) {
|
||||
try {
|
||||
Buffer encoded = new JsonObject(Json.mapper.readValue(url, Map.class)).toBuffer();
|
||||
Buffer encoded = new JsonObject(JacksonCodec.mapper.readValue(url, Map.class)).toBuffer();
|
||||
return Buffer.buffer().appendInt(encoded.length()).appendBuffer(encoded);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
|
||||
@@ -13,8 +13,8 @@ package io.vertx.benchmarks;
|
||||
|
||||
import io.netty.util.CharsetUtil;
|
||||
import io.vertx.core.buffer.Buffer;
|
||||
import io.vertx.core.json.Json;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
import io.vertx.core.json.impl.JacksonCodec;
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.CompilerControl;
|
||||
import org.openjdk.jmh.annotations.Scope;
|
||||
@@ -47,7 +47,7 @@ public class JsonEncodeBenchmark extends BenchmarkBase {
|
||||
|
||||
private JsonObject loadJson(URL url) {
|
||||
try {
|
||||
return new JsonObject(Json.mapper.readValue(url, Map.class));
|
||||
return new JsonObject(JacksonCodec.mapper.readValue(url, Map.class));
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.vertx.core.buffer.Buffer;
|
||||
import io.vertx.core.json.impl.JacksonCodec;
|
||||
import io.vertx.test.core.TestUtils;
|
||||
import io.vertx.test.core.VertxTestBase;
|
||||
import org.junit.Test;
|
||||
@@ -31,22 +32,22 @@ public class JacksonDatabindTest extends VertxTestBase {
|
||||
|
||||
@Test
|
||||
public void testGetSetMapper() {
|
||||
ObjectMapper mapper = Json.mapper;
|
||||
ObjectMapper mapper = JacksonCodec.mapper;
|
||||
assertNotNull(mapper);
|
||||
ObjectMapper newMapper = new ObjectMapper();
|
||||
Json.mapper = newMapper;
|
||||
assertSame(newMapper, Json.mapper);
|
||||
Json.mapper = mapper;
|
||||
JacksonCodec.mapper = newMapper;
|
||||
assertSame(newMapper, JacksonCodec.mapper);
|
||||
JacksonCodec.mapper = mapper;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSetPrettyMapper() {
|
||||
ObjectMapper mapper = Json.prettyMapper;
|
||||
ObjectMapper mapper = JacksonCodec.prettyMapper;
|
||||
assertNotNull(mapper);
|
||||
ObjectMapper newMapper = new ObjectMapper();
|
||||
Json.prettyMapper = newMapper;
|
||||
assertSame(newMapper, Json.prettyMapper);
|
||||
Json.prettyMapper = mapper;
|
||||
JacksonCodec.prettyMapper = newMapper;
|
||||
assertSame(newMapper, JacksonCodec.prettyMapper);
|
||||
JacksonCodec.prettyMapper = mapper;
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -57,12 +58,12 @@ public class JacksonDatabindTest extends VertxTestBase {
|
||||
String json = Json.encode(Collections.singletonList(original));
|
||||
List<Pojo> correct;
|
||||
|
||||
correct = Json.decodeValue(json, new TypeReference<List<Pojo>>() {});
|
||||
correct = JacksonCodec.fromString(json, new TypeReference<List<Pojo>>() {});
|
||||
assertTrue(((List)correct).get(0) instanceof Pojo);
|
||||
assertEquals(original.value, correct.get(0).value);
|
||||
|
||||
// same must apply if instead of string we use a buffer
|
||||
correct = Json.decodeValue(Buffer.buffer(json, "UTF8"), new TypeReference<List<Pojo>>() {});
|
||||
correct = JacksonCodec.fromBuffer(Buffer.buffer(json, "UTF8"), new TypeReference<List<Pojo>>() {});
|
||||
assertTrue(((List)correct).get(0) instanceof Pojo);
|
||||
assertEquals(original.value, correct.get(0).value);
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ package io.vertx.core.json;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.vertx.core.buffer.Buffer;
|
||||
import io.vertx.core.impl.Utils;
|
||||
import io.vertx.core.json.impl.JacksonCodec;
|
||||
import io.vertx.test.core.TestUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -380,14 +381,14 @@ public class JsonCodecTest {
|
||||
Type type = ref.getType();
|
||||
Class<?> clazz = type instanceof Class ? (Class<?>) type : (Class<?>) ((ParameterizedType) type).getRawType();
|
||||
assertEquals(expected, Json.decodeValue(buffer, clazz));
|
||||
assertEquals(expected, Json.decodeValue(buffer, ref));
|
||||
assertEquals(expected, JacksonCodec.fromBuffer(buffer, ref));
|
||||
assertEquals(expected, Json.decodeValue(buffer.toString(StandardCharsets.UTF_8), clazz));
|
||||
assertEquals(expected, Json.decodeValue(buffer.toString(StandardCharsets.UTF_8), ref));
|
||||
assertEquals(expected, JacksonCodec.fromString(buffer.toString(StandardCharsets.UTF_8), ref));
|
||||
Buffer nullValue = Buffer.buffer("null");
|
||||
assertNull(Json.decodeValue(nullValue, clazz));
|
||||
assertNull(Json.decodeValue(nullValue, ref));
|
||||
assertNull(JacksonCodec.fromBuffer(nullValue, ref));
|
||||
assertNull(Json.decodeValue(nullValue.toString(StandardCharsets.UTF_8), clazz));
|
||||
assertNull(Json.decodeValue(nullValue.toString(StandardCharsets.UTF_8), ref));
|
||||
assertNull(JacksonCodec.fromString(nullValue.toString(StandardCharsets.UTF_8), ref));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user