|
| 1 | +package org.tron.json; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonInclude; |
| 4 | +import com.fasterxml.jackson.core.JsonParser; |
| 5 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 6 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 7 | +import com.fasterxml.jackson.databind.JsonNode; |
| 8 | +import com.fasterxml.jackson.databind.MapperFeature; |
| 9 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 10 | +import com.fasterxml.jackson.databind.SerializationFeature; |
| 11 | +import com.fasterxml.jackson.databind.json.JsonMapper; |
| 12 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 13 | + |
| 14 | +/** |
| 15 | + * Drop-in replacement for {@code com.alibaba.fastjson.JSON}. |
| 16 | + * Swap the import line; no other source changes required for basic usages. |
| 17 | + * |
| 18 | + * <p>All static methods delegate to a shared, thread-safe Jackson {@link ObjectMapper} that is |
| 19 | + * configured to match the lenient parsing behavior historically provided by Fastjson: |
| 20 | + * <ul> |
| 21 | + * <li>Unquoted field names and single-quoted strings are accepted.</li> |
| 22 | + * <li>Unknown properties are ignored (Fastjson default).</li> |
| 23 | + * <li>Floating-point numbers are mapped to {@link java.math.BigDecimal} for precision.</li> |
| 24 | + * <li>Case-insensitive property matching is enabled.</li> |
| 25 | + * </ul> |
| 26 | + */ |
| 27 | +public final class JSON { |
| 28 | + |
| 29 | + /** |
| 30 | + * Shared, fully-configured Jackson mapper. Exposed for callers that hold a mapper reference. |
| 31 | + */ |
| 32 | + public static final ObjectMapper MAPPER = JsonMapper.builder() |
| 33 | + .configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true) |
| 34 | + .configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true) |
| 35 | + .configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, true) |
| 36 | + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) |
| 37 | + .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false) |
| 38 | + .serializationInclusion(JsonInclude.Include.NON_NULL) |
| 39 | + .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) |
| 40 | + .configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true) |
| 41 | + .build(); |
| 42 | + |
| 43 | + private JSON() { |
| 44 | + } |
| 45 | + |
| 46 | + // ------------------------------------------------------------------------- |
| 47 | + // parseObject |
| 48 | + // ------------------------------------------------------------------------- |
| 49 | + |
| 50 | + /** |
| 51 | + * Parses a JSON object string and returns a {@link JSONObject}. |
| 52 | + * Mirrors {@code JSON.parseObject(String)} and {@code JSONObject.parseObject(String)}. |
| 53 | + */ |
| 54 | + public static JSONObject parseObject(String text) { |
| 55 | + if (text == null || text.trim().isEmpty()) { |
| 56 | + return null; |
| 57 | + } |
| 58 | + try { |
| 59 | + return new JSONObject((ObjectNode) MAPPER.readTree(text)); |
| 60 | + } catch (Exception e) { |
| 61 | + throw new JSONException("Failed to parse JSON object: " + e.getMessage(), e); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Parses a JSON string and deserializes it into the given Java type. |
| 67 | + * Mirrors {@code JSON.parseObject(String, Class)}. |
| 68 | + */ |
| 69 | + public static <T> T parseObject(String text, Class<T> clazz) throws JsonProcessingException { |
| 70 | + if (text == null || text.isEmpty()) { |
| 71 | + return null; |
| 72 | + } |
| 73 | + return MAPPER.readValue(text, clazz); |
| 74 | + } |
| 75 | + |
| 76 | + // ------------------------------------------------------------------------- |
| 77 | + // parse (validate / generic parse — callers typically ignore the return value) |
| 78 | + // ------------------------------------------------------------------------- |
| 79 | + |
| 80 | + /** |
| 81 | + * Parses any valid JSON value. Throws {@link JSONException} on invalid input |
| 82 | + * so callers can detect malformed JSON (e.g. via try/catch), mirroring |
| 83 | + * Fastjson's observable behaviour in this codebase. |
| 84 | + * Mirrors {@code JSON.parse(String)}. |
| 85 | + */ |
| 86 | + public static JsonNode parse(String text) { |
| 87 | + if (text == null || text.isEmpty()) { |
| 88 | + return null; |
| 89 | + } |
| 90 | + try { |
| 91 | + return MAPPER.readTree(text); |
| 92 | + } catch (Exception e) { |
| 93 | + throw new JSONException("Failed to parse JSON: " + e.getMessage(), e); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Parses a JSON array string and returns a {@link JSONArray}. |
| 99 | + * Mirrors {@code JSON.parseArray(String)} and {@code JSONArray.parseArray(String)}. |
| 100 | + */ |
| 101 | + public static JSONArray parseArray(String text) { |
| 102 | + return JSONArray.parseArray(text); |
| 103 | + } |
| 104 | + |
| 105 | + // ------------------------------------------------------------------------- |
| 106 | + // toJSONString |
| 107 | + // ------------------------------------------------------------------------- |
| 108 | + |
| 109 | + /** |
| 110 | + * Serializes an object to a compact JSON string. |
| 111 | + * Mirrors {@code JSON.toJSONString(Object)}. |
| 112 | + */ |
| 113 | + public static String toJSONString(Object obj) { |
| 114 | + if (obj == null) { |
| 115 | + return "null"; |
| 116 | + } |
| 117 | + // Unwrap our own wrapper types so the inner Jackson node is serialized |
| 118 | + if (obj instanceof JSONObject) { |
| 119 | + return ((JSONObject) obj).unwrap().toString(); |
| 120 | + } |
| 121 | + if (obj instanceof JSONArray) { |
| 122 | + return ((JSONArray) obj).unwrap().toString(); |
| 123 | + } |
| 124 | + try { |
| 125 | + return MAPPER.writeValueAsString(obj); |
| 126 | + } catch (Exception e) { |
| 127 | + throw new JSONException("Failed to serialise object: " + e.getMessage(), e); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Serializes an object to a JSON string, optionally pretty-printed. |
| 133 | + * Mirrors {@code JSON.toJSONString(Object, boolean)}. |
| 134 | + */ |
| 135 | + public static String toJSONString(Object obj, boolean prettyFormat) { |
| 136 | + if (!prettyFormat) { |
| 137 | + return toJSONString(obj); |
| 138 | + } |
| 139 | + if (obj == null) { |
| 140 | + return "null"; |
| 141 | + } |
| 142 | + try { |
| 143 | + if (obj instanceof JSONObject) { |
| 144 | + return MAPPER.writerWithDefaultPrettyPrinter() |
| 145 | + .writeValueAsString(((JSONObject) obj).unwrap()); |
| 146 | + } |
| 147 | + if (obj instanceof JSONArray) { |
| 148 | + return MAPPER.writerWithDefaultPrettyPrinter() |
| 149 | + .writeValueAsString(((JSONArray) obj).unwrap()); |
| 150 | + } |
| 151 | + return MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(obj); |
| 152 | + } catch (Exception e) { |
| 153 | + throw new JSONException("Failed to serialise object: " + e.getMessage(), e); |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments