[Kotlin] Depending on if a value is returned from the server or cache, adapter reader has a different representation of the same object

In authoring new custom scalar adapters, I now notice that the fromJson reader value is a json object when the value is from the server and alternatively a json string if that same value is fetched instead from the cache. Is this the expected behavior?

If I check for the representation, I seem to be able to handle it.

// GeoJSONGeometry
// A geometry encoded as any valid GeoJSON geometry type. See https://geojson.org.

val geometryAdapter = object : Adapter<Geometry> {
    override fun fromJson(reader: JsonReader, customScalarAdapters: CustomScalarAdapters): Geometry {
        val peek = reader.peek()
        if (peek == JsonReader.Token.STRING) {
            val geometry = reader.nextString()!!

            return try {

        val map = AnyAdapter.fromJson(reader, customScalarAdapters) as Map<*, *>

        return when (map["type"]) {

            "Polygon" -> {
                val coordinates = (map["coordinates"] as List<List<List<Double>>>).map {
                    it.map { point -> Point.fromLngLat(point[0], point[1]) }


When using the cache, your scalar adapters must implement toJson: it is called when writing to the cache. This means toJson and fromJson must be consistent: if toJson expects a Json Object, then toJson must output a Json Object too. Did your toJson output a String? If yes that would explain what you’re seeing.

Ah, right - running it back through the any adapter for output did the trick. Thanks!