Jacksonsox
Jacksonsox

Reputation: 1233

Kotlin gson.fromJSON doesn't allow initialization at declaration

Using Kotlin:

I have a class with different variable names than my JSON field names. When I call gson.fromJSON(string, Array<MyClass>::class.java, the initialization at declaration doesn't occur.

My understand from another post that declaration isn't called with constructor that only the only the constructor is called.

How, then do I easily get my JSON data into my class without calling the gson.fromJSON and the calling my class?

I'm not a very advanced developer. I'm looking for code as simple as possible that is efficient systematically.

What I want to do is:

val gson = Gson()
val seriesFirebaseList = gson.fromJson(seriesJSONArrayString, Array<Series>::class.java).toList()
class Series(Series_Unique_ID: String,
             Series_Record_Name: String,
             Series_Name: String,
             Series_From_Year: Int,
             Series_To_Year: Int?,
             Is_Active: Int,
             Logo_Image_Name: String,
             Sort_Order: Int) {

    val uniqueId: String = Series_Unique_ID
    val recordName: String = Series_Record_Name
    val seriesName: String = Series_Name
    val fromYear: Int = Series_From_Year
    var toYear: Int? = Series_To_Year
    var isActive: Boolean = Is_Active == 1
    val logoImageName: String = Logo_Image_Name
    val sortOrder: Int = Sort_Order

    // generated data
    var imageFirebaseURLString: String? = null
    var isFavoriteSeries = false
}

But, all the variables end up null or default value, not the JSON values.

The only thing I can get to work which seems really inefficient is to call the JSON firebase class then convert that to my class

        val gson = Gson()
        // create seriesFirebase
        val seriesFirebaseList = gson.fromJson(seriesJSONArrayString, Array<SeriesFirebase>::class.java).toList()
        val seriesList = mutableListOf<Series>()
        seriesFirebaseList.forEach { seriesFirebase ->
            val series = Series(seriesFirebase)
            seriesList.add(series)
        }
// series models
class Series(seriesFirebase: SeriesFirebase) {

    val uniqueId: String = seriesFirebase.Series_Unique_ID
    val recordName: String = seriesFirebase.Series_Record_Name
    val seriesName: String = seriesFirebase.Series_Name
    val fromYear: Int = seriesFirebase.Series_From_Year
    var toYear: Int? = seriesFirebase.Series_To_Year
    var isActive: Boolean = seriesFirebase.Is_Active == 1
    val logoImageName: String = seriesFirebase.Logo_Image_Name
    val sortOrder: Int = seriesFirebase.Sort_Order

    // generated data
    var imageFirebaseURLString: String? = null
    var isFavoriteSeries = false
}

// used to convert Gson/JSON
class SeriesFirebase(val Series_Unique_ID: String,
                     val Series_Record_Name: String,
                     val Series_Name: String,
                     val Series_From_Year: Int,
                     val Series_To_Year: Int?,
                     val Is_Active: Int,
                     val Logo_Image_Name: String,
                     val Sort_Order: Int) {

    constructor() : this(
        "",
        "",
        "",
        0,
        2150,
        0,
        "",
        0
    )

    override fun toString(): String {
        return ("series: uniqueId: " + Series_Unique_ID
                + "; recordName: " + Series_Name
                + "; name: " + Series_Name
                + "; fromYear: " + Series_From_Year
                + "; toYear: " + Series_To_Year
                + "; isActive: " + Is_Active
                + "; logoImageName: " + Logo_Image_Name
                + "; sortOrder: " + Sort_Order)
    }
}

Upvotes: 0

Views: 464

Answers (1)

I.G.
I.G.

Reputation: 370

You can mark fields of Series class with @SerializedName. E.g.

  @SerializedName("Series_Record_Name")
  val recordName: String

Upvotes: 1

Related Questions