Renamed wrapper to key.

This commit is contained in:
Erik C. Thauvin 2021-05-28 00:26:11 -07:00
parent fc861276ef
commit 0eaa067d84
2 changed files with 9 additions and 9 deletions

View file

@ -74,7 +74,7 @@ println(price.toJson())
{"data":{"base":"BTC","currency":"USD","amount":"34567.89"}} {"data":{"base":"BTC","currency":"USD","amount":"34567.89"}}
``` ```
The format matches the [Coinbase API](https://developers.coinbase.com/api/v2#get-spot-price). To specify a different (or no) object wrapper, use: The `data` object matches the [Coinbase API](https://developers.coinbase.com/api/v2#get-spot-price). To specify a different (or no) key, use:
```kotlin ```kotlin
println(price.toJson("bitcoin")) println(price.toJson("bitcoin"))

View file

@ -62,14 +62,14 @@ open class CryptoPrice(val base: String, val currency: String, val amount: BigDe
/** /**
* Converts JSON data object to [CryptoPrice]. * Converts JSON data object to [CryptoPrice].
* *
* @param wrapper Specifies the JSON object the price data is wrapped in. * @param key Specifies the JSON object key the price data is wrapped in.
*/ */
@JvmStatic @JvmStatic
@JvmOverloads @JvmOverloads
@Throws(CryptoException::class) @Throws(CryptoException::class)
fun String.toPrice(wrapper: String = "data"): CryptoPrice { fun String.toPrice(key: String = "data"): CryptoPrice {
try { try {
val json = if (wrapper.isNotBlank()) JSONObject(this).getJSONObject(wrapper) else JSONObject(this) val json = if (key.isNotBlank()) JSONObject(this).getJSONObject(key) else JSONObject(this)
with(json) { with(json) {
return CryptoPrice(getString("base"), getString("currency"), getString("amount").toBigDecimal()) return CryptoPrice(getString("base"), getString("currency"), getString("amount").toBigDecimal())
} }
@ -195,24 +195,24 @@ open class CryptoPrice(val base: String, val currency: String, val amount: BigDe
/** /**
* Returns a JSON representation of the [CryptoPrice]. * Returns a JSON representation of the [CryptoPrice].
* *
* For example, with the default `data` wrapper: * For example, with the default `data` object key:
* *
* ``` * ```
* {"data":{"base":"BTC","currency":"USD","amount":"58977.17"}} * {"data":{"base":"BTC","currency":"USD","amount":"58977.17"}}
* ``` * ```
* *
* @param wrapper Specifies a JSON object to wrap the price data in. * @param key Specifies a JSON object key to wrap the price data in.
*/ */
@JvmOverloads @JvmOverloads
fun toJson(wrapper: String = "data"): String { fun toJson(key: String = "data"): String {
val json = JSONStringer() val json = JSONStringer()
if (wrapper.isNotBlank()) json.`object`().key(wrapper) if (key.isNotBlank()) json.`object`().key(key)
json.`object`() json.`object`()
.key("base").value(base) .key("base").value(base)
.key("currency").value(currency) .key("currency").value(currency)
.key("amount").value(amount.toString()) .key("amount").value(amount.toString())
.endObject() .endObject()
if (wrapper.isNotBlank()) json.endObject() if (key.isNotBlank()) json.endObject()
return json.toString() return json.toString()
} }