11package com .sfxcode .nosql .mongo .bson
22
33import java .math .BigInteger
4- import java .time .{LocalDate , LocalDateTime , ZoneId }
4+ import java .time .{ LocalDate , LocalDateTime , ZoneId }
55import java .util .Date
66
77import org .mongodb .scala .Document
@@ -22,32 +22,31 @@ object BsonConverter {
2222 case option : Option [Any ] =>
2323 if (option.isDefined) {
2424 toBson(option.get)
25- }
26- else {
25+ } else {
2726 BsonNull ()
2827 }
2928 case v : Any if converterPlugin.hasCustomClass(v) =>
3029 converterPlugin.toBson(v)
31- case b : Boolean => BsonBoolean (b)
32- case s : String => BsonString (s)
33- case c : Char => BsonString (c.toString)
30+ case b : Boolean => BsonBoolean (b)
31+ case s : String => BsonString (s)
32+ case c : Char => BsonString (c.toString)
3433 case bytes : Array [Byte ] => BsonBinary (bytes)
35- case r : Regex => BsonRegularExpression (r)
36- case d : Date => BsonDateTime (d)
34+ case r : Regex => BsonRegularExpression (r)
35+ case d : Date => BsonDateTime (d)
3736 case ld : LocalDate =>
3837 BsonDateTime (Date .from(ld.atStartOfDay(ZoneId .systemDefault()).toInstant))
3938 case ldt : LocalDateTime =>
4039 BsonDateTime (Date .from(ldt.atZone(ZoneId .systemDefault()).toInstant))
41- case oid : ObjectId => BsonObjectId (oid)
42- case i : Int => BsonInt32 (i)
43- case l : Long => BsonInt64 (l)
44- case bi : BigInt => BsonInt64 (bi.toLong)
45- case bi : BigInteger => BsonInt64 (bi.longValue())
46- case d : Double => BsonDouble (d)
47- case f : Float => BsonDouble (f)
48- case bd : BigDecimal => BsonDecimal128 .apply(bd)
40+ case oid : ObjectId => BsonObjectId (oid)
41+ case i : Int => BsonInt32 (i)
42+ case l : Long => BsonInt64 (l)
43+ case bi : BigInt => BsonInt64 (bi.toLong)
44+ case bi : BigInteger => BsonInt64 (bi.longValue())
45+ case d : Double => BsonDouble (d)
46+ case f : Float => BsonDouble (f)
47+ case bd : BigDecimal => BsonDecimal128 .apply(bd)
4948 case bd : java.math.BigDecimal => BsonDecimal128 .apply(bd)
50- case doc : Document => BsonDocument (doc)
49+ case doc : Document => BsonDocument (doc)
5150 case map : scala.collection.Map [_, _] =>
5251 var doc = Document ()
5352 map.keys.foreach { key =>
@@ -77,22 +76,22 @@ object BsonConverter {
7776 def fromBson (value : BsonValue ): Any =
7877 value match {
7978
80- case b : BsonBoolean => b.getValue
81- case s : BsonString => s.getValue
82- case bytes : BsonBinary => bytes.getData
79+ case b : BsonBoolean => b.getValue
80+ case s : BsonString => s.getValue
81+ case bytes : BsonBinary => bytes.getData
8382 case r : BsonRegularExpression => r.getPattern
84- case d : BsonDateTime => new Date (d.getValue)
85- case d : BsonTimestamp => new Date (d.getTime)
86- case oid : BsonObjectId => oid.getValue
87- case i : BsonInt32 => i.getValue
88- case l : BsonInt64 => l.getValue
89- case d : BsonDouble => d.doubleValue()
90- case d : BsonDecimal128 => d.getValue.bigDecimalValue()
91- case doc : BsonDocument => Document (doc)
83+ case d : BsonDateTime => new Date (d.getValue)
84+ case d : BsonTimestamp => new Date (d.getTime)
85+ case oid : BsonObjectId => oid.getValue
86+ case i : BsonInt32 => i.getValue
87+ case l : BsonInt64 => l.getValue
88+ case d : BsonDouble => d.doubleValue()
89+ case d : BsonDecimal128 => d.getValue.bigDecimalValue()
90+ case doc : BsonDocument => Document (doc)
9291 case array : BsonArray =>
9392 array.getValues.asScala.toList.map(v => fromBson(v))
9493 case n : BsonNull => null
95- case _ => value
94+ case _ => value
9695 }
9796
9897 def asMap (document : Document ): Map [String , Any ] = {
0 commit comments