0

I need to store my schedule in a single JSON file so I can load it again to my app if needed. The schedules are stored using the following three classes:

data class DayScheduleItem(
    var hourAndMin: Calendar = Calendar.getInstance(),
): ScheduleItem() 

data class ExceptionItem(
    var fromDate: Calendar = Calendar.getInstance(),
    var toDate: Calendar = Calendar.getInstance(),
): ScheduleItem() 

data class WeekDayScheduleItem(
    var name: String,
): ScheduleItem() {
    var isOpen: Boolean = false  
}

There is then the super class:

class ScheduleItem {
    var id: Int? = null
        get() = field
        set(value) {
            field= value
        }
    var data: List<Byte> = emptyList()
        get() = field
        set(value) {
            field = value
        }
}

Now I have a list as follows:

[
    [
         DayScheduleItem1,
         DayScheduleItem2
    ],
    [
         WeekDayScheduleItem1,
         WeekDayScheduleItem2,
         WeekDayScheduleItem3,
         WeekDayScheduleItem4
    ],
    [
    ]
]

The questions are:

  1. How to parse this json using GSON.fromJson()?
  2. Do I need to simplify my classes?
  3. Do I need to use another library like Moshi?
  4. Shall I change the structure of my JSON?
msc87
  • 943
  • 3
  • 17
  • 39

1 Answers1

1

1. How to parse this json using GSON.fromJson()?

Gson does not provide polymorphism support out of the box, see related questions such as this one. Normally you would have to include some kind of type identifier in the JSON data for each schedule item and then on deserialization based on this identifier decide as which schedule item subclass to deserialize.
The Jackson library has built-in support for this and it also supports deduction-based polymorphism where it only decides based on the properties which class the data represents, without requiring a separate property in JSON, though this might be a bit more difficult to maintain when adding or removing properties in the future.

3. Do I need to use another library like Moshi?

You could achieve this with Gson, but for your use case another library might be more suitable nonetheless:

  • As mentioned above Jackson provides built-in support for polymorphism
  • Gson is mainly designed for Java and its Kotlin support is limited; Moshi and Jackson explicitly support Kotlin and might therefore be easier to use for a Kotlin project

4. Shall I change the structure of my JSON?

If for your use case the values are always grouped by schedule type in the JSON data, then you might not actually need polymorphism support. It would be simpler if you use a JSON object to group the schedule items:

{
    "day": [
        DayScheduleItem1,
        DayScheduleItem2
    ],
    "weekday": [
        WeekDayScheduleItem1,
        WeekDayScheduleItem2
    ],
    ...
}

Then you could write a data class which has all these entries as properties, e.g. val day: List<DayScheduleItem>, and serialize and deserialize that data class.

Marcono1234
  • 5,856
  • 1
  • 25
  • 43