An open API service indexing awesome lists of open source software.

https://github.com/rtmigo/repr_kt

Kotlin/JVM library converting Kotlin objects to strings that are Kotlin code too
https://github.com/rtmigo/repr_kt

code-generation jvm kotlin library python serialization

Last synced: 10 months ago
JSON representation

Kotlin/JVM library converting Kotlin objects to strings that are Kotlin code too

Awesome Lists containing this project

README

          

![Generic badge](https://img.shields.io/badge/JVM-8+-blue.svg)
[![Maven Central](https://img.shields.io/maven-central/v/io.github.rtmigo/repr.svg)](https://search.maven.org/artifact/io.github.rtmigo/repr)

# [repr](https://github.com/rtmigo/repr_kt#readme)

Kotlin/JVM library. Converts Kotlin objects to strings.

`.toRepr()` is like `.toString()`, but aims to produce a valid Kotlin code.

Let's say we have the following:

```kotlin
data class Planet(val name: String,
val diameter: Int)

val data = mapOf(
"planets" to listOf(
Planet("Venus", 12104),
Planet("Earth", 12742),
Planet("Mars", 6779)
),
"location" to "Solar System"
)
```

Calling `data.toString()` would give us this:

```text
{
planets=[
Planet(name=Venus, diameter=12104),
Planet(name=Earth, diameter=12742),
Planet(name=Mars, diameter=6779)
],
location=Solar System
}
```

Calling `data.toRepr()` would give us this:

```kotlin
mapOf(
"planets" to listOf(
Planet(name="Venus", diameter=12104),
Planet(name="Earth", diameter=12742),
Planet(name="Mars", diameter=6779)
),
"location" to "Solar System"
)
```

*(both examples formatted for ease of reading)*

The library uses the features of Kotlin reflection. It is inspired by Python's built-in
[`repr`]([https://docs.python.org/3/library/functions.html#repr]) function (and `__repr__`
overloads).

# Install [![Maven Central](https://img.shields.io/maven-central/v/io.github.rtmigo/repr.svg)](https://search.maven.org/artifact/io.github.rtmigo/repr)

```kotlin
// build.gradle.kts

dependencies {
implementation("io.github.rtmigo:repr:X.X.X")
// replace X.X.X with actual version
}
```

Find the latest version and instructions for other build systems
at [Maven Central](https://search.maven.org/artifact/io.github.rtmigo/repr).

# Use

```kotlin
import io.github.rtmigo.repr.toRepr

fun main() {
val data = listOf(1, 2, 3)
println(data.toRepr())
}
```

Output:

```kotlin
listOf(1, 2, 3)
```

## Customize .toRepr for your class

The `.toRepr()` extension method automatically converts `Any` objects.

Sometimes you may want to customize the way how your objects are converted. To do this,
define `toRepr(): String` method for your class.

```kotlin
import io.github.rtmigo.repr.toRepr

class TimeDefault(days: Int) {
val hours = days * 24
}

class TimeTweaked(days: Int) {
val hours = days * 24
fun toRepr() = "TimeTweaked(days=${hours / 24})"
}

fun main() {
println(listOf(TimeDefault(days = 1), TimeDefault(days = 7)).toRepr())
println(listOf(TimeTweaked(days = 1), TimeTweaked(days = 7)).toRepr())
}
```

Output:

```text
listOf(TimeDefault(hours=24), TimeDefault(hours=168))
listOf(TimeTweaked(days=1), TimeTweaked(days=7))
```
Specifying the `toRepr` method here is similar to overloading the `__repr__()` method in Python.

# License

Copyright © 2022 [Artsiom iG](https://github.com/rtmigo).
Licensed under the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0.txt).