11package com .avsystem .commons
22package mongo
33
4+ import com .avsystem .commons .serialization .json .{JsonStringInput , JsonStringOutput }
45import com .avsystem .commons .serialization .{GenCodec , HasGenCodecWithDeps }
6+ import org .bson ._
57import org .bson .json .JsonReader
68import org .bson .types .{Decimal128 , ObjectId }
7- import org .bson . _
9+ import org .scalactic . source . Position
810import org .scalatest .funsuite .AnyFunSuite
911
1012case class AllTypesInABag (
@@ -23,7 +25,7 @@ case class AllTypesInABag(
2325)
2426object AllTypesInABag extends HasGenCodecWithDeps [BsonGenCodecs .type , AllTypesInABag ]
2527
26- class BsonValueCodecsTest extends AnyFunSuite {
28+ class BsonValueCodecsTest extends AnyFunSuite with BsonGenCodecs {
2729 test(" codec roundtrip" ) {
2830 val doc = new BsonDocument (JList (
2931 new BsonElement (" someInt64" , new BsonInt64 (64 )),
@@ -69,8 +71,6 @@ class BsonValueCodecsTest extends AnyFunSuite {
6971 }
7072
7173 test(" null handling" ) {
72- import BsonGenCodecs .bsonDocumentCodec
73-
7474 val reader = new JsonReader (""" {"key": null}""" )
7575 val input = new BsonReaderInput (reader)
7676 val document = GenCodec .read[BsonDocument ](input)
@@ -83,11 +83,22 @@ class BsonValueCodecsTest extends AnyFunSuite {
8383 }
8484
8585 test(" null in doc in array" ) {
86- import BsonGenCodecs .bsonArrayCodec
87-
8886 val reader = new JsonReader (""" [{"key": null}]""" )
8987 val input = new BsonReaderInput (reader)
9088 val array = GenCodec .read[BsonArray ](input)
9189 assert(array === new BsonArray (JList (new BsonDocument (" key" , BsonNull .VALUE ))))
9290 }
91+
92+ def testJsonRoundtrip [T : GenCodec ](value : T )(implicit pos : Position ): Unit = {
93+ val json = JsonStringOutput .write(value)
94+ val readValue = JsonStringInput .read[T ](json)
95+ assert(value == readValue)
96+ }
97+
98+ test(" JSON string roundtrip for BsonValue" ) {
99+ testJsonRoundtrip(new BsonBoolean (true ))
100+ testJsonRoundtrip(new BsonInt32 (42 ))
101+ testJsonRoundtrip(new BsonInt64 (42 ))
102+ testJsonRoundtrip(new BsonDocument (" v" , new BsonInt32 (42 )))
103+ }
93104}
0 commit comments