question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Computed property for serialization

See original GitHub issue

Hi 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:closed
  • Created 3 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
shanshincommented, Dec 10, 2020

@shanshin Does it make sense to use @SerialName on a @Transient property?

No. Because @Transient properties are not serialized SerialName does not affect encoding/decoding. In the example above they are given as an example for clarity only.

1reaction
shanshincommented, Dec 3, 2020

If the names of JSON attributes is fixed you can redefine the serial name of properties like this

@Serializable
data class Record(val id: String,
                  val sourceUrl: String,
                  @SerialName("index")
                  private val index_: Int? = null,
                  @SerialName("name")
                  private val name_: String? = null) {

    @Transient
    @SerialName("index_public")
    val index = index_ ?: indexFromUri(Uri.parse(sourceUrl))

    @Transient
    @SerialName("name_public")
    val name = name_ ?: nameFromUri(Uri.parse(sourceUrl))
} 

If you using equals/hashCode you should also override these functions to use index and name not index_, name_ properties, same with toString if necessary.

Another workaround is to create a custom KSerializer that will implement these intermediate computations.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found