Initial commit
This commit is contained in:
95
json/build.gradle.kts
Normal file
95
json/build.gradle.kts
Normal file
@@ -0,0 +1,95 @@
|
||||
import java.util.*
|
||||
|
||||
plugins {
|
||||
alias(libs.plugins.dokka)
|
||||
alias(libs.plugins.kotlin.jvm)
|
||||
alias(libs.plugins.maven.publish)
|
||||
}
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(libs.kotlinx.serialization.json)
|
||||
implementation(libs.mongo.bson)
|
||||
}
|
||||
|
||||
kotlin {
|
||||
jvmToolchain(11)
|
||||
}
|
||||
|
||||
val javadocJar by tasks.register<Jar>("dokkaJavadocJar") {
|
||||
dependsOn(tasks.dokkaJavadoc)
|
||||
from(tasks.dokkaJavadoc.flatMap { it.outputDirectory })
|
||||
archiveClassifier.set("javadoc")
|
||||
}
|
||||
|
||||
val sourcesJar by tasks.register<Jar>("sourcesJar") {
|
||||
from(sourceSets.main.get().allSource)
|
||||
archiveClassifier.set("sources")
|
||||
}
|
||||
|
||||
publishing {
|
||||
publications {
|
||||
create<MavenPublication>("maven") {
|
||||
groupId = rootProject.group.toString()
|
||||
artifactId = "bson-json"
|
||||
version = rootProject.version.toString()
|
||||
from(components["java"])
|
||||
artifact(javadocJar)
|
||||
artifact(sourcesJar)
|
||||
}
|
||||
}
|
||||
repositories {
|
||||
val secretFile = rootProject.file("secret.properties")
|
||||
if (secretFile.exists()) {
|
||||
val secret = Properties()
|
||||
secretFile.inputStream().also { secret.load(it) }
|
||||
|
||||
val mavenUrl = secret["neuon.maven.url"] as? String
|
||||
val mavenUsername = secret["neuon.maven.username"] as? String
|
||||
val mavenPassword = secret["neuon.maven.password"] as? String
|
||||
|
||||
if (mavenUrl != null && mavenUsername != null && mavenPassword != null) {
|
||||
maven {
|
||||
requireNotNull(mavenUrl) {
|
||||
"neuon.maven.url is missing from secret.properties"
|
||||
}
|
||||
requireNotNull(mavenUsername) {
|
||||
"neuon.maven.username is missing from secret.properties"
|
||||
}
|
||||
requireNotNull(mavenPassword) {
|
||||
"neuon.maven.password is missing from secret.properties"
|
||||
}
|
||||
name = "neuon"
|
||||
url = uri(mavenUrl)
|
||||
|
||||
credentials(PasswordCredentials::class) {
|
||||
username = mavenUsername
|
||||
password = mavenPassword
|
||||
}
|
||||
}
|
||||
} else {
|
||||
val list = listOfNotNull(
|
||||
"neuon.maven.url".takeIf { mavenUrl == null },
|
||||
"neuon.maven.username".takeIf { mavenUsername == null },
|
||||
"neuon.maven.password".takeIf { mavenPassword == null },
|
||||
)
|
||||
val message = buildString {
|
||||
append("Missing Maven publication in secret.properties: ")
|
||||
append(list.joinToString())
|
||||
append(". Ignore this if you don't plan on publishing this library")
|
||||
}
|
||||
|
||||
logger.error(message)
|
||||
}
|
||||
} else {
|
||||
logger.error(
|
||||
buildString {
|
||||
append("Missing secret.properties. Ignore this if you don't plan on publishing this library")
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
52
json/src/main/kotlin/ai/neuon/utility/bson/json/BsonJson.kt
Normal file
52
json/src/main/kotlin/ai/neuon/utility/bson/json/BsonJson.kt
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2025 Neuon AI
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package ai.neuon.utility.bson.json
|
||||
|
||||
import kotlinx.serialization.json.JsonArray
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import kotlinx.serialization.json.buildJsonArray
|
||||
import kotlinx.serialization.json.buildJsonObject
|
||||
import kotlinx.serialization.json.put
|
||||
import org.bson.BsonArray
|
||||
import org.bson.BsonDocument
|
||||
import org.bson.BsonDouble
|
||||
import org.bson.BsonInt32
|
||||
import org.bson.BsonInt64
|
||||
import org.bson.BsonNumber
|
||||
import org.bson.BsonObjectId
|
||||
import org.bson.BsonString
|
||||
|
||||
fun BsonArray.toJsonArray(): JsonArray = buildJsonArray {
|
||||
forEach { bson ->
|
||||
when (bson) {
|
||||
is BsonDocument -> {
|
||||
this.add(bson.toJsonObject())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun BsonDocument.toJsonObject(): JsonObject = buildJsonObject {
|
||||
forEach { key, value ->
|
||||
when (value) {
|
||||
is BsonObjectId -> this.put(key, value.value.toHexString())
|
||||
is BsonInt32 -> this.put(key, value.value)
|
||||
is BsonInt64 -> this.put(key, value.value)
|
||||
is BsonDouble -> this.put(key, value.value)
|
||||
is BsonNumber -> this.put(key, value.doubleValue())
|
||||
is BsonString -> this.put(key, value.value)
|
||||
is BsonArray -> this.put(key, value.toJsonArray())
|
||||
is BsonDocument -> this.put(key, value.toJsonObject())
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user