package io.scanbot.sdk.reactnative.extensions import com.facebook.react.bridge.ReadableArray import com.facebook.react.bridge.ReadableMap import com.facebook.react.bridge.ReadableType import io.scanbot.sdk.barcode.BarcodeFormatConfigurationBase import org.json.JSONArray import org.json.JSONException import org.json.JSONObject @Throws(JSONException::class) fun ReadableMap.toJSON(): JSONObject { val jsonResult = JSONObject() val iterator = this.keySetIterator() while (iterator.hasNextKey()) { val key = iterator.nextKey() when (this.getType(key)) { ReadableType.Boolean -> jsonResult.put(key, this.getBoolean(key)) ReadableType.Number -> jsonResult.put(key, this.getDouble(key)) ReadableType.String -> jsonResult.put(key, this.getString(key)) ReadableType.Map -> jsonResult.put(key, this.getMap(key)?.toJSON()) ReadableType.Array -> jsonResult.put(key, this.getArray(key)?.toJSONArray()) ReadableType.Null -> { jsonResult.put(key, JSONObject.NULL) } } } return jsonResult } @Suppress("UNNECESSARY_SAFE_CALL") @Throws(JSONException::class) fun ReadableArray.toJSONArray(): JSONArray { val jsonResult = JSONArray() for (i in 0 until this.size()) { when (this.getType(i)) { ReadableType.Boolean -> jsonResult.put(this.getBoolean(i)) ReadableType.Number -> jsonResult.put(this.getDouble(i)) ReadableType.String -> jsonResult.put(this.getString(i)) ReadableType.Map -> jsonResult.put(this.getMap(i)?.toJSON()) ReadableType.Array -> jsonResult.put(this.getArray(i)?.toJSONArray()) ReadableType.Null -> { jsonResult.put(JSONObject.NULL) } } } return jsonResult } fun ReadableArray.extractBarcodeFormatConfigurations(): List { val formatConfigs = mutableListOf() for (index in 0..