-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Jackson kun som test-avhengighet 🧪
- Loading branch information
1 parent
864ddaf
commit 49279b6
Showing
8 changed files
with
69 additions
and
63 deletions.
There are no files selected for viewing
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
18 changes: 12 additions & 6 deletions
18
azure-token-client/src/main/kotlin/com/github/navikt/tbd_libs/azure/AzureErrorResponse.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 |
---|---|---|
@@ -1,12 +1,18 @@ | ||
package com.github.navikt.tbd_libs.azure | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties | ||
import com.fasterxml.jackson.annotation.JsonProperty | ||
import com.github.navikt.tbd_libs.azure.JsonSerde.Companion.deserializeOrNull | ||
import com.github.navikt.tbd_libs.azure.JsonSerde.Companion.stringOrNull | ||
|
||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
internal data class AzureErrorResponse( | ||
@JsonProperty("error") | ||
val error: String, | ||
@JsonProperty("error_description") | ||
val description: String | ||
) | ||
) { | ||
internal companion object { | ||
internal fun JsonSerde.azureErrorResponseOrNull(body: String): AzureErrorResponse? { | ||
val json = deserializeOrNull(body) ?: return null | ||
val error = json.stringOrNull("error") ?: return null | ||
val errorDescription = json.stringOrNull("error_description") ?: return null | ||
return AzureErrorResponse(error, errorDescription) | ||
} | ||
} | ||
} |
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
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
14 changes: 14 additions & 0 deletions
14
azure-token-client/src/main/kotlin/com/github/navikt/tbd_libs/azure/JsonSerde.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,14 @@ | ||
package com.github.navikt.tbd_libs.azure | ||
|
||
import java.lang.Exception | ||
|
||
interface JsonSerde { | ||
fun deserialize(content: String): Map<String, Any?> | ||
fun serialize(content: Map<String, Any?>): String | ||
|
||
companion object { | ||
internal fun Map<String, Any?>.stringOrNull(key: String): String? = get(key)?.takeIf { it is String }?.let { it as String } | ||
internal fun Map<String, Any?>.longOrNull(key: String): Long? = get(key)?.takeIf { it is Number }?.let { it as Number }?.toLong() | ||
internal fun JsonSerde.deserializeOrNull(content: String) = try { deserialize(content) } catch (_: Exception) { null } | ||
} | ||
} |
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
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
14 changes: 14 additions & 0 deletions
14
azure-token-client/src/test/kotlin/com/github/navikt/tbd_libs/azure/Jackson.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,14 @@ | ||
package com.github.navikt.tbd_libs.azure | ||
|
||
import com.fasterxml.jackson.databind.SerializationFeature | ||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule | ||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import com.fasterxml.jackson.module.kotlin.readValue | ||
|
||
internal object Jackson: JsonSerde { | ||
private val objectMapper = jacksonObjectMapper() | ||
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) | ||
.registerModule(JavaTimeModule()) | ||
override fun deserialize(content: String): Map<String, Any?> = objectMapper.readValue(content) | ||
override fun serialize(content: Map<String, Any?>): String = objectMapper.writeValueAsString(content) | ||
} |