使用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;
        }
    }
}
相关推荐
贼爱学习的小黄1 小时前
NC BIP参照开发
java·前端·nc
小江的记录本1 小时前
【MyBatis-Plus】MyBatis-Plus的核心特性、条件构造器、分页插件、乐观锁插件
java·前端·spring boot·后端·sql·tomcat·mybatis
小张会进步1 小时前
数组:二维数组
java·javascript·算法
vx-程序开发1 小时前
springboot在线装修管理系统-计算机毕业设计源码56278
java·c语言·spring boot·python·spring·django·php
大傻^1 小时前
Spring AI Alibaba 可观测性实践:AI应用监控与链路追踪
java·人工智能·后端·spring·springaialibaba
云烟成雨TD1 小时前
Spring AI Alibaba 1.x 系列【1】阿里巴巴 AI 生态
java·人工智能·spring
诗人不写诗1 小时前
spring是如何组织切面的
java·后端·spring
大傻^2 小时前
Spring AI Alibaba Agent开发:基于ChatClient的智能体构建模式
java·数据库·人工智能·后端·spring·springaialibaba
li星野2 小时前
C++面试真题分享20260320
java·c++·面试
Irissgwe2 小时前
c++特殊类设计
java·开发语言·c++