Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/version-3' into version-3
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownJoe796 committed Nov 1, 2024
2 parents b6ba307 + 337ca8f commit 160b79e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import kotlinx.serialization.SerializationException

private class SerializablePropertyParser<T>(val serializer: KSerializer<T>) {
val children = run {
serializer.serializableProperties!!.associateBy {it.name }
(serializer.serializableProperties ?: throw SerializationException("${serializer.descriptor.serialName} does not have any serializable properties")).associateBy {it.name }
}
companion object {
val existing = HashMap<KSerializerKey, SerializablePropertyParser<*>>()
Expand Down Expand Up @@ -60,16 +60,21 @@ class DataClassPathSerializer<T>(val inner: KSerializer<T>): KSerializerWithDefa
fun fromString(value: String): DataClassPathPartial<T> {
var current: DataClassPathPartial<T>? = null
var currentSerializer: KSerializer<*> = inner
for(part in value.split('.')) {
val valueParts = value.split('.')
for((index, part) in valueParts.withIndex()) {
val name = part.removeSuffix("?")
if(name == "this") continue
val prop = try{ SerializablePropertyParser[currentSerializer](name) } catch (e:IllegalStateException) { throw SerializationException(message = e.message, cause = e)}
val prop = try{
SerializablePropertyParser[currentSerializer](name)
} catch (e:IllegalStateException) {
throw SerializationException(message = e.message, cause = e)
}
currentSerializer = prop.serializer
val c = current
@Suppress("UNCHECKED_CAST")
current = if(c == null) DataClassPathAccess(DataClassPathSelf<T>(inner), prop as SerializableProperty<T, Any?>)
else DataClassPathAccess(c as DataClassPath<T, Any?>, prop as SerializableProperty<Any?, Any?>)
if(part.endsWith('?')) {
if(part.endsWith('?') || prop.serializer.descriptor.isNullable && index != valueParts.lastIndex) {
@Suppress("UNCHECKED_CAST")
current = DataClassPathNotNull(current as DataClassPath<T, Any?>)
currentSerializer = currentSerializer.nullElement()!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ class SerializationTest {
val restored = myJson.decodeFromString(serializer, asText)
assertEquals(item, restored)
}
@Test fun dataClassPathForgotQuestionMark() {
println(DataClassPathSerializer(LargeTestModel.serializer()).fromString("embeddedNullable.value1"))
}

@Test fun oldSearchStyle() {
myJson.decodeFromString<Condition<String>>("""
Expand Down

0 comments on commit 160b79e

Please sign in to comment.