Merge pull request #165 from nielsvanvelzen/fix-the-uuid

Fix uuid parser sometimes failing
This commit is contained in:
Bill Thornton 2020-12-30 10:14:15 -05:00 committed by GitHub
commit fc8f6b9c26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 3 deletions

View File

@ -17,8 +17,7 @@ public class UUIDSerializer : KSerializer<UUID> {
override fun deserialize(decoder: Decoder): UUID {
val uuid = decoder.decodeString()
return if (uuid.length == 32) UUID.fromString(uuid.replace(UUID_REGEX, "$1-$2-$3-$4-$5"))
else UUID.fromString(uuid)
return UUID.fromString(uuid.replace(UUID_REGEX, "$1-$2-$3-$4-$5"))
}
override fun serialize(encoder: Encoder, value: UUID) {
@ -26,6 +25,6 @@ public class UUIDSerializer : KSerializer<UUID> {
}
private companion object {
private val UUID_REGEX = "^([a-z\\d]{8})([a-z\\d]{4})(4[a-z\\d]{3})([a-z\\d]{4})([a-z\\d]{12})\$".toRegex()
private val UUID_REGEX = "^([a-z\\d]{8})([a-z\\d]{4})([a-z\\d]{4})([a-z\\d]{4})([a-z\\d]{12})\$".toRegex()
}
}

View File

@ -32,5 +32,13 @@ public class UUIDSerializerTests {
UUID.fromString("713dc3fe-952b-438f-a70e-d35e4ef0525a"),
Json.decodeFromString(instance, "\"713dc3fe952b438fa70ed35e4ef0525a\"")
)
assertEquals(
UUID.fromString("be275f0b-db14-71d8-6d2d-be5b8bb6918e"),
Json.decodeFromString(instance, "\"be275f0bdb1471d86d2dbe5b8bb6918e\"")
)
assertEquals(
UUID.fromString("70a37b76-f996-24a5-7725-cbf8345e2b62"),
Json.decodeFromString(instance, "\"70a37b76f99624a57725cbf8345e2b62\"")
)
}
}