update(BsonJson): add date and boolean support

This commit is contained in:
2026-09-10 18:04:22 +08:00
parent f907f89ca2
commit bbf351f169
2 changed files with 10 additions and 1 deletions

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 -> {}
}
}
}