Skip to content

Commit bec1e8f

Browse files
Replace gson with kotlin serialization (#209)
* replace gson with kotlin serialization * Remove unused plugin * Fix missing library reference * Remove unused json library --------- Co-authored-by: Fabian Keunecke <f@biankeunecke.de>
1 parent fd1a6c3 commit bec1e8f

9 files changed

Lines changed: 97 additions & 89 deletions

File tree

app/build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
plugins {
22
alias(libs.plugins.android.application)
33
alias(libs.plugins.kotlin.android)
4+
alias(libs.plugins.kotlinx.serialization)
45
}
56

67
apply {
@@ -88,6 +89,9 @@ dependencies {
8889
// Logging
8990
implementation(libs.timber)
9091

92+
// Serialization
93+
implementation(libs.kotlinx.serialization.json)
94+
9195
// Leak Canary
9296
implementation(libs.leakcanary)
9397

app/src/main/java/org/maplibre/navigation/android/example/GraphHopperNavigationActivity.kt

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import android.os.Bundle
55
import android.view.View
66
import androidx.appcompat.app.AppCompatActivity
77
import com.google.android.material.snackbar.Snackbar
8-
import com.google.gson.Gson
8+
import kotlinx.serialization.json.*
99
import org.maplibre.geojson.model.Point
1010
import org.maplibre.android.annotations.MarkerOptions
1111
import org.maplibre.android.camera.CameraPosition
@@ -171,17 +171,21 @@ class GraphHopperNavigationActivity :
171171

172172
// The full GraphHopper API is documented here:
173173
// https://docs.graphhopper.com/openapi/routing
174-
val requestBody = mapOf(
175-
"type" to "mapbox",
176-
"profile" to "car",
177-
"locale" to language,
178-
"points" to listOf(
179-
listOf(origin.longitude, origin.latitude),
180-
listOf(destination.longitude, destination.latitude)
181-
)
182-
)
183-
184-
val requestBodyJson = Gson().toJson(requestBody)
174+
val requestBodyJson = buildJsonObject {
175+
put("type", "mapbox")
176+
put("profile", "car")
177+
put("locale", language)
178+
putJsonArray("points") {
179+
addJsonArray {
180+
add(origin.longitude)
181+
add(origin.latitude)
182+
}
183+
addJsonArray {
184+
add(destination.longitude)
185+
add(destination.latitude)
186+
}
187+
}
188+
}.toString()
185189
val client = OkHttpClient()
186190

187191
// Create request object. Requires graphhopper_url to be set in developer-config.xml

app/src/main/java/org/maplibre/navigation/android/example/ValhallaNavigationActivity.kt

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import android.os.Bundle
55
import android.view.View
66
import androidx.appcompat.app.AppCompatActivity
77
import com.google.android.material.snackbar.Snackbar
8-
import com.google.gson.Gson
8+
import kotlinx.serialization.json.*
99
import org.maplibre.geojson.Point
1010
import org.maplibre.android.annotations.MarkerOptions
1111
import org.maplibre.android.camera.CameraPosition
@@ -180,35 +180,33 @@ class ValhallaNavigationActivity :
180180
// That would allow us to skip adding fake attributes further down as well.
181181
// But this is the first step to show how the newly added banner_instructions
182182
// and voice_instructions of Valhalla can be used to generate directions directly:
183-
val requestBody = mapOf(
184-
"format" to "osrm",
185-
"costing" to "auto",
186-
"banner_instructions" to true,
187-
"voice_instructions" to true,
188-
"language" to language,
189-
"directions_options" to mapOf(
190-
"units" to "kilometers"
191-
),
192-
"costing_options" to mapOf(
193-
"auto" to mapOf(
194-
"top_speed" to 130
195-
)
196-
),
197-
"locations" to listOf(
198-
mapOf(
199-
"lon" to origin.longitude(),
200-
"lat" to origin.latitude(),
201-
"type" to "break"
202-
),
203-
mapOf(
204-
"lon" to destination.longitude(),
205-
"lat" to destination.latitude(),
206-
"type" to "break"
207-
)
208-
)
209-
)
210-
211-
val requestBodyJson = Gson().toJson(requestBody)
183+
val requestBodyJson = buildJsonObject {
184+
put("format", "osrm")
185+
put("costing", "auto")
186+
put("banner_instructions", true)
187+
put("voice_instructions", true)
188+
put("language", language)
189+
putJsonObject("directions_options") {
190+
put("units", "kilometers")
191+
}
192+
putJsonObject("costing_options") {
193+
putJsonObject("auto") {
194+
put("top_speed", 130)
195+
}
196+
}
197+
putJsonArray("locations") {
198+
addJsonObject {
199+
put("lon", origin.longitude())
200+
put("lat", origin.latitude())
201+
put("type", "break")
202+
}
203+
addJsonObject {
204+
put("lon", destination.longitude())
205+
put("lat", destination.latitude())
206+
put("type", "break")
207+
}
208+
}
209+
}.toString()
212210
val client = OkHttpClient()
213211

214212
// Create request object. Requires valhalla_url to be set in developer-config.xml

gradle/libs.versions.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ androidx-cardview = "1.0.0"
2121
androidx-lifecycle-viewmodel = "2.10.0"
2222
androidx-multidex = "2.0.1"
2323
picasso = "2.71828"
24-
json = "20250517"
2524
timber = "5.0.1"
2625
autovalue = "1.11.1"
2726
mapbox-geojson = "5.8.0"
@@ -62,7 +61,6 @@ autovalue = { group = "com.google.auto.value", name = "auto-value", version.ref
6261
autovalue-annotations = { group = "com.google.auto.value", name = "auto-value-annotations", version.ref = "autovalue" }
6362
picasso = { group = "com.squareup.picasso", name = "picasso", version.ref = "picasso" }
6463
timber = { group = "com.jakewharton.timber", name = "timber", version.ref = "timber" }
65-
json = { group = "org.json", name = "json", version.ref = "json" }
6664
leakcanary = { group = "com.squareup.leakcanary", name = "leakcanary-android", version.ref = "leakcanary" }
6765

6866
# Test KMP

libandroid-navigation-ui/build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ dependencies {
105105
testImplementation(libs.junit)
106106
testImplementation(libs.mockito)
107107
testImplementation(libs.robolectric)
108-
testImplementation(libs.json)
109108
testImplementation(libs.mockk)
110109
}
111110

libandroid-navigation-ui/src/main/java/org/maplibre/navigation/android/navigation/ui/v5/NavigationLauncher.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import android.content.SharedPreferences;
77
import android.preference.PreferenceManager;
88

9-
import com.google.gson.Gson;
10-
119
import org.maplibre.navigation.android.navigation.ui.v5.route.NavigationRoute;
1210
import org.maplibre.navigation.core.models.DirectionsRoute;
1311
import org.maplibre.navigation.core.navigation.NavigationConstants;
@@ -53,7 +51,7 @@ public static void startNavigation(Context context, NavigationLauncherOptions op
5351
* Used to extract the route used to launch the drop-in UI.
5452
* <p>
5553
* Extracts the route {@link String} from {@link SharedPreferences} and converts
56-
* it back to a {@link DirectionsRoute} object with {@link Gson}.
54+
* it back to a {@link DirectionsRoute} object.
5755
*
5856
* @param context to retrieve {@link SharedPreferences}
5957
* @return {@link DirectionsRoute} stored when launching

sample/android/app/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ dependencies {
5252
implementation(libs.maplibre.navigation.core)
5353
implementation(libs.maplibre.geojson)
5454

55+
implementation(libs.kotlinx.serialization.json)
5556
implementation(libs.okhttp)
5657
implementation(libs.androidx.core.ktx)
5758
implementation(libs.androidx.appcompat)

sample/android/app/src/main/java/org/maplibre/navigation/sample/android/core/CoreOnlyFragment.kt

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import androidx.core.view.ViewCompat
1010
import androidx.core.view.WindowInsetsCompat
1111
import androidx.fragment.app.Fragment
1212
import androidx.lifecycle.lifecycleScope
13-
import com.google.gson.Gson
13+
import kotlinx.serialization.json.*
1414
import kotlinx.coroutines.launch
1515
import okhttp3.Call
1616
import okhttp3.MediaType.Companion.toMediaType
@@ -148,53 +148,56 @@ class CoreOnlyFragment : Fragment() {
148148

149149
private suspend fun fetchRoute(): DirectionsResponse = suspendCoroutine { continuation ->
150150
val provider = "valhalla"
151-
152-
val requestBody = if (provider == "graphhopper") {
153-
mapOf(
154-
"type" to "mapbox",
155-
"profile" to "car",
156-
"locale" to "en-US",
157-
"points" to listOf(
151+
val requestBodyJson = if (provider == "graphhopper") {
152+
buildJsonObject {
153+
put("type", "mapbox")
154+
put("profile", "car")
155+
put("locale", "en-US")
156+
putJsonArray("points") {
158157
// Hannover, Germany
159-
listOf(9.6935451, 52.3758408),
158+
addJsonArray {
159+
add(9.6935451)
160+
add(52.3758408)
161+
}
160162
// Hamburg, Germany
161-
listOf(9.9769191, 53.5426183)
162-
)
163+
addJsonArray {
164+
add(9.9769191)
165+
add(53.5426183)
166+
}
167+
}
163168
// flexible options possible via "custom_model"
164-
)
169+
}.toString()
165170
} else {
166-
mapOf(
167-
"format" to "osrm",
168-
"costing" to "auto",
169-
"banner_instructions" to true,
170-
"voice_instructions" to true,
171-
"language" to "en-US",
172-
"directions_options" to mapOf(
173-
"units" to "kilometers"
174-
),
175-
"costing_options" to mapOf(
176-
"auto" to mapOf(
177-
"top_speed" to 130
178-
)
179-
),
180-
"locations" to listOf(
171+
buildJsonObject {
172+
put("format", "osrm")
173+
put("costing", "auto")
174+
put("banner_instructions", true)
175+
put("voice_instructions", true)
176+
put("language", "en-US")
177+
putJsonObject("directions_options") {
178+
put("units", "kilometers")
179+
}
180+
putJsonObject("costing_options") {
181+
putJsonObject("auto") {
182+
put("top_speed", 130)
183+
}
184+
}
185+
putJsonArray("locations") {
181186
// Hannover, Germany
182-
mapOf(
183-
"lon" to 9.6935451,
184-
"lat" to 52.3758408,
185-
"type" to "break"
186-
),
187+
addJsonObject {
188+
put("lon", 9.6935451)
189+
put("lat", 52.3758408)
190+
put("type", "break")
191+
}
187192
// Hamburg, Germany
188-
mapOf(
189-
"lon" to 9.9769191,
190-
"lat" to 53.5426183,
191-
"type" to "break"
192-
)
193-
)
194-
)
193+
addJsonObject {
194+
put("lon", 9.9769191)
195+
put("lat", 53.5426183)
196+
put("type", "break")
197+
}
198+
}
199+
}.toString()
195200
}
196-
197-
val requestBodyJson = Gson().toJson(requestBody)
198201
val client = OkHttpClient()
199202

200203
val url = if (provider == "valhalla") "https://valhalla1.openstreetmap.de/route"

sample/android/gradle/libs.versions.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ maplibre = "11.6.1"
66
maplibre-geojson = "343269b2"
77
maplibre-navigation = "411fb14c"
88

9+
kotlinx-serialization-json = "1.9.0"
10+
911
coreKtx = "1.15.0"
1012
junit = "4.13.2"
1113
junitVersion = "1.2.1"
@@ -24,6 +26,7 @@ androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version
2426
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
2527
androidx-activity = { group = "androidx.activity", name = "activity", version.ref = "activity" }
2628
androidx-constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "constraintlayout" }
29+
kotlinx-serialization-json = { group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-json", version.ref = "kotlinx-serialization-json" }
2730

2831
okhttp = { group = "com.squareup.okhttp3", name ="okhttp", version = "4.12.0" }
2932

0 commit comments

Comments
 (0)