I upload information to the Book class.
tableName = "Books_name" data class Books( // #Embedded // val additional_imgs: MutableList<String>, val adult: Int, val author: String, val bookmark: Int, val chapters: MutableList<Chapter>? = null, val chapters_total: Int, // #Embedded // val comments: MutableList<Comment>, autoGenerate = true val id: Int? = null, val img: String, val lang: String, val last_activity: Int, val n_chapters: Int, val publisher: String, val rating: String, val s_title: String, val status: String, val t_title: String, // val team: Int, val writer: String, val year: String )
Book has a class Chapter
//#Entity(tableName = "Books_name")
data class Chapter(
val can_read: Boolean,
// autoGenerate = true
val id_glavs_list: Int,
val new: Boolean,
val status: String,
val title: String
)
But after adding this class, an error appears. I was looking for information, but everywhere they say that this is corrected by cleaning the project. But it does not help. EROR:
Entities and Pojos must have a usable public constructor. You can have
an empty constructor or a constructor whose parameters match the
fields (by name and type). - java.util.List
Related
I am trying to implement a custom deserializer and when I try to install it on the ObjectMapper it never gets invoked, but when I use it directly as an annotation on the class, it does. Can someone explain why and how I can actually install it on the object mapper itself?
This is never invoked
val bug = ObjectMapper().registerModule(KotlinModule.Builder().build())
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.registerModule(SimpleModule().addDeserializer(Bug::class.java, BugDeserializer()))
.readValue(bugStream, Bug::class.java)
data class Bug(
#JsonProperty("rptno")
val id: Long,
#JsonProperty("status")
#JsonDeserialize(using = StatusDeserializer::class)
val status: Status,
#JsonProperty("reported_date")
val reportedDate:Instant,
#JsonProperty("updated_date")
val updatedDate: Instant,
// val pillar: String = "",
#JsonProperty("product_id")
#JsonDeserialize(using = ProductDeserializer::class)
val product: Product,
#JsonProperty("assignee")
val assignee: String,
// val serviceRequests: List<Long> = listOf(),
#JsonProperty("subject")
val title: String,
#JsonProperty("bug_type")
val type: String
)
But, this does:
val bug = ObjectMapper().registerModule(KotlinModule.Builder().build())
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.readValue(bugStream, Bug::class.java)
#JsonDeserialize(using = BugDeserializer::class)
data class Bug(
#JsonProperty("rptno")
val id: Long,
#JsonProperty("status")
#JsonDeserialize(using = StatusDeserializer::class)
val status: Status,
#JsonProperty("reported_date")
val reportedDate:Instant,
#JsonProperty("updated_date")
val updatedDate: Instant,
// val pillar: String = "",
#JsonProperty("product_id")
#JsonDeserialize(using = ProductDeserializer::class)
val product: Product,
#JsonProperty("assignee")
val assignee: String,
// val serviceRequests: List<Long> = listOf(),
#JsonProperty("subject")
val title: String,
#JsonProperty("bug_type")
val type: String
)
```kotlin
I have a Kotlin data class that I want to map to some Json - common as rain, by itself. What I'd like to do is represent each field as a JsonPath expression in the #JsonAttribute field because the result I get is an array with 1 single map in it - something like this:
data class Bug(
#JsonProperty("\$.items[0].rptno")
val id: Long,
val status: Int,
#JsonProperty("\$.items[0].updated_date")
val reportedDate: Instant, // fields.created
#JsonProperty("updated_date")
val updatedDate: Instant,
val pillar: String, // from pi file as pillar
#JsonProperty("\$.items[0].product_id")
val productId: Int,
#JsonProperty("\$.items[0].assignee")
val assignee: String,
val serviceRequests: List<Long> = listOf(),
#JsonProperty("\$.items[0].subject")
val title: String,
#JsonProperty("\$.items[0].bug_type")
val type: String,
)
I have the following classes
class A(
val value1: String,
val value2: String,
val value3: String,
val value4: String,
val value5: String,
)
class B(
val value1: String,
val value2: String,
val value3: String,
val value4: String,
val value5: String,
) {
compaion object {
from(a: A) = B(
value1 = a.value1,
value2 = a.value2,
value3 = a.value3,
value4 = a.value4,
value5 = a.value5,
)
}
}
I write codes as follows when I want to create an instance of B from A
val a: A = getAFromSomewhere()
val b: B = B.from(a)
I have a lot of codes as above and It's very boring for me to write the factory method, 'from'. Is there any easy way of writing this kind of codes in Kotlin??
You might be interested in the MapStruct library.
https://mapstruct.org/
It helps map between two objects(DTO, Entity, etc..).
Code
In this example we want to map between a Person (Model) and a PersonDto (DTO).
data class Person(var firstName: String?, var lastName: String?, var phoneNumber: String?, var birthdate: LocalDate?)
data class PersonDto(var firstName: String?, var lastName: String?, var phone: String?, var birthdate: LocalDate?)
The MapStruct converter:
#Mapper
interface PersonConverter {
#Mapping(source = "phoneNumber", target = "phone")
fun convertToDto(person: Person) : PersonDto
#InheritInverseConfiguration
fun convertToModel(personDto: PersonDto) : Person
}
Usage:
val converter = Mappers.getMapper(PersonConverter::class.java) // or PersonConverterImpl()
val person = Person("Samuel", "Jackson", "0123 334466", LocalDate.of(1948, 12, 21))
val personDto = converter.convertToDto(person)
println(personDto)
val personModel = converter.convertToModel(personDto)
println(personModel)
From: https://github.com/mapstruct/mapstruct-examples/tree/master/mapstruct-kotlin
I have a json with this kind of key:
...
"metaData": {
"date:create": "2019-11-13t15:42:02+01:00",
"date:modify": "2019-11-13t15:42:02+01:00",
"exif:ColorSpace": "1",
"exif:ExifImageLength": "1500",
"exif:ExifImageWidth": "1125",
"exif:ExifOffset": "38",
"exif:Orientation": "1",
"jpeg:colorspace": "2",
"jpeg:sampling-factor": "1x1,1x1,1x1"
},
...
All the keys in the metaData object are dynamic (not really but there's hundred of different keys) how can i serialize this object without having to create a class with all the possible keys.
This is my work:
#Serializable
data class Image(
val name: String? = null,
val uid: String,
val createdAt: String? = null,
val updatedAt: String? = null,
val metaData: MetaData? = null,
)
#Serializable
data class MetaData (
#SerialName("date:create")
val dateCreate: String,
#SerialName("date:modify")
val dateModify: String,
#SerialName("exif:ColorSpace")
val exifColorSpace: String,
#SerialName("exif:ExifImageLength")
val exifExifImageLength: String,
#SerialName("exif:ExifImageWidth")
val exifExifImageWidth: String,
#SerialName("exif:ExifOffset")
val exifExifOffset: String,
#SerialName("exif:Orientation")
val exifOrientation: String,
#SerialName("jpeg:colorspace")
val jpegColorspace: String,
#SerialName("jpeg:sampling-factor")
val jpegSamplingFactor: String
)
You can use a JSONObject to serialize dynamically the metadata. Your Metadata class is useless with this solution, your Image instance will have a JSONObject as metadata property :
data class Image(
val name: String? = null,
val uid: String,
val createdAt: String? = null,
val updatedAt: String? = null,
val metaData: JSONObject? = null,
)
This question already has answers here:
Gson Deserialization with Kotlin, Initializer block not called
(2 answers)
Closed 4 years ago.
I've a simple data class of User
data class User(#PrimaryKey(autoGenerate = true) val id: Long, val _id: String, val name: String, val about: String,
val phoneNumber: String, val token: String,
val lastLogin: String, val avatarUrl: String, #Embedded val location: Location,
val emailId: String, val gender: Boolean, val timestamp: Long = System.currentTimeMillis())
As you can see the last parameter is val timestamp: Long = System.currentTimeMillis()
the response comes from network using retrofit and parsed using GSON
timestamp does not comes in a response json it's just extra field I need to do some logic. The problem is that value is always 0. It should be the current time stamp
This will do the trick,
data class User(#PrimaryKey(autoGenerate = true) val id: Long, val _id: String, val name: String, val about: String,
val phoneNumber: String, val token: String,
val lastLogin: String, val avatarUrl: String, #Embedded val location: Location,
val emailId: String, val gender: Boolean) {
var timestamp: Long = System.currentTimeMillis()
get() = if(field > 0) field else {
field = System.currentTimeMillis()
field
}
}