Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialization Library #1499

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ out/
build/
classes/
**/jetbrains.db

21 changes: 21 additions & 0 deletions exposed-serialization/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
plugins {
kotlin("jvm") apply true
id("testWithDBs")
}

repositories {
mavenCentral()
}

dependencies {
api(project(":exposed-core"))
api(project(":exposed-dao"))
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2")
testImplementation("com.microsoft.sqlserver:mssql-jdbc:8.2.2.jre11")
// Before you ask, the author's work SQL server is running a very old version of MSSQL and newer drivers aren't
// compatible for whatever reason. He could probably get it working if he wanted to... but he doesn't
testImplementation(project(":exposed-dao"))
testImplementation(project(":exposed-tests"))
testImplementation("junit", "junit", "4.12")
testImplementation(kotlin("test-junit"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package org.jetbrains.exposed.serialization

import kotlinx.serialization.json.JsonNull
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.buildJsonObject
import org.jetbrains.exposed.dao.Entity
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.Table
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.transactions.transaction

object DAOSerializer {

/**
* A disgusting, performance-heavy way to create a JsonPrimitive from a generic primitive.
* @param value A primitive type such as a Number, String, or null.
*/
fun toElement(value: Any?): JsonPrimitive { // Horribly inefficient way of doing this (probably)
return if (value != null) {
val str = value.toString()
try {
JsonPrimitive(str.toInt())
} catch (notInt: NumberFormatException) {
try {
JsonPrimitive(str.toFloat())
} catch (notFloat: NumberFormatException) {
try {
JsonPrimitive(str.toBooleanStrict())
} catch (isString: IllegalArgumentException) {
JsonPrimitive(str)
}
}
}
} else JsonNull
}

/**
* Serialize a ResultRow.
* @param table The Table the row belongs to. Leave null to disable further serialization of referenced rows.
*/
fun jsonify(row: ResultRow, table: Table? = null): JsonObject {
return buildJsonObject {
row.fieldIndex.keys.forEach { field ->
val value = row[field]
val name = field.toString().substringAfterLast('.')
if (table != null) {
val refColumns = table.columns.filter { (it.name == name).and(it.referee != null) }
if (refColumns.isNotEmpty()) {
for (refCol in refColumns) {
val referee = refCol.referee!!
val referencedValue = referee.table.select {
referee eq referee.asLiteral(value)
}.first()
put(name, jsonify(referencedValue))
}
} else put(name, toElement(value))
} else put(name, toElement(value)) // Should clean this up
}
}
}

/**
* Serialize a DAO entity, including its referenced rows.
*/
fun jsonify(dao: Entity<Int>, recursion: Boolean = false): JsonObject {
val columns = dao.klass.dependsOnColumns
val values = dao.readValues
return buildJsonObject {
for (column in columns) {
val referee = column.referee
if (referee != null) {
transaction {
val referencedRow = referee.table.select {
referee eq referee.asLiteral(values[column])
}.single()
if (recursion) put(column.name, jsonify(referencedRow, referee.table))
else put(column.name, jsonify(referencedRow))
}
} else {
put(column.name, toElement(values[column]))
}
}
}
}
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ include("exposed-money")
include("exposed-bom")
include("exposed-kotlin-datetime")
include("exposed-crypt")
include("exposed-serialization")

pluginManagement {
plugins {
Expand Down