Commit 649e83bb authored by Michael Comella's avatar Michael Comella Committed by Sebastian Kaspari
Browse files

Issue #651: Add JSONArray.mapObjNotNull convenience function.

This utility function is used by the existing FFTV Pocket code and helps
make the code more terse.
parent 787b06c7
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package mozilla.components.service.pocket.ext
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
/**
* Map over the JSONArray, removing null values after [transform] and ignoring data that throws a [JSONException].
* An empty List will be returned if all the values are null or invalid.
*
* We don't expose this function publicly because it's not generic enough yet: JSONObject should be T. We don't
* want to expose this temporarily because then we'll break the public API.
*/
internal inline fun <R : Any> JSONArray.mapObjNotNull(transform: (JSONObject) -> R?): List<R> {
val transformedResults = mutableListOf<R>()
for (i in 0 until this.length()) {
try {
val transformed = transform(this.getJSONObject(i))
if (transformed != null) { transformedResults.add(transformed) }
} catch (e: JSONException) { /* Do nothing: we skip bad data. */ }
}
return transformedResults
}
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package mozilla.components.service.pocket.ext
import org.json.JSONArray
import org.json.JSONObject
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
@RunWith(RobolectricTestRunner::class)
class JSONKtTest {
@Test
fun `WHEN flatMapObj on an empty jsonArray THEN an empty list is returned`() {
assertEquals(emptyList<Int>(), JSONArray().mapObjNotNull { 1 })
}
@Test
fun `WHEN flatMapObj transform throws a JSONException THEN that item is ignored`() {
val input = JSONArray().apply {
put(JSONObject("""{"a": 1}"""))
put(JSONObject("""{"b": 2}"""))
}
val actual = input.mapObjNotNull {
it.get("b") // key not found for first item: throws an exception.
}
assertEquals(1, actual.size)
assertEquals(2, actual[0])
}
@Test
fun `WHEN flatMapObj on a list THEN nulls are removed and data is mapped`() {
val expected = listOf(
"a" to 1,
"b" to 2,
"c" to 3
)
// Convert expected input: [JSONObject("a" to 1), null, JSONObject("b" to 2), null, ...]
val input = JSONArray()
expected.forEach {
val obj = JSONObject().apply { put(it.first, it.second) }
input.put(obj)
input.put(null)
}
val actual = input.mapObjNotNull {
val keys = it.keys().asSequence().toList()
assertEquals(it.toString(), 1, keys.size)
val key = keys.first()
val value = it.get(key) as Int
Pair(key, value)
}
assertEquals(expected, actual)
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment