algolia/algoliasearch-client-android

String json as alterative to (or with) JSONObject

Diolor opened this issue ยท 5 comments

Hey ๐Ÿ‘‹,

would be great if the Index.searchXXX() functions return json as String and not (only) as JSONObject. JSONObject is somehow cumbersome if you (I) want to use a de/serializer (e.g. moshi, kotlinx, etc) for the payload. Especially for complex objects.

Let's take as an example Index.searchSync(Query):
Since you might like to keep JSONObject as returning type there, one idea to do it (fairly) easy is to return a class Result type. This Result can both provide a rawResponse : String to be used from ones who want to use a deserializer library and a jsonObject : JSONObject which you either way convert from the byte[] internally.

Hello Diolor,

Your use case is perfectly understandable.

We plan to deprecate this client in favour of a new client written entirely in Kotlin. This client will allow you to deserialize search hits (and others) with the kotlinx.serialization library.

In the meantime, it is always possible to convert the JSONObject to a string using .toString(), and use that as an input to another serialization mechanism such as kotlinx or Moshi.
This is obviously not the cleanest solution.

We will not add new features to this client as we are a few weeks away from releasing the Kotlin client, and deprecating this one.

I would be curious to know if you are a Kotlin or a Java user. If you are interested about the new client, I would be happy to provide more technical information about it.

Understood. And duuh...it never crossed my mind the simple .toString() solution...

In the meantime, I went fully manual with rest calls + usual Retrofit stack + moshi.

To your question, I am a Kotlin user. My 2 cents: Trying kotlinx few days ago was a bit cumbersome since it's early days for the library so I think some users might appreciate a string result option.

Feel free to keep this issue open or close it at your convenience!

To your question, I am a Kotlin user. My 2 cents: Trying kotlinx few days ago was a bit cumbersome since it's early days for the library so I think some users might appreciate a string result option.

What specifically posed you problem with kotlinx serialization ? I might be able to help.

Library api/design decisions like these Kotlin/kotlinx.serialization#19

They just changed the @Optional to a @Required logic. Now there's less code to write.

It's already available in the latest release.

data class Data(
   val optional : String? = null,
   val default: String,
   @Required val required: String
)