I have an object that I wrote to a file and now I want to take that string I get from the file to turn it back into an object for me to use.
Object class:
@Serializable
class DrinkItem {
@SerializedName("strAlcoholic")
val alcoholic: String? = null
@SerializedName("strIngredient1")
val ingredient1: String? = null
@SerializedName("strIngredient10")
val ingredient10: String? = null
@SerializedName("strIngredient11")
val ingredient11: String? = null
@SerializedName("strIngredient12")
val ingredient12: String? = null
@SerializedName("strIngredient13")
val ingredient13: String? = null
@SerializedName("strIngredient14")
val ingredient14: String? = null
@SerializedName("strIngredient15")
val ingredient15: String? = null
@SerializedName("strIngredient2")
val ingredient2: String? = null
@SerializedName("strIngredient3")
val ingredient3: String? = null
@SerializedName("strIngredient4")
val ingredient4: String? = null
@SerializedName("strIngredient5")
val ingredient5: String? = null
@SerializedName("strIngredient6")
val ingredient6: String? = null
@SerializedName("strIngredient7")
val ingredient7: String? = null
@SerializedName("strIngredient8")
val ingredient8: String? = null
@SerializedName("strIngredient9")
val ingredient9: String? = null
@SerializedName("strInstructions")
val instructions: String? = null
@SerializedName("strMeasure1")
val measurement1: String? = null
@SerializedName("strMeasure10")
val measurement10: String? = null
@SerializedName("strMeasure11")
val measurement11: String? = null
@SerializedName("strMeasure12")
val measurement12: String? = null
@SerializedName("strMeasure13")
val measurement13: String? = null
@SerializedName("strMeasure14")
val measurement14: String? = null
@SerializedName("strMeasure15")
val measurement15: String? = null
@SerializedName("strMeasure2")
val measurement2: String? = null
@SerializedName("strMeasure3")
val measurement3: String? = null
@SerializedName("strMeasure4")
val measurement4: String? = null
@SerializedName("strMeasure5")
val measurement5: String? = null
@SerializedName("strMeasure6")
val measurement6: String? = null
@SerializedName("strMeasure7")
val measurement7: String? = null
@SerializedName("strMeasure8")
val measurement8: String? = null
@SerializedName("strMeasure9")
val measurement9: String? = null
@SerializedName("strDrink")
val name: String? = null
@SerializedName("strDrinkThumb")
val thumbnail: String? = null
}
I use this function to write the object to a file:
private fun writeToFile(fileName: String, byteArray: ByteArray){
val lineSeparator: String = System.getProperty("line.separator") as String
// File
val path = context!!.filesDir
val directory = File(path, "LET")
directory.mkdirs()
val file = File(directory, fileName)
//append drink to file
FileOutputStream(file, true).use {
it.write(byteArray)
it.write(lineSeparator.toByteArray())
}
}
After the function is done with the object it is turned into this string:
{"alcoholic":"Alcoholic","ingredient1":"Apricot brandy","ingredient2":"Triple sec","ingredient3":"Lime","ingredient4":"Lime","instructions":"Shake all ingredients (except lime wedge) with ice and strain into a cocktail glass. Add the wedge of lime and serve.","measurement1":"1 oz ","measurement2":"1 oz ","measurement3":"Juice of 1 ","measurement4":"1 ","name":"After Dinner Cocktail","thumbnail":"https://www.thecocktaildb.com/images/media/drink/vtytxq1483387578.jpg"}
Is there a function or library that would help me turn a string of an object into said object?
CodePudding user response:
Are you using library to convert your DrinkItem
to the outputting JSON String? Seems that the keys in your JSON String do not match with what you have named in @SerializedName()
.
You can use the Gson library to handle JSON String and Object conversion.
For example, if you have the same DrinkItem
class, you can convert your DrinkItem
into JSON String as following:
// Define DrinkItem and set some of the attributes
val drinkItemTest = DrinkItem()
drinkItemTest.alcoholic = "Alcohol One"
drinkItemTest.ingredient1 = "Ingredient One"
// Use Gson library to convert Object to JSON String
val drinkItemTestString = Gson().toJson(drinkItemTest)
println(drinkItemTestString)
Output
{"strAlcoholic":"Alcohol One","strIngredient1":"Ingredient One"}
And to convert your JSON String back to DrinkItem
, you can do something like this:
// Read the whole JSON String from your file here
val drinkItemString = "{\"strAlcoholic\":\"Alcohol One\",\"strIngredient1\":\"Ingredient One\"}"
// And make use of Gson library to convert your JSON String into DrinkItem Object
val drinkItem = Gson().fromJson(drinkItemString, DrinkItem::class.java)