Skip to content
Permalink
Browse files

Issue #466: Fretboard: Move JSONExtensions into support-ktx

Closes #466: Fretboard: Move JSONExtensions into support-ktx
  • Loading branch information...
fercarcedo authored and pocmo committed Jul 20, 2018
1 parent 5a999c4 commit 0acf5a5ca79a743d768b5fe80fd04ba35ba5f5ec
@@ -22,6 +22,7 @@ android {
}

dependencies {
implementation project(':support-ktx')
implementation "org.jetbrains.kotlin:kotlin-stdlib:${rootProject.ext.dependencies['kotlin']}"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:${rootProject.ext.dependencies['coroutines']}"

@@ -4,6 +4,12 @@

package mozilla.components.service.fretboard

import mozilla.components.support.ktx.kotlin.putIfNotNull
import mozilla.components.support.ktx.kotlin.toJsonArray
import mozilla.components.support.ktx.kotlin.toList
import mozilla.components.support.ktx.kotlin.tryGetInt
import mozilla.components.support.ktx.kotlin.tryGetLong
import mozilla.components.support.ktx.kotlin.tryGetString
import org.json.JSONArray
import org.json.JSONObject

@@ -20,7 +26,7 @@ class JSONExperimentParser {
fun fromJson(jsonObject: JSONObject): Experiment {
val bucketsObject: JSONObject? = jsonObject.optJSONObject(BUCKETS_KEY)
val matchObject: JSONObject? = jsonObject.optJSONObject(MATCH_KEY)
val regions: List<String>? = matchObject?.optJSONArray(REGIONS_KEY)?.toList()
val regions: List<String>? = matchObject?.optJSONArray(REGIONS_KEY).toList()
val matcher = if (matchObject != null) {
Experiment.Matcher(
matchObject.tryGetString(LANG_KEY),

This file was deleted.

Oops, something went wrong.
@@ -24,3 +24,16 @@ fun JSONArray.asSequence(): Sequence<Any> {
}
}
}

/**
* Convenience method to convert a JSONArray into a List
*
* @return list with the JSONArray values, or an empty list if the JSONArray was null
*/
@Suppress("UNCHECKED_CAST")
fun <T> JSONArray?.toList(): List<T> {
if (this != null) {
return asSequence().map { it as T }.toList()
}
return listOf()
}
@@ -0,0 +1,68 @@
/* 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.support.ktx.kotlin

import org.json.JSONObject
import java.util.TreeMap

/**
* Returns the value mapped by {@code key} if it exists, and
* if the value returned is not null. If it's null, it returns null
*/
fun JSONObject.tryGetString(key: String): String? {
if (!isNull(key)) {
return getString(key)
}
return null
}

/**
* Returns the value mapped by {@code key} if it exists, and
* if the value returned is not null. If it's null, it returns null
*/
fun JSONObject.tryGetInt(key: String): Int? {
if (!isNull(key)) {
return getInt(key)
}
return null
}

/**
* Returns the value mapped by {@code key} if it exists, and
* if the value returned is not null. If it's null, it returns null
*/
fun JSONObject.tryGetLong(key: String): Long? {
if (!isNull(key)) {
return getLong(key)
}
return null
}

/**
* Puts the specified value under the key if it's not null
*/
fun JSONObject.putIfNotNull(key: String, value: Any?) {
if (value != null) {
put(key, value)
}
}

/**
* Sorts the keys of a JSONObject (and all of its child JSONObjects) alphabetically
*/
fun JSONObject.sortKeys(): JSONObject {
val map = TreeMap<String, Any>()
for (key in this.keys()) {
map[key] = this[key]
}
val jsonObject = JSONObject()
for (key in map.keys) {
if (map[key] is JSONObject) {
map[key] = (map[key] as JSONObject).sortKeys()
}
jsonObject.put(key, map[key])
}
return jsonObject
}
@@ -0,0 +1,14 @@
/* 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.support.ktx.kotlin

import org.json.JSONArray

/**
* Converts the list into a JSON array
*/
fun <T> List<T>.toJsonArray(): JSONArray {
return fold(JSONArray()) { jsonArray, element -> jsonArray.put(element) }
}
@@ -5,6 +5,7 @@
package mozilla.components.support.ktx.kotlin

import junit.framework.Assert.assertEquals
import junit.framework.Assert.assertTrue
import org.json.JSONArray
import org.junit.Test
import org.junit.runner.RunWith
@@ -23,4 +24,29 @@ class JSONArrayTest {

assertEquals(6, sum)
}
}

@Test
fun testToListNull() {
val jsonArray: JSONArray? = null
val list = jsonArray.toList<Any>()
assertEquals(0, list.size)
}

@Test
fun testToListEmpty() {
val jsonArray = JSONArray()
val list = jsonArray.toList<Any>()
assertEquals(0, list.size)
}

@Test
fun testToListNotEmpty() {
val jsonArray = JSONArray()
jsonArray.put("value")
jsonArray.put("another-value")
val list = jsonArray.toList<String>()
assertEquals(2, list.size)
assertTrue(list.contains("value"))
assertTrue(list.contains("another-value"))
}
}
@@ -0,0 +1,79 @@
package mozilla.components.support.ktx.kotlin

import org.json.JSONArray
import org.json.JSONObject
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
class JSONObjectTest {
@Test
fun testSortKeys() {
val jsonObject = JSONObject()
jsonObject.put("second-key", "second-value")
jsonObject.put("third-key", JSONArray().apply {
put(1)
put(2)
put(3)
})
jsonObject.put("first-key", JSONObject().apply {
put("one-key", "one-value")
put("a-key", "a-value")
put("second-key", "second")
})
assertEquals("""{"first-key":{"a-key":"a-value","one-key":"one-value","second-key":"second"},"second-key":"second-value","third-key":[1,2,3]}""", jsonObject.sortKeys().toString())
}

@Test
fun testPutIfNotNull() {
val jsonObject = JSONObject()
assertEquals(0, jsonObject.length())
jsonObject.putIfNotNull("key", null)
assertEquals(0, jsonObject.length())
jsonObject.putIfNotNull("key", "value")
assertEquals(1, jsonObject.length())
assertEquals("value", jsonObject["key"])
}

@Test
fun testTryGetStringNull() {
val jsonObject = JSONObject("""{"key":null}""")
assertNull(jsonObject.tryGetString("key"))
assertNull(jsonObject.tryGetString("another-key"))
}

@Test
fun testTryGetStringNotNull() {
val jsonObject = JSONObject("""{"key":"value"}""")
assertEquals("value", jsonObject.tryGetString("key"))
}

@Test
fun testTryGetLongNull() {
val jsonObject = JSONObject("""{"key":null}""")
assertNull(jsonObject.tryGetLong("key"))
assertNull(jsonObject.tryGetLong("another-key"))
}

@Test
fun testTryGetLongNotNull() {
val jsonObject = JSONObject("""{"key":218728173837192717}""")
assertEquals(218728173837192717, jsonObject.tryGetLong("key"))
}

@Test
fun testTryGetIntNull() {
val jsonObject = JSONObject("""{"key":null}""")
assertNull(jsonObject.tryGetInt("key"))
assertNull(jsonObject.tryGetInt("another-key"))
}

@Test
fun testTryGetIntNotNull() {
val jsonObject = JSONObject("""{"key":3}""")
assertEquals(3, jsonObject.tryGetInt("key"))
}
}
@@ -0,0 +1,25 @@
package mozilla.components.support.ktx.kotlin

import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
class ListTest {
@Test
fun testToJsonArrayEmptyList() {
val jsonArray = listOf<String>().toJsonArray()
assertEquals(0, jsonArray.length())
}

@Test
fun testToJsonArrayNotEmptyList() {
val list = listOf(1, 2, 3)
val jsonArray = list.toJsonArray()
assertEquals(3, jsonArray.length())
assertEquals(1, jsonArray[0])
assertEquals(2, jsonArray[1])
assertEquals(3, jsonArray[2])
}
}

0 comments on commit 0acf5a5

Please sign in to comment.
You can’t perform that action at this time.