-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
...m/infomaniak/multiplatform_swisstransfer/network/serializers/DateToTimestampSerializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Infomaniak SwissTransfer - Multiplatform | ||
* Copyright (C) 2024 Infomaniak Network SA | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.infomaniak.multiplatform_swisstransfer.network.serializers | ||
|
||
import kotlinx.datetime.LocalDateTime | ||
import kotlinx.datetime.TimeZone | ||
import kotlinx.datetime.format.FormatStringsInDatetimeFormats | ||
import kotlinx.datetime.format.byUnicodePattern | ||
import kotlinx.datetime.toInstant | ||
import kotlinx.serialization.builtins.serializer | ||
import kotlinx.serialization.json.JsonElement | ||
import kotlinx.serialization.json.JsonPrimitive | ||
import kotlinx.serialization.json.JsonTransformingSerializer | ||
import kotlinx.serialization.json.jsonPrimitive | ||
|
||
object DateToTimestampSerializer : JsonTransformingSerializer<Long>(Long.serializer()) { | ||
@OptIn(FormatStringsInDatetimeFormats::class) | ||
override fun transformDeserialize(element: JsonElement): JsonElement { | ||
val dateString = element.jsonPrimitive.content | ||
|
||
val formatter = LocalDateTime.Format { | ||
byUnicodePattern("uuuu-MM-dd HH:mm:ss") | ||
} | ||
|
||
val localDateTime = LocalDateTime.parse(dateString, formatter) | ||
val timestamp = localDateTime.toInstant(TimeZone.UTC).toEpochMilliseconds() | ||
|
||
return runCatching { JsonPrimitive(timestamp) }.getOrDefault(element) | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
.../com/infomaniak/multiplatform_swisstransfer/network/serializers/IntToBooleanSerializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Infomaniak SwissTransfer - Multiplatform | ||
* Copyright (C) 2024 Infomaniak Network SA | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.infomaniak.multiplatform_swisstransfer.network.serializers | ||
|
||
import kotlinx.serialization.builtins.serializer | ||
import kotlinx.serialization.json.JsonElement | ||
import kotlinx.serialization.json.JsonPrimitive | ||
import kotlinx.serialization.json.JsonTransformingSerializer | ||
import kotlinx.serialization.json.int | ||
import kotlinx.serialization.json.jsonPrimitive | ||
|
||
object IntToBooleanSerializer : JsonTransformingSerializer<Boolean>(Boolean.serializer()) { | ||
override fun transformDeserialize(element: JsonElement): JsonElement { | ||
return runCatching { JsonPrimitive(element.jsonPrimitive.int > 0) }.getOrDefault(element) | ||
} | ||
} |