code to json extension function

This commit is contained in:
Adam Velebil 2022-03-16 09:28:34 +01:00
parent 747d328303
commit cd4461d449
No known key found for this signature in database
GPG Key ID: AC6D6B9D715FC084
2 changed files with 12 additions and 16 deletions

View File

@ -358,9 +358,9 @@ class MainViewModel : ViewModel() {
val credential = getOathCredential(session, credentialId)
val resultJson = SerializeHelpers.serialize(
session.calculateCode(credential)
).toString()
val resultJson = session.calculateCode(credential)
.toJson()
.toString()
result.success(resultJson)
}

View File

@ -74,17 +74,13 @@ class SerializeHelpers {
)
fun serialize(code: Code?) =
when (code) {
null -> JsonNull
else -> JsonObject(
mapOf(
"value" to JsonPrimitive(code.value),
"valid_from" to JsonPrimitive(code.validFrom / 1000),
"valid_to" to JsonPrimitive(code.validUntil / 1000)
)
)
}
fun Code.toJson() = JsonObject(
mapOf(
"value" to JsonPrimitive(value),
"valid_from" to JsonPrimitive(validFrom / 1000),
"valid_to" to JsonPrimitive(validUntil / 1000)
)
)
fun credentialIdAsString(id: ByteArray): String = id.joinToString(
separator = ""
@ -105,7 +101,7 @@ class SerializeHelpers {
)
)
fun Map<Credential, Code>.toJson(deviceId: String) =
fun Map<Credential, Code?>.toJson(deviceId: String) =
JsonObject(
mapOf(
"entries" to JsonArray(
@ -113,7 +109,7 @@ class SerializeHelpers {
JsonObject(
mapOf(
"credential" to credential.toJson(deviceId),
"code" to serialize(code)
"code" to (code?.toJson() ?: JsonNull)
)
)
}