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