Handle BsonDateTime and BsonBoolean in toJsonObject conversion #4

Merged
chanteckwei merged 4 commits from updates/add-date-and-boolean-support into main 2026-09-11 15:59:38 +08:00
3 changed files with 11 additions and 2 deletions

2
.idea/vcs.xml generated
View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
<mapping directory="" vcs="Git" />
</component>
</project>

View File

@@ -3,4 +3,4 @@ plugins {
}
group = "ai.neuon.utility"
version = "0.1.1"
version = "0.1.2"

View File

@@ -10,6 +10,8 @@
* 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.
*/
@file:OptIn(ExperimentalTime::class)
package ai.neuon.utility.bson.json
import kotlinx.serialization.json.JsonArray
@@ -18,6 +20,8 @@ import kotlinx.serialization.json.buildJsonArray
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.put
import org.bson.BsonArray
import org.bson.BsonBoolean
import org.bson.BsonDateTime
import org.bson.BsonDocument
import org.bson.BsonDouble
import org.bson.BsonInt32
@@ -25,6 +29,8 @@ import org.bson.BsonInt64
import org.bson.BsonNumber
import org.bson.BsonObjectId
import org.bson.BsonString
import kotlin.time.ExperimentalTime
import kotlin.time.Instant
fun BsonArray.toJsonArray(): JsonArray = buildJsonArray {
forEach { bson ->
@@ -45,8 +51,11 @@ fun BsonDocument.toJsonObject(): JsonObject = buildJsonObject {
is BsonDouble -> this.put(key, value.value)
is BsonNumber -> this.put(key, value.doubleValue())
is BsonString -> this.put(key, value.value)
is BsonBoolean -> this.put(key, value.value)
is BsonDateTime -> this.put(key, Instant.fromEpochMilliseconds(value.value).toString())
is BsonArray -> this.put(key, value.toJsonArray())
is BsonDocument -> this.put(key, value.toJsonObject())
else -> {}
}
}
}