使用gson定制化序列化/反序列化

最近接到一个需求,要求只JSON序列化简单类型字段,复杂类型字段直接忽略。我这不想到了Gson的json适配器么。这里记录下

java 复制代码
import com.google.gson.TypeAdapter;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import lombok.Data;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Data
public class OrderDto {

    private String orderId;

    private List<OrderTicketDto> tickets;

    @Data
    @JsonAdapter(OrderTicketDtoTypeAdapter.class)
    public static class OrderTicketDto{

        private Map<String,Object> ticketWrapper;

    }

    static class OrderTicketDtoTypeAdapter extends TypeAdapter<OrderTicketDto> {

        @Override
        public void write(JsonWriter out, OrderTicketDto value) throws IOException {
            out.beginObject();
            if (value.getTicketWrapper() != null) {
                for (Map.Entry<String, Object> entry : value.getTicketWrapper().entrySet()) {
                    String key = entry.getKey();
                    Object val = entry.getValue();
                    boolean isSimpleType = val instanceof String || val instanceof Number || val instanceof Boolean;
                    if (isSimpleType) {
                        out.name(key);
                        if (val instanceof String) {
                            out.value((String) val);
                        } else if (val instanceof Number) {
                            out.value((Number) val);
                        } else if (val instanceof Boolean) {
                            out.value((Boolean) val);
                        } else {
                            out.nullValue();
                        }
                    }
                }
            }
            out.endObject();
        }

        @Override
        public OrderTicketDto read(JsonReader in) throws IOException {
            OrderTicketDto ticketDto = new OrderTicketDto();
            Map<String, Object> ticketWrapper = new java.util.HashMap<>();
            
            in.beginObject();
            while (in.hasNext()) {
                String name = in.nextName();
                switch (in.peek()){
                    case STRING:
                        ticketWrapper.put(name, in.nextString());
                        break;
                    case NUMBER:
                        ticketWrapper.put(name, in.nextDouble());
                        break;
                    case BOOLEAN:
                        ticketWrapper.put(name, in.nextBoolean());
                        break;
                    default:
                        in.skipValue();
                        break;
                }

            }
            in.endObject();
            
            ticketDto.setTicketWrapper(ticketWrapper);
            return ticketDto;
        }
    }
}
相关推荐
蜗牛互联网44 分钟前
GPT-6 与 Intelligent UI:从回答到可审计操作的产品迁移
java·人工智能·后端
SL_staff1 小时前
运维救火实录:钉钉考勤API变更引发工资计算异常,我们用JVS-Logic可视化逻辑引擎实现分钟级热修复
java·mysql·全栈
SunnyDays10112 小时前
Java 查找并高亮 Word 文字:精确查找及正则表达式模糊查找
java·查找文字·word 文档·高亮文字·正则匹配查找·模糊查找
geats人山人海2 小时前
java 4继承
java·开发语言
众链网络2 小时前
Agent 的成本大头不在“写“,在“判断“:拆解专用决策模型的三层架构
java·开发语言·人工智能·微服务
周小董2 小时前
[1371]jdk管理工具
java·开发语言
青春逝如流水2 小时前
Java 中线程的创建方式:从基础到现代
java·开发语言·多线程·ai文章
Peregrine93 小时前
C++ - > 类和对象
java·c++·算法
2501_933923253 小时前
SpringCloud组件:Eureka服务注册与发现
java·开发语言·后端·spring cloud·eureka
Wang's Blog3 小时前
Java 中间件之 RabbitMQ 快速入门: SpringAMQP 消息转换器
java·中间件·java-rabbitmq