Skip to content

Instantly share code, notes, and snippets.

View imGurpreetSK's full-sized avatar
😃
Learning

Gurpreet Singh imGurpreetSK

😃
Learning
View GitHub Profile
@imGurpreetSK
imGurpreetSK / gist:1140a24e9de2333b70f6a595397bdc97
Last active November 7, 2024 17:02
Default karabiner-elements config
{
"profiles": [
{
"complex_modifications": {
"parameters": { "basic.to_if_alone_timeout_milliseconds": 700 },
"rules": [
{
"description": "Auto close brackets & quotes ( (), '', \"\", ``, [], {}, “”, ‘’, <> )",
"enabled": false,
"manipulators": [
@imGurpreetSK
imGurpreetSK / gist:2f036a5197d3486c5aecb978efec4157
Last active October 21, 2024 18:24
Advantage2 keyboard config
[escape]>[caps]
[caps]>[null]
[lctrl]>[lwin]
[rwin]>[lctrl]
[rctrl]>[lwin]
[lshift]>[null]
[home]>[home][t&h50][lshift]
[pup]>[pup][t&h50][lshift]
[rshift]>[null]
[end]>[end][t&h50][escape]
@imGurpreetSK
imGurpreetSK / UseArrayPolymorphismExample.kt
Created May 3, 2020 06:22
Example for useArrayPolymorphism usage
@Test
fun `play around with useArrayPolymorphism`() {
@Serializable
abstract class Animal
@Serializable
@SerialName("Dog") // Overrides name to 'Dog' instead of fully qualified class name for runtime
data class Dog(val lovesTreats: Boolean) : Animal()
@Serializable
@imGurpreetSK
imGurpreetSK / allowStructuredMapKeysExample.kt
Created May 3, 2020 05:35
Example for allowStructuredMapKeys property in JsonConfiguration
@Test
fun `allowStructuredMapKeys allows json with non-primitive keys to be parsed successfully`() {
@Serializable
data class Contacts(
val contacts: Map<Int, Map<Map<Int, Int>, String>>
)
val contacts = Contacts(
linkedMapOf(
1 to mapOf(mapOf(1 to 1) to "Gurpreet"),
2 to linkedMapOf(mapOf(2 to 2) to "value")
@imGurpreetSK
imGurpreetSK / PrettyPrintExample2.kt
Created May 2, 2020 19:27
Example for prettyPrint property in JsonConfiguration
@Test
fun `pretty printing JSON with custom indent is also an option`() {
val user = User(1, "Gurpreet Singh")
val json = Json(JsonConfiguration(prettyPrint = true, indent = " "))
.stringify(User.serializer(), user)
assertEquals(
"""{
| "id": 1,
@imGurpreetSK
imGurpreetSK / PrettyPrintExample.kt
Created May 2, 2020 19:26
Example for prettyPrint property in JsonConfiguration
@Test
fun `pretty printing JSON is an option`() {
val user = User(1, "Gurpreet Singh")
val json = Json(JsonConfiguration(prettyPrint = true))
.stringify(User.serializer(), user)
assertEquals(
"""{
| "id": 1,
@imGurpreetSK
imGurpreetSK / UnquotedPrintExample2.kt
Created May 2, 2020 19:24
Example for unquotedPrint property in JsonConfiguration
@Test
fun `'unquotedPrint' isn't effective if strings have spaces`() {
val user = User(1, "Gurpreet Singh")
val json = Json(JsonConfiguration(unquotedPrint = true))
.stringify(User.serializer(), user)
assertEquals("""{id:1,name:"Gurpreet Singh"}""", json)
}
@imGurpreetSK
imGurpreetSK / UnquotedPrintExample.kt
Created May 2, 2020 19:21
Example for unquotedPrint property in JsonConfiguration
@Test
fun `JSON keys and values are unquoted if 'unquotedPrint' flag is true`() {
val user = User(1)
val json = Json(JsonConfiguration(unquotedPrint = true))
.stringify(User.serializer(), user)
assertEquals("""{id:1,name:N/A}""", json)
}
@imGurpreetSK
imGurpreetSK / serializeSpecialFloatingPointValuesExample.kt
Created May 2, 2020 19:19
Example for serializeSpecialFloatingPointValues property in JsonConfiguration
@Test
fun `allow serializing special floating point values with 'serializeSpecialFloatingPointValues'`() {
@Serializable
data class User(
val id: Float,
val name: String = "N/A"
)
val userJsonWithInfinityId = """{"id":Infinity,"name":"Gurpreet"}"""
val userJsonWithNaNId = """{"id":NaN,"name":"Gurpreet"}"""
@imGurpreetSK
imGurpreetSK / IsLenientExample.kt
Last active May 2, 2020 19:15
Example for isLenient property in JsonConfiguration
@Test
fun `serialization passes for malformed JSON if 'isLenient' flag is true`() {
val userJson = """ {id:1,name:Gurpreet} """
val parsedObject = Json(JsonConfiguration(isLenient = true))
.parse(User.serializer(), userJson)
assertEquals(User(1, "Gurpreet"), parsedObject)
}