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,31 +22,32 @@ object BsonConverter {
2222 case option : Option [Any ] =>
2323 if (option.isDefined) {
2424 toBson(option.get)
25- } else {
25+ }
26+ else {
2627 BsonNull ()
2728 }
2829 case v : Any if converterPlugin.hasCustomClass(v) =>
2930 converterPlugin.toBson(v)
30- case b : Boolean => BsonBoolean (b)
31- case s : String => BsonString (s)
32- case c : Char => BsonString (c.toString)
31+ case b : Boolean => BsonBoolean (b)
32+ case s : String => BsonString (s)
33+ case c : Char => BsonString (c.toString)
3334 case bytes : Array [Byte ] => BsonBinary (bytes)
34- case r : Regex => BsonRegularExpression (r)
35- case d : Date => BsonDateTime (d)
35+ case r : Regex => BsonRegularExpression (r)
36+ case d : Date => BsonDateTime (d)
3637 case ld : LocalDate =>
3738 BsonDateTime (Date .from(ld.atStartOfDay(ZoneId .systemDefault()).toInstant))
3839 case ldt : LocalDateTime =>
3940 BsonDateTime (Date .from(ldt.atZone(ZoneId .systemDefault()).toInstant))
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)
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)
4849 case bd : java.math.BigDecimal => BsonDecimal128 .apply(bd)
49- case doc : Document => BsonDocument (doc)
50+ case doc : Document => BsonDocument (doc)
5051 case map : scala.collection.Map [_, _] =>
5152 var doc = Document ()
5253 map.keys.foreach { key =>
@@ -76,22 +77,22 @@ object BsonConverter {
7677 def fromBson (value : BsonValue ): Any =
7778 value match {
7879
79- case b : BsonBoolean => b.getValue
80- case s : BsonString => s.getValue
81- case bytes : BsonBinary => bytes.getData
80+ case b : BsonBoolean => b.getValue
81+ case s : BsonString => s.getValue
82+ case bytes : BsonBinary => bytes.getData
8283 case r : BsonRegularExpression => r.getPattern
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)
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)
9192 case array : BsonArray =>
9293 array.getValues.asScala.toList.map(v => fromBson(v))
9394 case n : BsonNull => null
94- case _ => value
95+ case _ => value
9596 }
9697
9798 def asMap (document : Document ): Map [String , Any ] = {
0 commit comments