@@ -27,6 +27,7 @@ object ShardLayoutSerializer : KSerializer<ShardLayout> {
2727 element(" V0" , serializer<JsonElement >().descriptor)
2828 element(" V1" , serializer<JsonElement >().descriptor)
2929 element(" V2" , serializer<JsonElement >().descriptor)
30+ element(" V3" , serializer<JsonElement >().descriptor)
3031 }
3132
3233 override fun serialize (encoder : Encoder , value : ShardLayout ) {
@@ -51,6 +52,12 @@ object ShardLayoutSerializer : KSerializer<ShardLayout> {
5152 val payload = JsonObject (map)
5253 jsonEncoder.encodeJsonElement(payload)
5354 }
55+ is io.github.hosseinkarami_dev.near.rpc.models.ShardLayout .V3 -> {
56+ val map = mutableMapOf<String , JsonElement >()
57+ map[" V3" ] = jsonEncoder.json.encodeToJsonElement(serializer< io.github.hosseinkarami_dev.near.rpc.models.ShardLayoutV3 > (), value.v3)
58+ val payload = JsonObject (map)
59+ jsonEncoder.encodeJsonElement(payload)
60+ }
5461 }
5562 return
5663 }
@@ -59,6 +66,7 @@ object ShardLayoutSerializer : KSerializer<ShardLayout> {
5966 is io.github.hosseinkarami_dev.near.rpc.models.ShardLayout .V0 -> out .encodeSerializableElement(descriptor, 0 , serializer< io.github.hosseinkarami_dev.near.rpc.models.ShardLayout .V0 > (), value)
6067 is io.github.hosseinkarami_dev.near.rpc.models.ShardLayout .V1 -> out .encodeSerializableElement(descriptor, 1 , serializer< io.github.hosseinkarami_dev.near.rpc.models.ShardLayout .V1 > (), value)
6168 is io.github.hosseinkarami_dev.near.rpc.models.ShardLayout .V2 -> out .encodeSerializableElement(descriptor, 2 , serializer< io.github.hosseinkarami_dev.near.rpc.models.ShardLayout .V2 > (), value)
69+ is io.github.hosseinkarami_dev.near.rpc.models.ShardLayout .V3 -> out .encodeSerializableElement(descriptor, 3 , serializer< io.github.hosseinkarami_dev.near.rpc.models.ShardLayout .V3 > (), value)
6270 }
6371 out .endStructure(descriptor)
6472 }
@@ -78,7 +86,7 @@ object ShardLayoutSerializer : KSerializer<ShardLayout> {
7886
7987 is JsonObject -> {
8088 val jobj = element
81- val knownVariantNames = setOf (" V0" , " V1" , " V2" )
89+ val knownVariantNames = setOf (" V0" , " V1" , " V2" , " V3 " )
8290 if (jobj[" V0" ] != null ) {
8391 return io.github.hosseinkarami_dev.near.rpc.models.ShardLayout .V0 (decoder.json.decodeFromJsonElement(serializer< io.github.hosseinkarami_dev.near.rpc.models.ShardLayoutV0 > (), jobj[" V0" ]!! ))
8492 }
@@ -88,6 +96,9 @@ object ShardLayoutSerializer : KSerializer<ShardLayout> {
8896 if (jobj[" V2" ] != null ) {
8997 return io.github.hosseinkarami_dev.near.rpc.models.ShardLayout .V2 (decoder.json.decodeFromJsonElement(serializer< io.github.hosseinkarami_dev.near.rpc.models.ShardLayoutV2 > (), jobj[" V2" ]!! ))
9098 }
99+ if (jobj[" V3" ] != null ) {
100+ return io.github.hosseinkarami_dev.near.rpc.models.ShardLayout .V3 (decoder.json.decodeFromJsonElement(serializer< io.github.hosseinkarami_dev.near.rpc.models.ShardLayoutV3 > (), jobj[" V3" ]!! ))
101+ }
91102 if (jobj.size == 1 ) {
92103 val entry = jobj.entries.first()
93104 val key = entry.key
@@ -106,6 +117,10 @@ object ShardLayoutSerializer : KSerializer<ShardLayout> {
106117 val obj = valueElem as ? JsonObject ? : throw SerializationException (" Expected object payload for variant V2: " + key)
107118 return decoder.json.decodeFromJsonElement(serializer< io.github.hosseinkarami_dev.near.rpc.models.ShardLayout .V2 > (), obj)
108119 }
120+ " V3" -> {
121+ val obj = valueElem as ? JsonObject ? : throw SerializationException (" Expected object payload for variant V3: " + key)
122+ return decoder.json.decodeFromJsonElement(serializer< io.github.hosseinkarami_dev.near.rpc.models.ShardLayout .V3 > (), obj)
123+ }
109124 else -> { /* knownVariantNames.contains(key) guards this branch; shouldn't reach here */ }
110125 }
111126 }
@@ -128,6 +143,7 @@ object ShardLayoutSerializer : KSerializer<ShardLayout> {
128143 " V0" -> return decoder.json.decodeFromJsonElement(serializer< io.github.hosseinkarami_dev.near.rpc.models.ShardLayout .V0 > (), jobj)
129144 " V1" -> return decoder.json.decodeFromJsonElement(serializer< io.github.hosseinkarami_dev.near.rpc.models.ShardLayout .V1 > (), jobj)
130145 " V2" -> return decoder.json.decodeFromJsonElement(serializer< io.github.hosseinkarami_dev.near.rpc.models.ShardLayout .V2 > (), jobj)
146+ " V3" -> return decoder.json.decodeFromJsonElement(serializer< io.github.hosseinkarami_dev.near.rpc.models.ShardLayout .V3 > (), jobj)
131147 else -> { /* fallthrough to grouped handling */ }
132148 }
133149 // grouped handling by tf content (if any)
@@ -136,6 +152,7 @@ object ShardLayoutSerializer : KSerializer<ShardLayout> {
136152 if (chosenGroupKey == null && (" V0" .lowercase() == tfLower || tfLower.contains(" V0" .lowercase()) || " V0" .lowercase().contains(tfLower))) { chosenGroupKey = " V0" }
137153 if (chosenGroupKey == null && (" V1" .lowercase() == tfLower || tfLower.contains(" V1" .lowercase()) || " V1" .lowercase().contains(tfLower))) { chosenGroupKey = " V1" }
138154 if (chosenGroupKey == null && (" V2" .lowercase() == tfLower || tfLower.contains(" V2" .lowercase()) || " V2" .lowercase().contains(tfLower))) { chosenGroupKey = " V2" }
155+ if (chosenGroupKey == null && (" V3" .lowercase() == tfLower || tfLower.contains(" V3" .lowercase()) || " V3" .lowercase().contains(tfLower))) { chosenGroupKey = " V3" }
139156 if (chosenGroupKey != null ) {
140157 when (chosenGroupKey) {
141158 " V0" -> {
@@ -150,6 +167,10 @@ object ShardLayoutSerializer : KSerializer<ShardLayout> {
150167 try { return decoder.json.decodeFromJsonElement(serializer< io.github.hosseinkarami_dev.near.rpc.models.ShardLayout .V2 > (), jobj) } catch (_: Exception ) { }
151168 throw SerializationException (" Cannot disambiguate variant for base token 'V2' and tf='\$ tf'" )
152169 }
170+ " V3" -> {
171+ try { return decoder.json.decodeFromJsonElement(serializer< io.github.hosseinkarami_dev.near.rpc.models.ShardLayout .V3 > (), jobj) } catch (_: Exception ) { }
172+ throw SerializationException (" Cannot disambiguate variant for base token 'V3' and tf='\$ tf'" )
173+ }
153174 else -> { /* no group matched */ }
154175 }
155176 }
@@ -158,16 +179,19 @@ object ShardLayoutSerializer : KSerializer<ShardLayout> {
158179 // group: V0
159180 // group: V1
160181 // group: V2
182+ // group: V3
161183
162184 val requiredMatches = mutableListOf<Int >()
163185 if (jobj.containsKey(" V0" )) requiredMatches.add(0 )
164186 if (jobj.containsKey(" V1" )) requiredMatches.add(1 )
165187 if (jobj.containsKey(" V2" )) requiredMatches.add(2 )
188+ if (jobj.containsKey(" V3" )) requiredMatches.add(3 )
166189 if (requiredMatches.size == 1 ) {
167190 when (requiredMatches[0 ]) {
168191 0 -> return decoder.json.decodeFromJsonElement(serializer< io.github.hosseinkarami_dev.near.rpc.models.ShardLayout .V0 > (), jobj)
169192 1 -> return decoder.json.decodeFromJsonElement(serializer< io.github.hosseinkarami_dev.near.rpc.models.ShardLayout .V1 > (), jobj)
170193 2 -> return decoder.json.decodeFromJsonElement(serializer< io.github.hosseinkarami_dev.near.rpc.models.ShardLayout .V2 > (), jobj)
194+ 3 -> return decoder.json.decodeFromJsonElement(serializer< io.github.hosseinkarami_dev.near.rpc.models.ShardLayout .V3 > (), jobj)
171195 else -> throw SerializationException (" Internal required-match dispatch error" )
172196 }
173197 }
@@ -191,17 +215,25 @@ object ShardLayoutSerializer : KSerializer<ShardLayout> {
191215 val score = matchCount.toDouble() / 1 .toDouble()
192216 if (score > bestScore) { bestScore = score; bestIdx = 2 } else if (score == bestScore) { bestIdx = null }
193217 }
218+ run {
219+ var matchCount = 0
220+ if (jobj[" V3" ] != null ) matchCount++
221+ val score = matchCount.toDouble() / 1 .toDouble()
222+ if (score > bestScore) { bestScore = score; bestIdx = 3 } else if (score == bestScore) { bestIdx = null }
223+ }
194224 if (bestIdx != null && bestScore > 0.0 ) {
195225 when (bestIdx) {
196226 0 -> return decoder.json.decodeFromJsonElement(serializer< io.github.hosseinkarami_dev.near.rpc.models.ShardLayout .V0 > (), jobj)
197227 1 -> return decoder.json.decodeFromJsonElement(serializer< io.github.hosseinkarami_dev.near.rpc.models.ShardLayout .V1 > (), jobj)
198228 2 -> return decoder.json.decodeFromJsonElement(serializer< io.github.hosseinkarami_dev.near.rpc.models.ShardLayout .V2 > (), jobj)
229+ 3 -> return decoder.json.decodeFromJsonElement(serializer< io.github.hosseinkarami_dev.near.rpc.models.ShardLayout .V3 > (), jobj)
199230 else -> throw SerializationException (" Internal scoring dispatch error" )
200231 }
201232 }
202233 try { return decoder.json.decodeFromJsonElement(serializer< io.github.hosseinkarami_dev.near.rpc.models.ShardLayout .V0 > (), jobj) } catch (_: Exception ) { }
203234 try { return decoder.json.decodeFromJsonElement(serializer< io.github.hosseinkarami_dev.near.rpc.models.ShardLayout .V1 > (), jobj) } catch (_: Exception ) { }
204235 try { return decoder.json.decodeFromJsonElement(serializer< io.github.hosseinkarami_dev.near.rpc.models.ShardLayout .V2 > (), jobj) } catch (_: Exception ) { }
236+ try { return decoder.json.decodeFromJsonElement(serializer< io.github.hosseinkarami_dev.near.rpc.models.ShardLayout .V3 > (), jobj) } catch (_: Exception ) { }
205237 throw SerializationException (" Missing discriminator or recognizable variant in ShardLayout" )
206238 }
207239 }
0 commit comments