Can I convert data when parsing it from json to MutableLiveData - json

Can I convert data when parsing it from json to MutableLiveData and how if it's possible?
#TypeConverter
fun stringToDatas(string: String) :MutableLiveData<HashMap<String,Any>> {
val liveData = object : TypeToken<MutableLiveData<HashMap<String, Any>>>() {
//here i guess should return it.
//and how can i return it from string to MutableLiveData
}.type
return Gson().fromJson(string, liveData)
}
Thank you.

It could be done this way
#TypeConverter
fun stringToDatas(string: String) :MutableLiveData<HashMap<String,Any>> {
val map= object : TypeToken<HashMap<String, Any>>() {}.type
return MutableLiveData<HashMap<String,Any>>(Gson().fromJson(string, map))
}
UPDATE
#TypeConverter
fun datasToString(liveData: MutableLiveData<HashMap<String,Any>>): String {
return gson.toJson(liveData.value)
}

Related

Kotlinx Deserialization Object or List of Object to List of Object

I am currently implementing an API client with Ktor. The API I am requesting does not return a consistent JSON format.
for Example:
sometimes the JSON looks like this:
{
"description": {
"lang": "en",
"value": "an English description..."
},
...
}
and sometimes like this:
{
"description": [
{
"lang": "en",
"value": "an English description..."
},
{
"lang": "fr",
"value": "a French description..."
}
],
...
}
Now my Question:
How can I implement a Custom Kotlinx Deserializer to Decode an Object of T or a List<T> to a List<T>
My classes look like this:
#Serializable
class ResourceResponse(
#SerialName("description")
val descriptions: List<Description>
) {
#Serializable
data class Description(
#SerialName("value")
val value: String,
#SerialName("lang")
val language: String,
)
}
I want that a Json with only one Description-Object will be deserialized to a List with one Object and not specifically for the description, but in general for classes.
I've found nothing really helpful in the Web.
One solution is to first deserialize it to JsonElement, introspect and then decide how to deserialize it further into ResourceResponse:
fun decode(s: String): ResourceResponse {
val json = Json.parseToJsonElement(s).jsonObject
return when (val desc = json["description"]) {
is JsonArray -> Json.decodeFromJsonElement(json)
is JsonObject -> {
val json2 = json.toMutableMap()
json2["description"] = JsonArray(listOf(desc))
Json.decodeFromJsonElement(JsonObject(json2))
}
else -> throw IllegalArgumentException("Invalid value for \"description\": $desc")
}
}
This solution is definitely not ideal. It may be potentially less performant as we need to deserialize the whole tree into the tree of JsonElement objects only to transform it to the final types (although, maybe the library does this internally anyway). It works only for json and it is tricky to use this solution if ResourceResponse is somewhere deep into the data structure.
You can use a JsonContentPolymorphicSerializer to choose a deserializer based on the form of the JSON.
This one should work:
#Suppress("UNCHECKED_CAST")
class DescriptionsSerializer : JsonContentPolymorphicSerializer<List<ResourceResponse.Description>>(
List::class as KClass<List<ResourceResponse.Description>>
) {
// Here we check the form of the JSON we are decoding, and choose
// the serializer accordingly
override fun selectDeserializer(element: JsonElement): DeserializationStrategy<out List<ResourceResponse.Description>> {
return if (element is JsonArray)
ListSerializer(ResourceResponse.Description.serializer())
else
SingleDescriptionAsList()
}
class SingleDescriptionAsList : KSerializer<List<ResourceResponse.Description>> {
override val descriptor: SerialDescriptor
get() = ResourceResponse.Description.serializer().descriptor
override fun deserialize(decoder: Decoder): List<ResourceResponse.Description> {
return listOf(ResourceResponse.Description.serializer().deserialize(decoder))
}
override fun serialize(encoder: Encoder, value: List<ResourceResponse.Description>) {
throw Exception("Not in use")
}
}
}
You must also amend your original class to tell it to use this serializer:
#Serializable
class ResourceResponse(
#SerialName("description")
#Serializable(with = DescriptionsSerializer::class) val descriptions: List<Description>
) {
#Serializable
data class Description(
#SerialName("value")
val value: String,
#SerialName("lang")
val language: String,
)
}
Then you will be able to decode JSON objects with the single key "descriptions" using the ResourceResponse serializer.
For avoidance of doubt, if there are other keys in the JSON (it's not entirely clear from the question) then those should also be written into ResourceResponse definition.
After my research, I have now come up with a solution. For this you need a wrapper class. (here GenericResponse). I hope I can help others who have the same problem.
This is the Wrapper-Class
#Serializable(with = ListOrObjectSerializer::class)
class GenericResponse<T>(
val data: List<T> = emptyList()
) {
private var _isNothing : Boolean = false
val isNothing: Boolean
get() {
return this._isNothing
}
companion object {
fun <T> nothing(): GenericResponse<T> {
val o = GenericResponse(emptyList<T>())
o._isNothing = true
return o
}
}
}
And the Serializer looks like:
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.*
class ListOrObjectSerializer<T : Any>(private val tSerializer: KSerializer<T>): KSerializer<GenericResponse<T>> {
override val descriptor: SerialDescriptor
get() = tSerializer.descriptor
override fun deserialize(decoder: Decoder): GenericResponse<T> {
val input = decoder as JsonDecoder
val jsonObj = input.decodeJsonElement()
return when(jsonObj) {
is JsonObject -> GenericResponse(listOf(Json.decodeFromJsonElement(tSerializer, jsonObj)))
is JsonArray -> GenericResponse(Json.decodeFromJsonElement(ListSerializer(tSerializer), jsonObj))
else -> return GenericResponse.nothing()
}
}
override fun serialize(encoder: Encoder, value: GenericResponse<T>) {
throw IllegalAccessError("serialize not supported")
}
}
My Data-Class look now like:
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
#Serializable
class ResourceResponse(
#SerialName("description")
val descriptions: GenericResponse<Description>? = null,
) {
#Serializable
data class Description(
#SerialName("value")
val value: String? = null,
#SerialName("lang")
val language: String? = null,
)
}
data class ResourceResponse(
#SerializedName("description") val descriptions: List<Description>,
)
data class Description(
#SerializedName("value") val value: String,
#SerializedName("lang") val language: String,
)
it should be like that

Custom KotlinX Serializer for List class

I want to make a custom List serializer that will parse invalid json arrays safely. Example: list of Int [1, "invalid_int", 2] should be parsed as [1, 2].
I've made a serializer and added it to Json provider, but serialization keeps failing after first element and cannot continue, so I'm getting list of 1 element [1], how can I handle invalid element correctly so decoder will keep parsing other elements?
class SafeListSerializerStack<E>(val elementSerializer: KSerializer<E>) : KSerializer<List<E>> {
override val descriptor: SerialDescriptor = ListSerializer(elementSerializer).descriptor
override fun serialize(encoder: Encoder, value: List<E>) {
val size = value.size
val composite = encoder.beginCollection(descriptor, size)
val iterator = value.iterator()
for (index in 0 until size) {
composite.encodeSerializableElement(descriptor, index, elementSerializer, iterator.next())
}
composite.endStructure(descriptor)
}
override fun deserialize(decoder: Decoder): List<E> {
val arrayList = arrayListOf<E>()
try {
val startIndex = arrayList.size
val messageBuilder = StringBuilder()
val compositeDecoder = decoder.beginStructure(descriptor)
while (true) {
val index = compositeDecoder.decodeElementIndex(descriptor) // fails here on number 2
if (index == CompositeDecoder.DECODE_DONE) {
break
}
try {
arrayList.add(index, compositeDecoder.decodeSerializableElement(descriptor, startIndex + index, elementSerializer))
} catch (exception: Exception) {
exception.printStackTrace() // falls here when "invalid_int" is parsed, it's ok
}
}
compositeDecoder.endStructure(descriptor)
if (messageBuilder.isNotBlank()) {
println(messageBuilder.toString())
}
} catch (exception: Exception) {
exception.printStackTrace() // falls here on number 2
}
return arrayList
}
}
Error happens after invalid element is parsed and exception is thrown at compositeDecoder.decodeElementIndex(descriptor) line with:
kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 4: Expected end of the array or comma
JSON input: [1, "invalid_int", 2]
I had a feeling that it should "swallow" invalid element and just keep moving, but instead it's stuck and cannot continue parsing, which doesn't make sense to me.
This could be done without custom serializer. Just parse everything as a String (specify isLenient = true to allow unquoted strings) and then convert to Int all valid integers:
fun main() {
val input = "[1, \"invalid_int\", 2]"
val result: List<Int> = Json { isLenient = true }
.decodeFromString<List<String>>(input)
.mapNotNull { it.toIntOrNull() }
println(result) // [1, 2]
}
In a more generic case (when the list is a field and/or its elements are not simple Ints), you'll need a custom serializer:
class SafeListSerializerStack<E>(private val elementSerializer: KSerializer<E>) : KSerializer<List<E>> {
private val listSerializer = ListSerializer(elementSerializer)
override val descriptor: SerialDescriptor = listSerializer.descriptor
override fun serialize(encoder: Encoder, value: List<E>) {
listSerializer.serialize(encoder, value)
}
override fun deserialize(decoder: Decoder): List<E> = with(decoder as JsonDecoder) {
decodeJsonElement().jsonArray.mapNotNull {
try {
json.decodeFromJsonElement(elementSerializer, it)
} catch (e: SerializationException) {
e.printStackTrace()
null
}
}
}
}
Note that this solution works only with deserialization from the Json format and requires kotlinx.serialization 1.2.0+
Found a way, we can extract json array from decoder given we are using Json to parse it
override fun deserialize(decoder: Decoder): List<E> {
val jsonInput = decoder as? JsonDecoder
?: error("Can be deserialized only by JSON")
val rawJson = jsonInput.decodeJsonElement()
if (rawJson !is JsonArray) {
return arrayListOf()
}
val jsonArray = rawJson.jsonArray
val jsonParser = jsonInput.json
val arrayList = ArrayList<E>(jsonArray.size)
jsonArray.forEach { jsonElement ->
val result = readElement(jsonParser, jsonElement)
when {
result.isSuccess -> arrayList.add(result.getOrThrow())
result.isFailure -> Log.d("ERROR", "error parsing array")
}
}
arrayList.trimToSize()
return arrayList
}
private fun readElement(json: Json, jsonElement: JsonElement): Result<E> {
return try {
Result.success(json.decodeFromJsonElement(elementSerializer, jsonElement))
} catch (exception: Exception) {
Result.failure(exception)
}
}

how to return a specific JSON object by value

I have my JSON object below
{
"test1": 1,
"test2": 2
}
Suppose I wanted to return just the "test1" json object. How would I go about doing this? The code I currently have returns everything. Below is the code I have that returns the JSON
fun json(): JSONObject {
val map: Map<String, Int> = x().map{ it.name to it.age }.toMap()
return JSONObject(map)
}
Add a filter before map with condition you need :
x()
.filter {it.name == "test1"}
.map { ... }
.toMap()

Why does my Gson object keep returning null?

I'm trying to parse JSON data to a class but gson.fromJson(response, bitt::class.java) keeps returning null.
class bitt(#SerializedName("result")val result: String) {
val someVal: String = "string"
fun method() {
print("something")
}
}
val response: String = "{'success':true,'message':'','result':'Im a sult'}"
println(response)
val gson = Gson()
val ticker = gson.fromJson(response, bitt::class.java)
println(ticker)
What am I doing wrong here?
JSON always uses double quotes ", not single quotes '. Your response uses single quotes, so it is not valid JSON.
As in many other languages, you can use \" to put a double quote in a string literal:
val response: String = "{\"success\":true,\"message\":\"\",\"result\":\"I'm a result\"}"
change to Data Class instead of Class
example from your code:
data class bitt(val result: String = "") {
val someVal: String = "string"
fun method() {
print("something")
}
}
I guess it takes long time before you get the result back
so the ticker still remain null
you can use kotlin coroutines to handle it.
or simply use callback like this
data class bitt(val result: String = "") {
val someVal: String = "string"
fun method() {
print("something")
}
}
fun getTicker(response: String, onComplete: (bitt) -> Unit) {
val ticker = Gson().fromJson(response, bitt::class.java)
onComplete(ticker)
}
val response: String = "{'success':true,'message':'','result':'Im a sult'}"
println(response)
getTicker(response){ println(it) }
then you might need to use Coroutine
https://github.com/Kotlin/kotlinx.coroutines
data class bitt(val result: String = "") {
val someVal: String = "string"
fun method() {
print("something")
}
}
suspend fun getTicker(response: String) = Gson().fromJson(response, bitt::class.java)
fun yourMethod() {
val response: String = "{'success':true,'message':'','result':'Im a sult'}"
println(response)
CoroutineScope(IO).launch {
val ticker = getTicker(response)
println(ticker)
}
}
KotlinConf 2017 - Introduction to Coroutines by Roman Elizarov

Moshi/Kotlin - How to serialize NULL JSON strings into empty strings instead?

I'm trying to write a null-safe String adapter that will serialize this JSON {"nullString": null} into this: Model(nullString = "") so that any JSON with a 'null' value that I expect to be a String will be replaced with "" (assuming there exists a data class like this: data class Model(val nullString: String))
I wrote a custom adapter to try and handle this:
class NullStringAdapter: JsonAdapter<String>() {
#FromJson
override fun fromJson(reader: JsonReader?): String {
if (reader == null) {
return ""
}
return if (reader.peek() == NULL) "" else reader.nextString()
}
#ToJson
override fun toJson(writer: JsonWriter?, value: String?) {
writer?.value(value)
}
}
...in an attempt to solve this parsing error:
com.squareup.moshi.JsonDataException: Expected a name but was NULL at path $.nullString
Moshi parsing code:
val json = "{\"nullString\": null}"
val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.add(NullStringAdapter())
.build()
val result = moshi.adapter(Model::class.java).fromJson(configStr)
What am I missing here? Still new to moshi so any help is appreciated!
The immediate problem is the missing reader.nextNull() call to consume the null value.
There are a couple other cleanup things you can do here, too.
With #FromJson, implementing JsonAdapter is unnecessary.
Also, the JsonReader and JsonWriter are not nullable.
object NULL_TO_EMPTY_STRING_ADAPTER {
#FromJson fun fromJson(reader: JsonReader): String {
if (reader.peek() != JsonReader.Token.NULL) {
return reader.nextString()
}
reader.nextNull<Unit>()
return ""
}
}
and use add the adapter:
val moshi = Moshi.Builder()
.add(NULL_TO_EMPTY_STRING_ADAPTER)
.add(KotlinJsonAdapterFactory())
.build()