package org.maplibre.reactnative.utils import android.graphics.PointF import android.util.Log import com.facebook.react.bridge.Arguments import com.facebook.react.bridge.NoSuchKeyException import com.facebook.react.bridge.ReadableArray import com.facebook.react.bridge.ReadableMap import com.facebook.react.bridge.ReadableType import com.facebook.react.bridge.WritableArray import com.facebook.react.bridge.WritableMap import com.google.gson.JsonArray import com.google.gson.JsonElement import com.google.gson.JsonObject import com.google.gson.JsonPrimitive import org.json.JSONException import org.json.JSONObject object ConvertUtils { const val LOG_TAG: String = "ConvertUtils" fun toJsonObject(map: ReadableMap?): JsonObject? { if (map == null) return null val result = JsonObject() val it = map.keySetIterator() while (it.hasNextKey()) { val key = it.nextKey() when (map.getType(key)) { ReadableType.Map -> result.add(key, toJsonObject(map.getMap(key))) ReadableType.Array -> result.add(key, toJsonArray(map.getArray(key))) ReadableType.Null -> result.add(key, null) ReadableType.Number -> result.addProperty(key, map.getDouble(key)) ReadableType.String -> result.addProperty(key, map.getString(key)) ReadableType.Boolean -> result.addProperty(key, map.getBoolean(key)) } } return result } @JvmStatic fun toJsonArray(array: ReadableArray?): JsonArray? { if (array == null) return null val result = JsonArray(array.size()) for (i in 0.. result.add(toJsonObject(array.getMap(i))) ReadableType.Array -> result.add(toJsonArray(array.getArray(i))) ReadableType.Null -> result.add(null as JsonElement?) ReadableType.Number -> result.add(array.getDouble(i)) ReadableType.String -> result.add(array.getString(i)) ReadableType.Boolean -> result.add(array.getBoolean(i)) } } return result } @JvmStatic fun typedToJsonElement(map: ReadableMap?): JsonElement? { if (map == null) return null val type = map.getString("type") when (type) { ExpressionParser.TYPE_MAP -> { val result = JsonObject() val keyValues = map.getArray("value") for (i in 0.. { val arrayValue = map.getArray("value") val result = JsonArray(arrayValue!!.size()) for (i in 0.. { return JsonPrimitive(map.getBoolean("value")) } ExpressionParser.TYPE_NUMBER -> { return JsonPrimitive(map.getDouble("value")) } ExpressionParser.TYPE_STRING -> { return JsonPrimitive(map.getString("value")) } else -> { throw RuntimeException(String.format("Unrecognized type {}", map.getString("type"))) } } } fun toWritableArray(array: JsonArray): WritableArray { val writableArray = Arguments.createArray() for (i in 0.. { val list: MutableList = ArrayList() if (array == null) { return list } for (i in 0..