|
| 1 | +/* |
| 2 | + * Copyright (c) 2011-2017 Contributors to the Eclipse Foundation |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License 2.0 which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| 7 | + * which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| 10 | + */ |
| 11 | + |
| 12 | +package io.vertx.core.json; |
| 13 | + |
| 14 | +import com.dtstack.flink.sql.util.MathUtil; |
| 15 | +import com.fasterxml.jackson.core.JsonGenerator; |
| 16 | +import com.fasterxml.jackson.core.JsonParser; |
| 17 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 18 | +import com.fasterxml.jackson.databind.*; |
| 19 | +import com.fasterxml.jackson.databind.module.SimpleModule; |
| 20 | +import io.netty.buffer.ByteBufInputStream; |
| 21 | +import io.vertx.core.buffer.Buffer; |
| 22 | + |
| 23 | +import java.io.DataInput; |
| 24 | +import java.io.IOException; |
| 25 | +import java.io.InputStream; |
| 26 | +import java.math.BigDecimal; |
| 27 | +import java.sql.Timestamp; |
| 28 | +import java.time.Instant; |
| 29 | +import java.util.Base64; |
| 30 | +import java.util.Date; |
| 31 | +import java.util.Iterator; |
| 32 | +import java.util.List; |
| 33 | +import java.util.Map; |
| 34 | +import java.util.stream.Stream; |
| 35 | +import java.util.stream.StreamSupport; |
| 36 | + |
| 37 | +import static java.time.format.DateTimeFormatter.ISO_INSTANT; |
| 38 | + |
| 39 | +/** |
| 40 | + * @author <a href="http://tfox.org">Tim Fox</a> |
| 41 | + */ |
| 42 | +public class Json { |
| 43 | + |
| 44 | + public static ObjectMapper mapper = new ObjectMapper(); |
| 45 | + public static ObjectMapper prettyMapper = new ObjectMapper(); |
| 46 | + |
| 47 | + static { |
| 48 | + // Non-standard JSON but we allow C style comments in our JSON |
| 49 | + mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true); |
| 50 | + |
| 51 | + prettyMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true); |
| 52 | + prettyMapper.configure(SerializationFeature.INDENT_OUTPUT, true); |
| 53 | + |
| 54 | + SimpleModule module = new SimpleModule(); |
| 55 | + // custom types |
| 56 | + module.addSerializer(JsonObject.class, new JsonObjectSerializer()); |
| 57 | + module.addSerializer(JsonArray.class, new JsonArraySerializer()); |
| 58 | + // he have 2 extensions: RFC-7493 |
| 59 | + module.addSerializer(Instant.class, new InstantSerializer()); |
| 60 | + module.addSerializer(byte[].class, new ByteArraySerializer()); |
| 61 | + |
| 62 | + mapper.registerModule(module); |
| 63 | + prettyMapper.registerModule(module); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Encode a POJO to JSON using the underlying Jackson mapper. |
| 68 | + * |
| 69 | + * @param obj a POJO |
| 70 | + * @return a String containing the JSON representation of the given POJO. |
| 71 | + * @throws EncodeException if a property cannot be encoded. |
| 72 | + */ |
| 73 | + public static String encode(Object obj) throws EncodeException { |
| 74 | + try { |
| 75 | + return mapper.writeValueAsString(obj); |
| 76 | + } catch (Exception e) { |
| 77 | + throw new EncodeException("Failed to encode as JSON: " + e.getMessage()); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Encode a POJO to JSON using the underlying Jackson mapper. |
| 83 | + * |
| 84 | + * @param obj a POJO |
| 85 | + * @return a Buffer containing the JSON representation of the given POJO. |
| 86 | + * @throws EncodeException if a property cannot be encoded. |
| 87 | + */ |
| 88 | + public static Buffer encodeToBuffer(Object obj) throws EncodeException { |
| 89 | + try { |
| 90 | + return Buffer.buffer(mapper.writeValueAsBytes(obj)); |
| 91 | + } catch (Exception e) { |
| 92 | + throw new EncodeException("Failed to encode as JSON: " + e.getMessage()); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Encode a POJO to JSON with pretty indentation, using the underlying Jackson mapper. |
| 98 | + * |
| 99 | + * @param obj a POJO |
| 100 | + * @return a String containing the JSON representation of the given POJO. |
| 101 | + * @throws EncodeException if a property cannot be encoded. |
| 102 | + */ |
| 103 | + public static String encodePrettily(Object obj) throws EncodeException { |
| 104 | + try { |
| 105 | + return prettyMapper.writeValueAsString(obj); |
| 106 | + } catch (Exception e) { |
| 107 | + throw new EncodeException("Failed to encode as JSON: " + e.getMessage()); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Decode a given JSON string to a POJO of the given class type. |
| 113 | + * @param str the JSON string. |
| 114 | + * @param clazz the class to map to. |
| 115 | + * @param <T> the generic type. |
| 116 | + * @return an instance of T |
| 117 | + * @throws DecodeException when there is a parsing or invalid mapping. |
| 118 | + */ |
| 119 | + public static <T> T decodeValue(String str, Class<T> clazz) throws DecodeException { |
| 120 | + try { |
| 121 | + return mapper.readValue(str, clazz); |
| 122 | + } catch (Exception e) { |
| 123 | + throw new DecodeException("Failed to decode: " + e.getMessage()); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Decode a given JSON string to a POJO of the given type. |
| 129 | + * @param str the JSON string. |
| 130 | + * @param type the type to map to. |
| 131 | + * @param <T> the generic type. |
| 132 | + * @return an instance of T |
| 133 | + * @throws DecodeException when there is a parsing or invalid mapping. |
| 134 | + */ |
| 135 | + public static <T> T decodeValue(String str, TypeReference<T> type) throws DecodeException { |
| 136 | + try { |
| 137 | + return mapper.readValue(str, type); |
| 138 | + } catch (Exception e) { |
| 139 | + throw new DecodeException("Failed to decode: " + e.getMessage(), e); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Decode a given JSON buffer to a POJO of the given class type. |
| 145 | + * @param buf the JSON buffer. |
| 146 | + * @param type the type to map to. |
| 147 | + * @param <T> the generic type. |
| 148 | + * @return an instance of T |
| 149 | + * @throws DecodeException when there is a parsing or invalid mapping. |
| 150 | + */ |
| 151 | + public static <T> T decodeValue(Buffer buf, TypeReference<T> type) throws DecodeException { |
| 152 | + try { |
| 153 | + return mapper.readValue(new ByteBufInputStream(buf.getByteBuf()), type); |
| 154 | + } catch (Exception e) { |
| 155 | + throw new DecodeException("Failed to decode:" + e.getMessage(), e); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Decode a given JSON buffer to a POJO of the given class type. |
| 161 | + * @param buf the JSON buffer. |
| 162 | + * @param clazz the class to map to. |
| 163 | + * @param <T> the generic type. |
| 164 | + * @return an instance of T |
| 165 | + * @throws DecodeException when there is a parsing or invalid mapping. |
| 166 | + */ |
| 167 | + public static <T> T decodeValue(Buffer buf, Class<T> clazz) throws DecodeException { |
| 168 | + try { |
| 169 | + return mapper.readValue((InputStream) new ByteBufInputStream(buf.getByteBuf()), clazz); |
| 170 | + } catch (Exception e) { |
| 171 | + throw new DecodeException("Failed to decode:" + e.getMessage(), e); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + @SuppressWarnings("unchecked") |
| 176 | + static Object checkAndCopy(Object val, boolean copy) { |
| 177 | + if (val == null) { |
| 178 | + // OK |
| 179 | + } else if (val instanceof Number && !(val instanceof BigDecimal)) { |
| 180 | + // OK |
| 181 | + } else if (val instanceof Boolean) { |
| 182 | + // OK |
| 183 | + } else if (val instanceof String) { |
| 184 | + // OK |
| 185 | + } else if (val instanceof Character) { |
| 186 | + // OK |
| 187 | + } else if (val instanceof CharSequence) { |
| 188 | + val = val.toString(); |
| 189 | + } else if (val instanceof JsonObject) { |
| 190 | + if (copy) { |
| 191 | + val = ((JsonObject) val).copy(); |
| 192 | + } |
| 193 | + } else if (val instanceof JsonArray) { |
| 194 | + if (copy) { |
| 195 | + val = ((JsonArray) val).copy(); |
| 196 | + } |
| 197 | + } else if (val instanceof Map) { |
| 198 | + if (copy) { |
| 199 | + val = (new JsonObject((Map)val)).copy(); |
| 200 | + } else { |
| 201 | + val = new JsonObject((Map)val); |
| 202 | + } |
| 203 | + } else if (val instanceof List) { |
| 204 | + if (copy) { |
| 205 | + val = (new JsonArray((List)val)).copy(); |
| 206 | + } else { |
| 207 | + val = new JsonArray((List)val); |
| 208 | + } |
| 209 | + } else if (val instanceof byte[]) { |
| 210 | + val = Base64.getEncoder().encodeToString((byte[]) val); |
| 211 | + } else if (val instanceof Instant) { |
| 212 | + val = ISO_INSTANT.format((Instant) val); |
| 213 | + } else if (val instanceof Timestamp) { |
| 214 | + val = MathUtil.getStringFromTimestamp((Timestamp) val); |
| 215 | + } else if (val instanceof Date) { |
| 216 | + val = MathUtil.getStringFromDate((java.sql.Date) val); |
| 217 | + } else { |
| 218 | + val = val.toString(); |
| 219 | + } |
| 220 | + return val; |
| 221 | + } |
| 222 | + |
| 223 | + static <T> Stream<T> asStream(Iterator<T> sourceIterator) { |
| 224 | + Iterable<T> iterable = () -> sourceIterator; |
| 225 | + return StreamSupport.stream(iterable.spliterator(), false); |
| 226 | + } |
| 227 | + |
| 228 | + private static class JsonObjectSerializer extends JsonSerializer<JsonObject> { |
| 229 | + @Override |
| 230 | + public void serialize(JsonObject value, JsonGenerator jgen, SerializerProvider provider) throws IOException { |
| 231 | + jgen.writeObject(value.getMap()); |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + private static class JsonArraySerializer extends JsonSerializer<JsonArray> { |
| 236 | + @Override |
| 237 | + public void serialize(JsonArray value, JsonGenerator jgen, SerializerProvider provider) throws IOException { |
| 238 | + jgen.writeObject(value.getList()); |
| 239 | + } |
| 240 | + } |
| 241 | + |
| 242 | + private static class InstantSerializer extends JsonSerializer<Instant> { |
| 243 | + @Override |
| 244 | + public void serialize(Instant value, JsonGenerator jgen, SerializerProvider provider) throws IOException { |
| 245 | + jgen.writeString(ISO_INSTANT.format(value)); |
| 246 | + } |
| 247 | + } |
| 248 | + |
| 249 | + private static class ByteArraySerializer extends JsonSerializer<byte[]> { |
| 250 | + private final Base64.Encoder BASE64 = Base64.getEncoder(); |
| 251 | + |
| 252 | + @Override |
| 253 | + public void serialize(byte[] value, JsonGenerator jgen, SerializerProvider provider) throws IOException { |
| 254 | + jgen.writeString(BASE64.encodeToString(value)); |
| 255 | + } |
| 256 | + } |
| 257 | +} |
0 commit comments