Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
M
Mirai
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
Mirai
Commits
94502d44
Commit
94502d44
authored
Jan 28, 2020
by
Him188
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
JceStruct serialization: optional elements support
parent
2e80d330
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
34 additions
and
16 deletions
+34
-16
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/io/serialization/Jce.kt
.../kotlin/net/mamoe/mirai/qqandroid/io/serialization/Jce.kt
+33
-15
mirai-core-qqandroid/src/jvmTest/kotlin/net.mamoe.mirai.qqandroid.io.serialization/JceDecoderTest.kt
....mamoe.mirai.qqandroid.io.serialization/JceDecoderTest.kt
+1
-1
No files found.
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/io/serialization/Jce.kt
View file @
94502d44
...
@@ -330,6 +330,9 @@ class Jce private constructor(private val charset: JceCharset, context: SerialMo
...
@@ -330,6 +330,9 @@ class Jce private constructor(private val charset: JceCharset, context: SerialMo
override
fun
decodeTaggedString
(
tag
:
Int
):
String
=
input
.
readString
(
tag
)
override
fun
decodeTaggedString
(
tag
:
Int
):
String
=
input
.
readString
(
tag
)
override
fun
decodeTaggedBoolean
(
tag
:
Int
):
Boolean
=
input
.
readBoolean
(
tag
)
override
fun
decodeTaggedBoolean
(
tag
:
Int
):
Boolean
=
input
.
readBoolean
(
tag
)
override
fun
decodeTaggedEnum
(
tag
:
Int
,
enumDescription
:
SerialDescriptor
):
Int
=
TODO
()
/**
/**
* 在 [KSerializer.serialize] 前
* 在 [KSerializer.serialize] 前
*/
*/
...
@@ -365,16 +368,26 @@ class Jce private constructor(private val charset: JceCharset, context: SerialMo
...
@@ -365,16 +368,26 @@ class Jce private constructor(private val charset: JceCharset, context: SerialMo
}
}
is
MapLikeDescriptor
->
{
is
MapLikeDescriptor
->
{
val
tag
=
currentTagOrNull
val
tag
=
currentTagOrNull
if
(
tag
!=
null
&&
input
.
skipToTagOrNull
(
tag
)
{
popTag
()
}
==
null
&&
desc
.
isNullable
)
{
if
(
tag
!=
null
&&
input
.
skipToTagOrNull
(
tag
)
{
popTag
()
}
==
null
&&
desc
.
isNullable
)
{
return
NullReader
(
this
.
input
)
return
NullReader
(
this
.
input
)
}
}
if
(
tag
!=
null
)
{
popTag
()
}
return
JceMapReader
(
input
.
readInt
(
0
),
this
.
input
)
return
JceMapReader
(
input
.
readInt
(
0
),
this
.
input
)
}
}
}
}
if
(!
input
.
input
.
endOfInput
)
{
val
tag
=
currentTagOrNull
if
(
tag
!=
null
&&
input
.
peakHead
().
tag
>
tag
)
{
return
NullReader
(
this
.
input
)
}
}
if
(
desc
.
kind
==
StructureKind
.
CLASS
||
desc
.
kind
==
UnionKind
.
OBJECT
)
{
if
(
desc
.
kind
==
StructureKind
.
CLASS
||
desc
.
kind
==
UnionKind
.
OBJECT
)
{
val
tag
=
currentTagOrNull
val
tag
=
currentTagOrNull
if
(
tag
!=
null
)
{
if
(
tag
!=
null
)
{
...
@@ -390,18 +403,20 @@ class Jce private constructor(private val charset: JceCharset, context: SerialMo
...
@@ -390,18 +403,20 @@ class Jce private constructor(private val charset: JceCharset, context: SerialMo
return
this
// top-level
return
this
// top-level
}
}
if
(!
input
.
input
.
endOfInput
)
{
val
tag
=
currentTagOrNull
if
(
tag
!=
null
&&
input
.
peakHead
().
tag
>
tag
)
{
return
NullReader
(
this
.
input
)
}
}
return
super
.
beginStructure
(
desc
,
*
typeParams
)
return
super
.
beginStructure
(
desc
,
*
typeParams
)
}
}
override
fun
decodeTaggedNull
(
tag
:
Int
):
Nothing
?
{
return
null
}
override
fun
decodeTaggedNotNullMark
(
tag
:
Int
):
Boolean
{
return
!
input
.
input
.
endOfInput
&&
input
.
peakHead
().
tag
<=
tag
}
@Suppress
(
"UNCHECKED_CAST"
)
@Suppress
(
"UNCHECKED_CAST"
)
override
fun
<
T
:
Any
>
decodeNullableSerializableValue
(
deserializer
:
DeserializationStrategy
<
T
?
>):
T
?
{
override
fun
<
T
:
Any
>
decodeNullableSerializableValue
(
deserializer
:
DeserializationStrategy
<
T
?
>):
T
?
{
println
(
"decodeNullableSerializableValue: ${deserializer.getClassName()}"
)
if
(
deserializer
is
NullReader
)
{
if
(
deserializer
is
NullReader
)
{
return
null
return
null
}
}
...
@@ -421,6 +436,7 @@ class Jce private constructor(private val charset: JceCharset, context: SerialMo
...
@@ -421,6 +436,7 @@ class Jce private constructor(private val charset: JceCharset, context: SerialMo
)
{
)
{
return
input
.
readByteArray
(
popTag
()).
toMutableList
()
as
T
return
input
.
readByteArray
(
popTag
()).
toMutableList
()
as
T
}
}
return
super
.
decodeSerializableValue
(
deserializer
)
}
}
is
MapLikeDescriptor
->
{
is
MapLikeDescriptor
->
{
// 将 mapOf(k1 to v1, k2 to v2, ...) 转换为 listOf(k1, v1, k2, v2, ...) 以便于写入.
// 将 mapOf(k1 to v1, k2 to v2, ...) 转换为 listOf(k1, v1, k2, v2, ...) 以便于写入.
...
@@ -430,16 +446,18 @@ class Jce private constructor(private val charset: JceCharset, context: SerialMo
...
@@ -430,16 +446,18 @@ class Jce private constructor(private val charset: JceCharset, context: SerialMo
return
setOfEntries
.
associateBy
({
it
.
key
},
{
it
.
value
})
as
T
return
setOfEntries
.
associateBy
({
it
.
key
},
{
it
.
value
})
as
T
}
}
}
}
return
super
.
decodeSerializableValue
(
deserializer
)
val
tag
=
currentTagOrNull
?:
return
deserializer
.
deserialize
(
this
)
return
if
(
this
.
decodeTaggedNotNullMark
(
tag
)){
deserializer
.
deserialize
(
this
)
}
else
{
null
}
}
}
@Suppress
(
"UNCHECKED_CAST"
)
@Suppress
(
"UNCHECKED_CAST"
)
override
fun
<
T
>
decodeSerializableValue
(
deserializer
:
DeserializationStrategy
<
T
>):
T
{
override
fun
<
T
>
decodeSerializableValue
(
deserializer
:
DeserializationStrategy
<
T
>):
T
{
return
decodeNullableSerializableValue
(
deserializer
as
DeserializationStrategy
<
Any
?
>)
as
?
T
?:
error
(
"value is not optional but cannot find"
)
return
decodeNullableSerializableValue
(
deserializer
as
DeserializationStrategy
<
Any
?
>)
as
?
T
?:
error
(
"value is not optional but cannot find"
)
}
}
override
fun
decodeTaggedEnum
(
tag
:
Int
,
enumDescription
:
SerialDescriptor
):
Int
=
TODO
()
}
}
...
...
mirai-core-qqandroid/src/jvmTest/kotlin/net.mamoe.mirai.qqandroid.io.serialization/JceDecoderTest.kt
View file @
94502d44
...
@@ -72,7 +72,7 @@ class JceDecoderTest {
...
@@ -72,7 +72,7 @@ class JceDecoderTest {
writeJceStruct
(
TestSimpleJceStruct
(),
10
)
writeJceStruct
(
TestSimpleJceStruct
(),
10
)
writeCollection
(
listOf
(
listOf
(
1
,
2
,
3
),
listOf
(
1
,
2
,
3
)),
11
)
writeCollection
(
listOf
(
listOf
(
1
,
2
,
3
),
listOf
(
1
,
2
,
3
)),
11
)
}.
readBytes
().
toUHexString
(),
}.
readBytes
().
toUHexString
(),
TestComplex
JceStruct
().
toByteArray
(
TestComplex
JceStruct
.
serializer
()).
toUHexString
()
TestComplex
NullableJceStruct
().
toByteArray
(
TestComplexNullable
JceStruct
.
serializer
()).
toUHexString
()
)
)
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment