Showing
2 changed files
with
63 additions
and
1 deletions
| 1 | +package com.tianting.infoloop.utils; | ||
| 2 | + | ||
| 3 | +import com.fasterxml.jackson.annotation.JsonInclude; | ||
| 4 | +import com.fasterxml.jackson.core.JsonProcessingException; | ||
| 5 | +import com.fasterxml.jackson.core.type.TypeReference; | ||
| 6 | +import com.fasterxml.jackson.databind.DeserializationFeature; | ||
| 7 | +import com.fasterxml.jackson.databind.JsonNode; | ||
| 8 | +import com.fasterxml.jackson.databind.ObjectMapper; | ||
| 9 | +import com.fasterxml.jackson.databind.module.SimpleModule; | ||
| 10 | +import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; | ||
| 11 | + | ||
| 12 | +import java.io.IOException; | ||
| 13 | +import java.util.List; | ||
| 14 | + | ||
| 15 | +public final class JSONUtil { | ||
| 16 | + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); | ||
| 17 | + | ||
| 18 | + static { | ||
| 19 | + final SimpleModule module = new SimpleModule(); | ||
| 20 | + module.addSerializer(Long.class, ToStringSerializer.instance); | ||
| 21 | + module.addSerializer(long.class, ToStringSerializer.instance); | ||
| 22 | + OBJECT_MAPPER.registerModule(module); | ||
| 23 | + // 如果json中有新增的字段并且是实体类类中不存在的,不报错 | ||
| 24 | + OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); | ||
| 25 | + // 序列化时忽略值为null的属性 | ||
| 26 | + OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL); | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + public static String writeAsJson(final Object object) { | ||
| 30 | + try { | ||
| 31 | + return OBJECT_MAPPER.writeValueAsString(object); | ||
| 32 | + } catch (final JsonProcessingException e) { | ||
| 33 | + throw new RuntimeException(e); | ||
| 34 | + } | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | + public static <T> T readJsonAs(final String json, final Class<T> clazz) { | ||
| 38 | + try { | ||
| 39 | + return OBJECT_MAPPER.readerFor(clazz).readValue(json); | ||
| 40 | + } catch (final IOException e) { | ||
| 41 | + throw new RuntimeException(e); | ||
| 42 | + } | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + public static <T> List<T> readJsonAsList(String jsonString, Class<T> clazz) { | ||
| 46 | + try { | ||
| 47 | + return OBJECT_MAPPER.readValue(jsonString, new TypeReference<>() { | ||
| 48 | + }); | ||
| 49 | + } catch (Exception e) { | ||
| 50 | + throw new RuntimeException("Failed to convert JSON to List<" + clazz.getSimpleName() + ">", e); | ||
| 51 | + } | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + public static <T> JsonNode readTree(final String json) { | ||
| 55 | + try { | ||
| 56 | + return OBJECT_MAPPER.readTree(json); | ||
| 57 | + } catch (final IOException e) { | ||
| 58 | + throw new RuntimeException(e); | ||
| 59 | + } | ||
| 60 | + } | ||
| 61 | + private JSONUtil() {} | ||
| 62 | +} |
| ... | @@ -104,7 +104,7 @@ public class ProtoBeanUtil { | ... | @@ -104,7 +104,7 @@ public class ProtoBeanUtil { |
| 104 | */ | 104 | */ |
| 105 | private static <M extends Message.Builder> void jsonToBuilder(M targetBuilder, String json) { | 105 | private static <M extends Message.Builder> void jsonToBuilder(M targetBuilder, String json) { |
| 106 | try { | 106 | try { |
| 107 | - JsonFormat.parser().ignoringUnknownFields().merge(json, targetBuilder); | 107 | + JsonFormat.parser().ignoringUnknownFields().merge(JSONUtil.writeAsJson(json), targetBuilder); |
| 108 | } catch (Exception e) { | 108 | } catch (Exception e) { |
| 109 | throw new IllegalArgumentException("ProtoBeanUtil->JsonToBuilder->errorMessage:json 转 Builder对象异常", e); | 109 | throw new IllegalArgumentException("ProtoBeanUtil->JsonToBuilder->errorMessage:json 转 Builder对象异常", e); |
| 110 | } | 110 | } | ... | ... |
-
Please register or login to post a comment