-
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.
Merge pull request #12 from Q42/bugfix/float-serialization-inconsistency
Bugfix/float serialization inconsistency
- Loading branch information
Showing
9 changed files
with
176 additions
and
78 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
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
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
12 changes: 8 additions & 4 deletions
12
q42stats/src/main/java/com/q42/q42stats/library/util/ApiFormatUtil.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
24 changes: 24 additions & 0 deletions
24
q42stats/src/main/java/com/q42/q42stats/library/util/SerializationUtil.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,24 @@ | ||
package com.q42.q42stats.library.util | ||
|
||
import org.json.JSONArray | ||
import org.json.JSONObject | ||
|
||
internal fun serializeMeasurement(value: Map<String, Any?>) = | ||
JSONObject(value).toString() | ||
|
||
internal fun deserializeMeasurement(it: String): Map<String, Any?> = | ||
JSONObject(it).toMap() | ||
|
||
private fun JSONObject.toMap(): Map<String, Any?> = keys().asSequence().associateWith { | ||
when (val value = this[it]) { | ||
is JSONArray -> { | ||
val map = (0 until value.length()).associate { Pair(it.toString(), value[it]) } | ||
JSONObject(map).toMap().values.toList() | ||
} | ||
is JSONObject -> value.toMap() | ||
JSONObject.NULL -> null | ||
else -> { | ||
value | ||
} | ||
} | ||
} |
22 changes: 17 additions & 5 deletions
22
q42stats/src/test/java/com/q42/q42stats/library/ApiFormatUtilTest.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,22 +1,34 @@ | ||
package com.q42.q42stats.library | ||
|
||
import org.junit.Assert.assertEquals | ||
import org.junit.Test | ||
|
||
class ApiFormatUtilTest { | ||
@Test | ||
fun testToQ42StatsApiFormat() { | ||
val expected = | ||
"""{"screen width":"360","textScale":"1.6","language":"nl","currentMeasurement":{"createdAt":"1337"},"talkbackEnabled":"true"}""" | ||
val actual = mapOf( | ||
"screen width" to 360, | ||
"fontSize" to 1.0, // expected is "1.0". should not be truncated to "1" | ||
"textScale" to 1.6, | ||
"talkbackEnabled" to true, | ||
"language" to "nl", | ||
"currentMeasurement" to mapOf( | ||
"createdAt" to 1337L | ||
) | ||
).toQ42StatsApiFormat().toString() | ||
assertEquals(expected, actual) | ||
).toQ42StatsApiFormat() | ||
|
||
testQ42StatsMap(actual) | ||
} | ||
|
||
private fun testQ42StatsMap(actual: Map<*, *>) { | ||
actual.forEach { | ||
when (it.value) { | ||
is Map<*, *> -> { | ||
testQ42StatsMap(it.value as Map<*, *>) | ||
} | ||
else -> { | ||
assert(it.value is String) { "Expected $it to have a string type but was ${it.value}" } | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.