15
15
16
16
using System ;
17
17
using System . Linq ;
18
- using MongoDB . Bson ;
19
18
using MongoDB . Bson . Serialization ;
20
19
using MongoDB . Bson . Serialization . Attributes ;
21
20
using Xunit ;
@@ -41,6 +40,19 @@ public class TestClass
41
40
public bool S ;
42
41
}
43
42
43
+ public class TestClassWithPrivate
44
+ {
45
+ [ BsonRepresentation ( BsonType . String ) ]
46
+ private bool _b ;
47
+
48
+ public TestClassWithPrivate ( bool b )
49
+ {
50
+ _b = b ;
51
+ }
52
+
53
+ public bool GetPrivateB ( ) => _b ;
54
+ }
55
+
44
56
[ Fact ]
45
57
public void TestFalse ( )
46
58
{
@@ -72,6 +84,16 @@ public void TestTrue()
72
84
var rehydrated = BsonSerializer . Deserialize < TestClass > ( bson ) ;
73
85
Assert . True ( bson . SequenceEqual ( rehydrated . ToBson ( ) ) ) ;
74
86
}
87
+
88
+ [ Fact ]
89
+ public void TestPrivateFieldWithBsonRepresentation ( )
90
+ {
91
+ var testValue = true ;
92
+ var json = $ "{{ _b : '{ testValue } ' }}";
93
+
94
+ var deserialized = BsonSerializer . Deserialize < TestClassWithPrivate > ( json ) ;
95
+ Assert . Equal ( testValue , deserialized . GetPrivateB ( ) ) ;
96
+ }
75
97
}
76
98
77
99
public class DateTimeSerializerTests
@@ -95,6 +117,19 @@ public class TestClass
95
117
public DateTime Document ;
96
118
}
97
119
120
+ public class TestClassWithPrivateField
121
+ {
122
+ [ BsonDateTimeOptions ( Representation = BsonType . String ) ]
123
+ private DateTime _d ;
124
+
125
+ public TestClassWithPrivateField ( DateTime d )
126
+ {
127
+ _d = d ;
128
+ }
129
+
130
+ public DateTime GetPrivateD ( ) => _d ;
131
+ }
132
+
98
133
private static string __expectedTemplate =
99
134
"{ 'Default' : #Default, 'Local' : #Local, 'Unspecified' : #Unspecified, 'Utc' : #Utc, 'Ticks' : #Ticks, 'String' : '#String', 'DateOnlyString' : '#DateOnlyString', 'Document' : #Document }" ;
100
135
@@ -543,6 +578,16 @@ public void TestUtc()
543
578
Assert . Equal ( DateTimeKind . Utc , rehydrated . DateOnlyString . Kind ) ;
544
579
Assert . Equal ( DateTimeKind . Utc , rehydrated . Document . Kind ) ;
545
580
}
581
+
582
+ [ Fact ]
583
+ public void TestPrivateFieldWithBsonRepresentation ( )
584
+ {
585
+ var testValue = new DateTime ( 2020 , 01 , 01 ) ;
586
+ var json = $ "{{ '_d' : '{ testValue : yyyy-MM-ddTHH:mm:ss.FFFZ} ' }}";
587
+
588
+ var deserialized = BsonSerializer . Deserialize < TestClassWithPrivateField > ( json ) ;
589
+ Assert . Equal ( testValue , deserialized . GetPrivateD ( ) ) ;
590
+ }
546
591
}
547
592
548
593
public class DoubleSerializerTests
@@ -558,6 +603,19 @@ public class TestClass
558
603
public double S ;
559
604
}
560
605
606
+ public class TestClassWithPrivateField
607
+ {
608
+ [ BsonRepresentation ( BsonType . String ) ]
609
+ private double _d ;
610
+
611
+ public TestClassWithPrivateField ( double d )
612
+ {
613
+ _d = d ;
614
+ }
615
+
616
+ public double GetPrivateD ( ) => _d ;
617
+ }
618
+
561
619
[ Fact ]
562
620
public void TestMin ( )
563
621
{
@@ -738,6 +796,16 @@ public void TestPositiveInfinity()
738
796
var rehydrated = BsonSerializer . Deserialize < TestClass > ( bson ) ;
739
797
Assert . True ( bson . SequenceEqual ( rehydrated . ToBson ( ) ) ) ;
740
798
}
799
+
800
+ [ Fact ]
801
+ public void TestPrivateFieldWithBsonRepresentation ( )
802
+ {
803
+ var testValue = 5 ;
804
+ var json = $ "{{ '_d' : '{ testValue } ' }}";
805
+
806
+ var deserialized = BsonSerializer . Deserialize < TestClassWithPrivateField > ( json ) ;
807
+ Assert . Equal ( testValue , deserialized . GetPrivateD ( ) ) ;
808
+ }
741
809
}
742
810
743
811
public class GuidSerializerTests
@@ -749,6 +817,19 @@ public class TestClass
749
817
public Guid String { get ; set ; }
750
818
}
751
819
820
+ public class TestClassWithPrivate
821
+ {
822
+ [ BsonRepresentation ( BsonType . String ) ]
823
+ private Guid _b ;
824
+
825
+ public TestClassWithPrivate ( Guid b )
826
+ {
827
+ _b = b ;
828
+ }
829
+
830
+ public Guid GetPrivateB ( ) => _b ;
831
+ }
832
+
752
833
[ Fact ]
753
834
public void TestEmpty ( )
754
835
{
@@ -791,6 +872,16 @@ public void TestGuidRepresentation()
791
872
var rehydrated = BsonSerializer . Deserialize < TestClass > ( bson ) ;
792
873
Assert . True ( bson . SequenceEqual ( rehydrated . ToBson ( ) ) ) ;
793
874
}
875
+
876
+ [ Fact ]
877
+ public void TestPrivateFieldWithBsonRepresentation ( )
878
+ {
879
+ var testValue = new Guid ( "01020304-0506-0708-090a-0b0c0d0e0f10" ) ;
880
+ var json = $ "{{ '_b' : '{ testValue } ' }}";
881
+
882
+ var deserialized = BsonSerializer . Deserialize < TestClassWithPrivate > ( json ) ;
883
+ Assert . Equal ( testValue , deserialized . GetPrivateB ( ) ) ;
884
+ }
794
885
}
795
886
796
887
public class Int32SerializerTests
@@ -809,6 +900,19 @@ public class TestClass
809
900
public int S ;
810
901
}
811
902
903
+ public class TestClassWithPrivate
904
+ {
905
+ [ BsonRepresentation ( BsonType . String ) ]
906
+ private int _i ;
907
+
908
+ public TestClassWithPrivate ( int i )
909
+ {
910
+ _i = i ;
911
+ }
912
+
913
+ public int GetPrivateI ( ) => _i ;
914
+ }
915
+
812
916
[ Fact ]
813
917
public void TestMin ( )
814
918
{
@@ -912,6 +1016,16 @@ public void TestMax()
912
1016
var rehydrated = BsonSerializer . Deserialize < TestClass > ( bson ) ;
913
1017
Assert . True ( bson . SequenceEqual ( rehydrated . ToBson ( ) ) ) ;
914
1018
}
1019
+
1020
+ [ Fact ]
1021
+ public void TestPrivateFieldWithBsonRepresentation ( )
1022
+ {
1023
+ var testValue = 3 ;
1024
+ var json = $ "{{ '_i' : '{ testValue } ' }}";
1025
+
1026
+ var deserialized = BsonSerializer . Deserialize < TestClassWithPrivate > ( json ) ;
1027
+ Assert . Equal ( testValue , deserialized . GetPrivateI ( ) ) ;
1028
+ }
915
1029
}
916
1030
917
1031
public class Int64SerializerTests
@@ -930,6 +1044,19 @@ public class TestClass
930
1044
public long S ;
931
1045
}
932
1046
1047
+ public class TestClassWithPrivate
1048
+ {
1049
+ [ BsonRepresentation ( BsonType . String ) ]
1050
+ private long _i ;
1051
+
1052
+ public TestClassWithPrivate ( long i )
1053
+ {
1054
+ _i = i ;
1055
+ }
1056
+
1057
+ public long GetPrivateI ( ) => _i ;
1058
+ }
1059
+
933
1060
[ Fact ]
934
1061
public void TestMin ( )
935
1062
{
@@ -1033,6 +1160,16 @@ public void TestMax()
1033
1160
var rehydrated = BsonSerializer . Deserialize < TestClass > ( bson ) ;
1034
1161
Assert . True ( bson . SequenceEqual ( rehydrated . ToBson ( ) ) ) ;
1035
1162
}
1163
+
1164
+ [ Fact ]
1165
+ public void TestPrivateFieldWithBsonRepresentation ( )
1166
+ {
1167
+ var testValue = long . MaxValue ;
1168
+ var json = $ "{{ '_i' : '{ testValue } ' }}";
1169
+
1170
+ var deserialized = BsonSerializer . Deserialize < TestClassWithPrivate > ( json ) ;
1171
+ Assert . Equal ( testValue , deserialized . GetPrivateI ( ) ) ;
1172
+ }
1036
1173
}
1037
1174
1038
1175
public class ObjectIdSerializerTests
@@ -1044,6 +1181,19 @@ public class TestClass
1044
1181
public ObjectId String { get ; set ; }
1045
1182
}
1046
1183
1184
+ public class TestClassWithPrivate
1185
+ {
1186
+ [ BsonRepresentation ( BsonType . String ) ]
1187
+ private ObjectId _o ;
1188
+
1189
+ public TestClassWithPrivate ( ObjectId o )
1190
+ {
1191
+ _o = o ;
1192
+ }
1193
+
1194
+ public ObjectId GetPrivateO ( ) => _o ;
1195
+ }
1196
+
1047
1197
[ Fact ]
1048
1198
public void TestSerializer ( )
1049
1199
{
@@ -1066,6 +1216,18 @@ public void TestSerializer()
1066
1216
var rehydrated = BsonSerializer . Deserialize < TestClass > ( bson ) ;
1067
1217
Assert . True ( bson . SequenceEqual ( rehydrated . ToBson ( ) ) ) ;
1068
1218
}
1219
+
1220
+ [ Fact ]
1221
+ public void TestPrivateFieldWithBsonRepresentation ( )
1222
+ {
1223
+ #pragma warning disable 618
1224
+ var testValue = new ObjectId ( 1 , 2 , 3 , 4 ) ;
1225
+ #pragma warning restore 618
1226
+ var json = "{ '_o' : '000000010000020003000004' }" ;
1227
+
1228
+ var deserialized = BsonSerializer . Deserialize < TestClassWithPrivate > ( json ) ;
1229
+ Assert . Equal ( testValue , deserialized . GetPrivateO ( ) ) ;
1230
+ }
1069
1231
}
1070
1232
1071
1233
public class StringSerializerTests
@@ -1075,6 +1237,19 @@ public class TestClass
1075
1237
public String String { get ; set ; }
1076
1238
}
1077
1239
1240
+ public class TestClassWithPrivate
1241
+ {
1242
+ [ BsonRepresentation ( BsonType . String ) ]
1243
+ private string _s ;
1244
+
1245
+ public TestClassWithPrivate ( string s )
1246
+ {
1247
+ _s = s ;
1248
+ }
1249
+
1250
+ public string GetPrivateS ( ) => _s ;
1251
+ }
1252
+
1078
1253
[ Fact ]
1079
1254
public void TestNull ( )
1080
1255
{
@@ -1122,5 +1297,15 @@ public void TestHelloWorld()
1122
1297
var rehydrated = BsonSerializer . Deserialize < TestClass > ( bson ) ;
1123
1298
Assert . True ( bson . SequenceEqual ( rehydrated . ToBson ( ) ) ) ;
1124
1299
}
1300
+
1301
+ [ Fact ]
1302
+ public void TestPrivateFieldWithBsonRepresentation ( )
1303
+ {
1304
+ var testValue = "test" ;
1305
+ var json = $ "{{ '_s' : '{ testValue } ' }}";
1306
+
1307
+ var deserialized = BsonSerializer . Deserialize < TestClassWithPrivate > ( json ) ;
1308
+ Assert . Equal ( testValue , deserialized . GetPrivateS ( ) ) ;
1309
+ }
1125
1310
}
1126
1311
}
0 commit comments