|
| 1 | +package com.braintreepayments.api.sharedutils |
| 2 | + |
| 3 | +import androidx.annotation.RestrictTo |
| 4 | +import org.json.JSONObject |
| 5 | + |
| 6 | +@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) |
| 7 | +object Json { |
| 8 | + |
| 9 | + /** |
| 10 | + * Returns the value mapped by `name` from the given `json` object as a [String], or the provided nullable |
| 11 | + * `fallback`. |
| 12 | + * |
| 13 | + * If the `json` object is null, the `name` is null, or the value mapped by `name` is null, the provided nullable |
| 14 | + * `fallback` value is returned. |
| 15 | + * This works around the issue where `JSONObject.optString` returns the string "null" for null values. |
| 16 | + * |
| 17 | + * @param json The [JSONObject] to query, or null. |
| 18 | + * @param name The key to look up in the [JSONObject], or null. |
| 19 | + * @param fallback The nullable fallback value to return if the mapping does not exist or is null. |
| 20 | + * @return The mapped [String] value, or the nullable `fallback` if not found. |
| 21 | + */ |
| 22 | + fun optString(json: JSONObject?, name: String?, fallback: String?): String? { |
| 23 | + return if (json == null || name == null || json.isNull(name)) { |
| 24 | + fallback |
| 25 | + } else { |
| 26 | + if (fallback != null) { |
| 27 | + json.optString(name, fallback) |
| 28 | + } else { |
| 29 | + val result = json.optString(name) |
| 30 | + if (result.isEmpty() && json.isNull(name)) null else result |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Returns the value mapped by `name` from the given `json` object as a non-null [String]. |
| 37 | + * |
| 38 | + * If the `json` object is null, the `name` is null, or the value mapped by `name` is null, |
| 39 | + * the provided non-null `fallback` value is returned. |
| 40 | + * |
| 41 | + * @param json The [JSONObject] to query, or null. |
| 42 | + * @param name The key to look up in the [JSONObject], or null. |
| 43 | + * @param fallback The non-null fallback value to return if the mapping does not exist or is null. |
| 44 | + * @return The mapped [String] value, or the non-null `fallback` if not found. |
| 45 | + */ |
| 46 | + @JvmName("optStringNonNull") |
| 47 | + fun optString(json: JSONObject?, name: String?, fallback: String): String { |
| 48 | + return if (json == null || name == null || json.isNull(name)) { |
| 49 | + fallback |
| 50 | + } else { |
| 51 | + json.optString(name, fallback) |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Returns the value mapped by `name` from the given `json` object as a [Boolean], or the provided fallback value. |
| 57 | + * |
| 58 | + * If the `json` object is null, the `name` is null, or the value mapped by `name` is null, the provided fallback |
| 59 | + * value is returned. |
| 60 | + * |
| 61 | + * @param json The [JSONObject] to query, or null. |
| 62 | + * @param name The key to look up in the [JSONObject], or null. |
| 63 | + * @param fallback The fallback value to return if the mapping does not exist or is null. |
| 64 | + * @return The mapped [Boolean] value, or the fallback if not found. |
| 65 | + */ |
| 66 | + fun optBoolean(json: JSONObject?, name: String?, fallback: Boolean): Boolean { |
| 67 | + return if (json == null || name == null || json.isNull(name)) { |
| 68 | + fallback |
| 69 | + } else { |
| 70 | + json.optBoolean(name, fallback) |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments