Files
auto-solution/auto-solution-rule/src/main/java/com/solution/rule/parser/JsonStreamParser.java
2026-03-27 10:27:04 +08:00

120 lines
5.4 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//package com.solution.rule.parser;
//
//import com.fasterxml.jackson.core.JsonParser;
//import com.fasterxml.jackson.core.JsonToken;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import javax.servlet.http.HttpServletRequest;
//import java.io.IOException;
//import java.util.*;
//
//public class JsonStreamParser {
//
// // 核心:解析所有集合 → 内存关联ID → 组装完整数据
// public List<AssembledWeapon> parseAndAssemble(HttpServletRequest request) throws IOException {
// ObjectMapper mapper = new ObjectMapper();
// try (JsonParser parser = mapper.getFactory().createParser(request.getInputStream())) {
//
// // ========== 步骤1:流式解析,把所有关联集合存到内存 ==========
// String version = null;
// // 存储所有集合:key=集合名(weaponList/weaponParams),value=数据列表
// Map<String, List<Map<String, Object>>> allCollections = new HashMap<>();
//
// // 解析状态
// String currentCollection = null;
// Map<String, Object> currentItem = null;
//
// JsonToken token;
// while ((token = parser.nextToken()) != null) {
// switch (token) {
// case START_OBJECT:
// if (currentCollection != null) currentItem = new HashMap<>();
// break;
// case END_OBJECT:
// if (currentCollection != null && currentItem != null) {
// allCollections.get(currentCollection).add(currentItem);
// currentItem = null;
// }
// break;
// case START_ARRAY:
// currentCollection = parser.getCurrentName();
// allCollections.putIfAbsent(currentCollection, new ArrayList<>());
// break;
// case END_ARRAY:
// currentCollection = null;
// break;
// case FIELD_NAME:
// String field = parser.getCurrentName();
// // 解析版本号
// if ("version".equals(field) && isInMeta(parser)) {
// version = parser.nextTextValue();
// break;
// }
// // 解析普通字段
// if (currentItem != null) {
// parser.nextToken();
// currentItem.put(field, getValue(parser));
// }
// break;
// }
// }
//
// // ========== 步骤2:核心!通过ID关联匹配,组装完整数据 ==========
// return assembleFullWeaponData(allCollections);
// }
// }
//
// /**
// * 【核心逻辑】ID关联匹配:武器列表 + 参数列表 → 完整武器
// */
// private List<AssembledWeapon> assembleFullWeaponData(Map<String, List<Map<String, Object>>> allCollections) {
// List<AssembledWeapon> result = new ArrayList<>();
//
// // 1. 获取武器主列表
// List<Map<String, Object>> weaponList = allCollections.getOrDefault("weaponList", Collections.emptyList());
// // 2. 获取武器参数列表
// List<Map<String, Object>> paramList = allCollections.getOrDefault("weaponParams", Collections.emptyList());
//
// // 3. 构建【ID -> 参数】索引Map(O(1)快速匹配,效率最高)
// Map<Long, Map<String, Object>> paramIndex = new HashMap<>();
// for (Map<String, Object> param : paramList) {
// Long weaponId = Long.valueOf(param.get("weaponId").toString());
// paramIndex.put(weaponId, param);
// }
//
// // 4. 遍历武器,通过ID匹配参数,组装完整对象
// for (Map<String, Object> weapon : weaponList) {
// AssembledWeapon fullWeapon = new AssembledWeapon();
// Long weaponId = Long.valueOf(weapon.get("id").toString());
//
// // 填充基础信息
// fullWeapon.setId(weaponId);
// fullWeapon.setName((String) weapon.get("name"));
//
// // 匹配参数(通过ID)
// Map<String, Object> param = paramIndex.get(weaponId);
// if (param != null) {
// fullWeapon.setDamage(Integer.valueOf(param.get("damage").toString()));
// fullWeapon.setMagazine(Integer.valueOf(param.get("magazine").toString()));
// fullWeapon.setType((String) param.get("type"));
// }
//
// result.add(fullWeapon);
// }
//
// return result;
// }
//
// // ---------------- 工具方法 ----------------
// private boolean isInMeta(JsonParser parser) {
// return parser.getParsingContext().getParent() != null
// && "meta".equals(parser.getParsingContext().getParent().getCurrentName());
// }
//
// private Object getValue(JsonParser parser) throws IOException {
// if (parser.getCurrentToken().isNumeric()) return parser.getLongValue();
// if (parser.getCurrentToken() == JsonToken.VALUE_STRING) return parser.getText();
// if (parser.getCurrentToken() == JsonToken.VALUE_TRUE) return true;
// if (parser.getCurrentToken() == JsonToken.VALUE_FALSE) return false;
// return null;
// }
//}