@@ -43,17 +43,21 @@ public void Attribute_should_set_correct_units()
4343 Microseconds = timeOnly ,
4444 Ticks = timeOnly ,
4545 Nanoseconds = timeOnly ,
46+ Document = timeOnly
4647 } ;
4748
4849 var json = testObj . ToJson ( ) ;
4950
50- var expected = "{ \" Hours\" : 13, "
51- + "\" Minutes\" : 804, "
52- + "\" Seconds\" : 48293, "
53- + "\" Milliseconds\" : 48293000, "
54- + "\" Microseconds\" : 48293000000, "
55- + "\" Ticks\" : 482930000000, "
56- + "\" Nanoseconds\" : 48293000000000 }" ;
51+ var baseString = """
52+ { "Hours" : 13, "Minutes" : 804, "Seconds" : 48293, "Milliseconds" : 48293000, "Microseconds" : 48293000000, "Ticks" : 482930000000, "Nanoseconds" : 48293000000000
53+ """ ;
54+
55+ var documentString = """
56+ { "Hour" : 13, "Minute" : 24, "Second" : 53, "Millisecond" : 0, "Ticks" : 482930000000 }
57+ """ ;
58+
59+
60+ var expected = baseString + """, "Document" : """ + documentString + " }" ;
5761 Assert . Equal ( expected , json ) ;
5862 }
5963
@@ -69,7 +73,7 @@ public void Constructor_with_no_arguments_should_return_expected_result()
6973 [ Theory ]
7074 [ ParameterAttributeData ]
7175 public void Constructor_with_representation_should_return_expected_result (
72- [ Values ( BsonType . String , BsonType . Int64 , BsonType . Int32 , BsonType . Double ) ]
76+ [ Values ( BsonType . String , BsonType . Int64 , BsonType . Int32 , BsonType . Double , BsonType . Document ) ]
7377 BsonType representation ,
7478 [ Values ( TimeOnlyUnits . Ticks , TimeOnlyUnits . Hours , TimeOnlyUnits . Minutes , TimeOnlyUnits . Seconds ,
7579 TimeOnlyUnits . Milliseconds , TimeOnlyUnits . Microseconds , TimeOnlyUnits . Nanoseconds ) ]
@@ -81,6 +85,53 @@ public void Constructor_with_representation_should_return_expected_result(
8185 subject . Units . Should ( ) . Be ( units ) ;
8286 }
8387
88+ [ Theory ]
89+ [ InlineData ( """{ "x" : { Ticks: { "$numberLong" : "307255946583" } } }""" , "08:32:05.5946583" ) ]
90+ [ InlineData ( """{ "x" : { Ticks: { "$numberLong" : "0" } } }""" , "00:00:00.0000000" ) ]
91+ [ InlineData ( """{ "x" : { Ticks: { "$numberLong" : "863999999999" } } }""" , "23:59:59.9999999" ) ]
92+ public void Deserialize_with_document_should_have_expected_result ( string json , string expectedResult )
93+ {
94+ var subject = new TimeOnlySerializer ( ) ;
95+ TestDeserialize ( subject , json , expectedResult ) ;
96+ }
97+
98+ [ Theory ]
99+ [ InlineData ( """{ "x" : { Ticks: { "$numberDouble" : "307255946583" } } }""" , "08:32:05.5946583" ) ]
100+ [ InlineData ( """{ "x" : { Ticks: { "$numberDecimal" : "307255946583" } } }""" , "08:32:05.5946583" ) ]
101+ public void Deserialize_with_document_should_be_forgiving_of_actual_numeric_type ( string json , string expectedResult )
102+ {
103+ var subject = new TimeOnlySerializer ( ) ;
104+ TestDeserialize ( subject , json , expectedResult ) ;
105+ }
106+
107+ [ Theory ]
108+ [ InlineData ( """
109+ { "x" : { Hour: { "$numberInt": 0 }, Minute: { "$numberInt": 0 }, Second: { "$numberInt": 0 },
110+ Millisecond: { "$numberInt": 0 }, Microsecond: { "$numberInt": 0 }, Nanosecond: { "$numberInt": 0 },
111+ Ticks: { "$numberDouble" : "307255946583" } } }
112+ """ , "08:32:05.5946583" ) ]
113+ public void Deserialize_with_document_should_ignore_other_time_components ( string json , string expectedResult )
114+ {
115+ var subject = new TimeOnlySerializer ( ) ;
116+ TestDeserialize ( subject , json , expectedResult ) ;
117+ }
118+
119+ [ Theory ]
120+ [ InlineData ( """{ "x" : { "Unknown": "test", Ticks: { "$numberDouble" : "307255946583" } } }""" ) ]
121+ public void Deserialize_with_document_should_throw_when_field_is_unknown ( string json )
122+ {
123+ var subject = new TimeOnlySerializer ( ) ;
124+
125+ using var reader = new JsonReader ( json ) ;
126+ reader . ReadStartDocument ( ) ;
127+ reader . ReadName ( "x" ) ;
128+ var context = BsonDeserializationContext . CreateRoot ( reader ) ;
129+
130+ var exception = Record . Exception ( ( ) => subject . Deserialize ( context ) ) ;
131+ exception . Should ( ) . BeOfType < BsonSerializationException > ( ) ;
132+ exception . Message . Should ( ) . Be ( "Invalid element: 'Unknown'." ) ;
133+ }
134+
84135 [ Theory ]
85136 [ InlineData ( """{ "x" : "08:32:05.5946583" }""" , "08:32:05.5946583" ) ]
86137 [ InlineData ( """{ "x" : "00:00:00.0000000" }""" , "00:00:00.0000000" ) ]
@@ -407,8 +458,8 @@ public void Serializer_should_be_registered()
407458 [ Theory ]
408459 [ ParameterAttributeData ]
409460 public void WithRepresentation_should_return_expected_result (
410- [ Values ( BsonType . String , BsonType . Int64 , BsonType . Int32 , BsonType . Double ) ] BsonType oldRepresentation ,
411- [ Values ( BsonType . String , BsonType . Int64 , BsonType . Int32 , BsonType . Double ) ] BsonType newRepresentation )
461+ [ Values ( BsonType . String , BsonType . Int64 , BsonType . Int32 , BsonType . Double , BsonType . Document ) ] BsonType oldRepresentation ,
462+ [ Values ( BsonType . String , BsonType . Int64 , BsonType . Int32 , BsonType . Double , BsonType . Document ) ] BsonType newRepresentation )
412463 {
413464 var subject = new TimeOnlySerializer ( oldRepresentation ) ;
414465
@@ -473,6 +524,9 @@ private class TestClass
473524
474525 [ BsonTimeOnlyOptions ( BsonType . Int64 , TimeOnlyUnits . Nanoseconds ) ]
475526 public TimeOnly Nanoseconds { get ; set ; }
527+
528+ [ BsonTimeOnlyOptions ( BsonType . Document , TimeOnlyUnits . Nanoseconds ) ]
529+ public TimeOnly Document { get ; set ; }
476530 }
477531 }
478532#endif
0 commit comments