Computed property for serialization
See original GitHub issueHi there, Is there any way to make computed property for serialization?
The code down below produces a compilation error: e: org.jetbrains.kotlin.codegen.CompilationException: Back-end (JVM) Internal error: Failed to generate expression: KtNameReferenceExpression
but I expected it works properly:
@Serializable
data class Record(val id: String,
val sourceUrl: String,
val index: Int = indexFromUri(Uri.parse(sourceUrl)),
val name: String = nameFromUri(Uri.parse(sourceUrl))) {
}
fun indexFromUri(uri: Uri): Int {
val lastComponent = uri.lastPathSegment?.substringAfterLast('/')
return lastComponent?.take(2)?.toIntOrNull() ?: 0
}
fun nameFromUri(uri: Uri): String {
val lastComponent = uri.lastPathSegment?.substringAfterLast('/')
return lastComponent?.substring(3) ?: "No name"
}
Kotlin: 1.4.20 kotlinx-serialization-json: 1.0.1
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (6 by maintainers)
Top Results From Across the Web
how to serialize computed properties? #1334 - mobxjs/mobx
I have a computed property in my grid component (which is an observer also), which returns the calculated column properties array.
Read more >Serialize computed property Carbon instance - Laracasts
Hi, my model offers a computed property called "departureTime"; if a departure time is available, it will be returned as a Carbon instance....
Read more >Serializing models - AdonisJS
Computed properties During the serialization process, the model returns an object with properties using the @column decorator. If you want to serialize any ......
Read more >Should I expose a "computed" value as a property or a method?
Just expose it publicly. Knerd makes a good point: properties can be serialized. You would never deserialize from the HTML, so it doesn't...
Read more >Model computed property to json - Ember.JS
Hi acero, I guess you could override serialize in your model-specific or application serializer. ... Hi emberigniter, thank you for idea I will...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
No. Because
@Transient
properties are not serializedSerialName
does not affect encoding/decoding. In the example above they are given as an example for clarity only.If the names of JSON attributes is fixed you can redefine the serial name of properties like this
If you using equals/hashCode you should also override these functions to use
index
andname
notindex_
,name_
properties, same withtoString
if necessary.Another workaround is to create a custom
KSerializer
that will implement these intermediate computations.