1+ package vm
2+
3+ import com.google.gson.*
4+ import vm.element.WidgetNode
5+
6+ /* *
7+ * 安全的Gson配置,用于处理深度嵌套的Widget Tree
8+ */
9+ object SafeGsonConfig {
10+
11+ private const val MAX_DEPTH = 250
12+
13+ val gson: Gson = GsonBuilder ()
14+ .setStrictness(Strictness .LENIENT )
15+ .registerTypeAdapter(WidgetNode ::class .java, SafeWidgetNodeDeserializer ())
16+ .create()
17+
18+ /* *
19+ * 安全的WidgetNode反序列化器,限制嵌套深度
20+ */
21+ class SafeWidgetNodeDeserializer : JsonDeserializer <WidgetNode > {
22+
23+ override fun deserialize (
24+ json : JsonElement ,
25+ typeOfT : java.lang.reflect.Type ,
26+ context : JsonDeserializationContext
27+ ): WidgetNode {
28+ return deserializeWithDepth(json, context, 0 )
29+ }
30+
31+ private fun deserializeWithDepth (
32+ json : JsonElement ,
33+ context : JsonDeserializationContext ,
34+ currentDepth : Int
35+ ): WidgetNode {
36+ val jsonObject = json.asJsonObject
37+
38+ // 如果深度超过限制,返回简化的节点
39+ if (currentDepth >= MAX_DEPTH ) {
40+ return WidgetNode (
41+ description = jsonObject.get(" description" )?.asString + " (深度限制)" ,
42+ shouldIndent = jsonObject.get(" shouldIndent" )?.asBoolean,
43+ widgetRuntimeType = jsonObject.get(" widgetRuntimeType" )?.asString,
44+ valueId = jsonObject.get(" valueId" )?.asString,
45+ createdByLocalProject = jsonObject.get(" createdByLocalProject" )?.asBoolean,
46+ children = null , // 截断子节点
47+ textPreview = jsonObject.get(" textPreview" )?.asString,
48+ properties = null ,
49+ renderObject = null ,
50+ hasChildren = jsonObject.get(" hasChildren" )?.asBoolean,
51+ allowsInspection = jsonObject.get(" allowsInspection" )?.asBoolean,
52+ locationId = jsonObject.get(" locationId" )?.asString,
53+ creationLocation = null ,
54+ isStateful = jsonObject.get(" isStateful" )?.asBoolean
55+ )
56+ }
57+
58+ // 递归处理子节点
59+ val children = jsonObject.get(" children" )?.asJsonArray?.let { childrenArray ->
60+ if (childrenArray.size() > 20 ) {
61+ // 如果子节点太多,只取前20个
62+ childrenArray.take(20 ).map { childElement ->
63+ deserializeWithDepth(childElement, context, currentDepth + 1 )
64+ }
65+ } else {
66+ childrenArray.map { childElement ->
67+ deserializeWithDepth(childElement, context, currentDepth + 1 )
68+ }
69+ }
70+ }
71+
72+ return WidgetNode (
73+ description = jsonObject.get(" description" )?.asString,
74+ shouldIndent = jsonObject.get(" shouldIndent" )?.asBoolean,
75+ widgetRuntimeType = jsonObject.get(" widgetRuntimeType" )?.asString,
76+ valueId = jsonObject.get(" valueId" )?.asString,
77+ createdByLocalProject = jsonObject.get(" createdByLocalProject" )?.asBoolean,
78+ children = children,
79+ textPreview = jsonObject.get(" textPreview" )?.asString,
80+ properties = null , // 暂时不处理properties以减少复杂度
81+ renderObject = null ,
82+ hasChildren = jsonObject.get(" hasChildren" )?.asBoolean,
83+ allowsInspection = jsonObject.get(" allowsInspection" )?.asBoolean,
84+ locationId = jsonObject.get(" locationId" )?.asString,
85+ creationLocation = null , // 暂时不处理creationLocation
86+ isStateful = jsonObject.get(" isStateful" )?.asBoolean
87+ )
88+ }
89+ }
90+ }
0 commit comments