Skip to content

Commit

Permalink
Experimental | Fix Item Serialize
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFloodDragon committed May 19, 2024
1 parent 8c1d730 commit 09f3554
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.descriptors.elementNames
import kotlinx.serialization.json.JsonNames

fun SerialDescriptor.getElementDescriptor(name:String) = this.getElementDescriptor(this.getElementIndex(name))

/**
* 获取序列化器中使用的节点
* 即所有的元素名称和 [JsonNames] 注解中的所有名称
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ val baseJson by lazy {
}
}

operator fun JsonObject.get(names: Iterable<String>): JsonElement? {
for (name in names) {
val find = this[name]
if (find != null) return find
}
return null
}

/**
* 可变的 [JsonObject]
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@file:OptIn(ExperimentalSerializationApi::class)

package cn.fd.ratziel.module.item.impl.builder

import cn.fd.ratziel.core.serialization.get
import cn.fd.ratziel.core.serialization.getElementDescriptor
import cn.fd.ratziel.core.serialization.handle
import cn.fd.ratziel.core.serialization.usedNodes
import cn.fd.ratziel.module.item.api.ItemMaterial
Expand All @@ -14,7 +14,6 @@ import cn.fd.ratziel.module.item.impl.part.serializers.*
import cn.fd.ratziel.module.item.nbt.NBTData
import cn.fd.ratziel.module.item.nbt.NBTSerializer
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.*
import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.modules.plus
Expand Down Expand Up @@ -49,23 +48,20 @@ class DefaultItemSerializer(rawJson: Json) : ItemKSerializer<VItemMeta> {
* 反序列化 (检查结构化解析)
*/
override fun deserialize(element: JsonElement): VItemMeta {
// 结构化解析
if (isStructured(element)) return json.decodeFromJsonElement(VItemMeta.serializer(), element)
// 异步方法
fun <T> asyncDecode(deserializer: DeserializationStrategy<T>) =
fun <T> asyncDecode(deserializer: DeserializationStrategy<T>, from: JsonElement = element) =
CompletableFuture.supplyAsync {
json.decodeFromJsonElement(deserializer, element)
json.decodeFromJsonElement(deserializer, from)
}.exceptionally { it.printStackTrace();null }
// 结构化解析
val meta = asyncDecode(VItemMeta.serializer())
if (isStructured(element)) return meta.get()
// 非结构化解析
// 一般解析
val display = asyncDecode(VItemDisplay.serializer())
val durability = asyncDecode(VItemDurability.serializer())
val sundry = asyncDecode(VItemSundry.serializer())
return meta.get().apply {
this.display = display.get()
this.durability = durability.get()
this.sundry = sundry.get()
}
val material = (element as? JsonObject)?.get(NODES_MATERIAL)
?.let { asyncDecode(ItemMaterialSerializer, it) } ?: CompletableFuture.completedFuture(ItemMaterial.EMPTY)
return VItemMeta(material.get(), display.get(), durability.get(), sundry.get())
}

/**
Expand All @@ -84,6 +80,8 @@ class DefaultItemSerializer(rawJson: Json) : ItemKSerializer<VItemMeta> {
VItemSundry.serializer(),
)

val NODES_MATERIAL = VItemMeta.serializer().descriptor.getElementDescriptor(VItemMeta::material.name).usedNodes

/**
* 占据的节点
*/
Expand Down
64 changes: 2 additions & 62 deletions wiki/docs/yaml/basic.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ sidebar_position: 2
```
```JSON
"values": [[ "value1", "value2"], ["value3", "value4"] ]
"values":[[ "value1", "value2"], ["value3", "value4"]]
```

## 字符串(String)
Expand Down Expand Up @@ -180,7 +180,7 @@ float:

## 空(Null)

> “null”、“Null”和“~”都是空,不指定值默认也是空
> “null”、“Null”和“~”都是**空**,不指定值默认也是**空**

```YAML
# YAML
Expand All @@ -191,14 +191,9 @@ nulls:
-
```

```JSON
"nulls": [null, null, null, null]
```
## 时间戳(Timestamp)

> YAML 也支持 ISO 8601 格式的时间数据
> 这里使用 JavaScript 对象进行对比

```YAML
# YAML
Expand All @@ -208,61 +203,6 @@ date3: 2020-05-26T02:00:00.10+08:00
date4: 2020-05-26 03:00:00.10 +8
```

```JS
// JavaScript
date1: Tue
May
26
2020
0
8
:
00
:
00
GMT + 0
800(中国标准时间),
date2
:
Tue
May
26
2020
01
:
00
:
00
GMT + 0
800(中国标准时间),
dete3
:
Tue
May
26
2020
02
:
00
:
00
GMT + 0
800(中国标准时间),
date4
:
Tue
May
26
2020
03
:
00
:
00
GMT + 0
800(中国标准时间)
```

## 标量(Scalars)

> 表示 YAML 中最基本的数据类型
Expand Down

0 comments on commit 09f3554

Please sign in to comment.