使用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;
        }
    }
}
相关推荐
aloha_789几秒前
软考信息系统项目管理师错误归纳总结
java·学习
vortex59 分钟前
Linux PAM 配置详解:从原理到实战,彻底阻断非授权提权
java·linux·服务器
invicinble10 分钟前
spring提供的其他机制
java·后端·spring
阿昌喜欢吃黄桃14 分钟前
如果线程池中线程异常后:销毁还是复用?
java·线程·线程池·多线程·juc
奋斗的小乌龟18 分钟前
动态创建Agent01
java·笔记
彦为君32 分钟前
Java文件处理效率库Commons-IO(速览)
java·开发语言·mfc
她的男孩1 小时前
后台权限不只是菜单隐藏:Forge Admin 的 RBAC 权限链路拆解
java·后端·架构
Slow菜鸟1 小时前
Maven 仓库下载机制
java·数据库·maven
一个诺诺前行的后端程序员1 小时前
rag+springai
java·eclipse
Hexian25801 小时前
SpringAI+RAG
java·spring·ai