package io.vertx.core.json;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import com.fasterxml.jackson.databind.module.SimpleModule;
import io.netty.buffer.ByteBufInputStream;
import io.vertx.core.buffer.Buffer;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.time.DateTimeException;
import java.time.Instant;
import java.util.Base64;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import static java.time.format.DateTimeFormatter.ISO_INSTANT;
public class Json {
public static ObjectMapper mapper = new ObjectMapper();
public static ObjectMapper prettyMapper = new ObjectMapper();
static {
mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
prettyMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
prettyMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
SimpleModule module = new SimpleModule();
module.addSerializer(JsonObject.class, new JsonObjectSerializer());
module.addSerializer(JsonArray.class, new JsonArraySerializer());
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);
}
public static String encode(Object obj) throws EncodeException {
try {
return mapper.writeValueAsString(obj);
} catch (Exception e) {
throw new EncodeException("Failed to encode as JSON: " + e.getMessage());
}
}
public static Buffer encodeToBuffer(Object obj) throws EncodeException {
try {
return Buffer.buffer(mapper.writeValueAsBytes(obj));
} catch (Exception e) {
throw new EncodeException("Failed to encode as JSON: " + e.getMessage());
}
}
public static String encodePrettily(Object obj) throws EncodeException {
try {
return prettyMapper.writeValueAsString(obj);
} catch (Exception e) {
throw new EncodeException("Failed to encode as JSON: " + e.getMessage());
}
}
public static <T> T decodeValue(String str, Class<T> clazz) throws DecodeException {
try {
return mapper.readValue(str, clazz);
} catch (Exception e) {
throw new DecodeException("Failed to decode: " + e.getMessage());
}
}
public static Object decodeValue(String str) throws DecodeException {
try {
Object value = mapper.readValue(str, Object.class);
if (value instanceof List) {
List list = (List) value;
return new JsonArray(list);
} else if (value instanceof Map) {
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) value;
return new JsonObject(map);
}
return value;
} catch (Exception e) {
throw new DecodeException("Failed to decode: " + e.getMessage());
}
}
public static <T> T decodeValue(String str, TypeReference<T> type) throws DecodeException {
try {
return mapper.readValue(str, type);
} catch (Exception e) {
throw new DecodeException("Failed to decode: " + e.getMessage(), e);
}
}
public static Object decodeValue(Buffer buf) throws DecodeException {
try {
Object value = mapper.readValue((InputStream) new ByteBufInputStream(buf.getByteBuf()), Object.class);
if (value instanceof List) {
List list = (List) value;
return new JsonArray(list);
} else if (value instanceof Map) {
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) value;
return new JsonObject(map);
}
return value;
} catch (Exception e) {
throw new DecodeException("Failed to decode: " + e.getMessage());
}
}
public static <T> T decodeValue(Buffer buf, TypeReference<T> type) throws DecodeException {
try {
return mapper.readValue(new ByteBufInputStream(buf.getByteBuf()), type);
} catch (Exception e) {
throw new DecodeException("Failed to decode:" + e.getMessage(), e);
}
}
public static <T> T decodeValue(Buffer buf, Class<T> clazz) throws DecodeException {
try {
return mapper.readValue((InputStream) new ByteBufInputStream(buf.getByteBuf()), clazz);
} catch (Exception e) {
throw new DecodeException("Failed to decode:" + e.getMessage(), e);
}
}
@SuppressWarnings("unchecked")
static Object checkAndCopy(Object val, boolean copy) {
if (val == null) {
} else if (val instanceof Number && !(val instanceof BigDecimal)) {
} else if (val instanceof Boolean) {
} else if (val instanceof String) {
} else if (val instanceof Character) {
} else if (val instanceof CharSequence) {
val = val.toString();
} else if (val instanceof JsonObject) {
if (copy) {
val = ((JsonObject) val).copy();
}
} else if (val instanceof JsonArray) {
if (copy) {
val = ((JsonArray) val).copy();
}
} else if (val instanceof Map) {
if (copy) {
val = (new JsonObject((Map)val)).copy();
} else {
val = new JsonObject((Map)val);
}
} else if (val instanceof List) {
if (copy) {
val = (new JsonArray((List)val)).copy();
} else {
val = new JsonArray((List)val);
}
} else if (val instanceof byte[]) {
val = Base64.getEncoder().encodeToString((byte[])val);
} else if (val instanceof Instant) {
val = ISO_INSTANT.format((Instant) val);
} else {
throw new IllegalStateException("Illegal type in JsonObject: " + val.getClass());
}
return val;
}
static <T> Stream<T> asStream(Iterator<T> sourceIterator) {
Iterable<T> iterable = () -> sourceIterator;
return StreamSupport.stream(iterable.spliterator(), false);
}
private static class JsonObjectSerializer extends JsonSerializer<JsonObject> {
@Override
public void serialize(JsonObject value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
jgen.writeObject(value.getMap());
}
}
private static class JsonArraySerializer extends JsonSerializer<JsonArray> {
@Override
public void serialize(JsonArray value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
jgen.writeObject(value.getList());
}
}
private static class InstantSerializer extends JsonSerializer<Instant> {
@Override
public void serialize(Instant value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
jgen.writeString(ISO_INSTANT.format(value));
}
}
private static class InstantDeserializer extends JsonDeserializer<Instant> {
@Override
public Instant deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
String text = p.getText();
try {
return Instant.from(ISO_INSTANT.parse(text));
} catch (DateTimeException e) {
throw new InvalidFormatException(p, "Expected an ISO 8601 formatted date time", text, Instant.class);
}
}
}
private static class ByteArraySerializer extends JsonSerializer<byte[]> {
@Override
public void serialize(byte[] value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
jgen.writeString(Base64.getEncoder().encodeToString(value));
}
}
private static class ByteArrayDeserializer extends JsonDeserializer<byte[]> {
@Override
public byte[] deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
String text = p.getText();
try {
return Base64.getDecoder().decode(text);
} catch (IllegalArgumentException e) {
throw new InvalidFormatException(p, "Expected a base64 encoded byte array", text, Instant.class);
}
}
}
}