Created
November 30, 2019 09:32
-
-
Save toantran-ea/96b890bf968541ba566c7469794427ba to your computer and use it in GitHub Desktop.
Data model for Food Intake Entry
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo.data | |
import kotlinx.serialization.Serializable | |
import kotlinx.serialization.json.Json | |
import kotlinx.serialization.list | |
import kotlinx.serialization.parse | |
@Serializable | |
data class FoodEntry( | |
val id: Long, | |
val name: String, | |
val time: Long = System.currentTimeMillis(), | |
val description: String, | |
val energyCount: Long = 0, | |
val updated: Long = System.currentTimeMillis() | |
) | |
fun FoodEntry.toJsonString(): String { | |
return Json.stringify(FoodEntry.serializer(), this) | |
} | |
fun List<FoodEntry>.toJsonString(): String { | |
return Json.stringify(FoodEntry.serializer().list, this) | |
} | |
fun fromJsonStringToObject(input: String): FoodEntry { | |
return Json.parse(FoodEntry.serializer(), input) | |
} | |
fun fromJsonStringToList(input: String): List<FoodEntry> { | |
return Json.parse(FoodEntry.serializer().list, input) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment