@@ -16,13 +16,74 @@ namespace Azure.Core.Tests
1616{
1717 public class BinaryDataSerializationTests
1818 {
19+ [ Test ]
20+ public void DeserializeModelWithDictionaryOfBinaryData ( )
21+ {
22+ using var fs = File . Open ( GetFileName ( "JsonFormattedStringDictOfBinaryData.json" ) , FileMode . Open , FileAccess . Read , FileShare . Read ) ;
23+ using var document = JsonDocument . Parse ( fs ) ;
24+ var data = ModelWithBinaryDataInDictionary . DeserializeModelWithBinaryDataInDictionary ( document . RootElement ) ;
25+
26+ Assert . AreEqual ( "a.value" , data . A ) ;
27+ Assert . AreEqual ( "1" , data . Details [ "strValue" ] . ToObjectFromJson < string > ( ) ) ;
28+ Assert . IsTrue ( data . Details [ "strValue" ] . ToObjectFromJson ( ) is string ) ;
29+ Assert . AreEqual ( 1 , data . Details [ "intValue" ] . ToObjectFromJson < int > ( ) ) ;
30+ Assert . IsTrue ( data . Details [ "intValue" ] . ToObjectFromJson ( ) is int ) ;
31+ Assert . AreEqual ( 1.1 , data . Details [ "doubleValue" ] . ToObjectFromJson < double > ( ) ) ;
32+ Assert . IsTrue ( data . Details [ "doubleValue" ] . ToObjectFromJson ( ) is double ) ;
33+
34+ var toObjectWithT = data . Details [ "innerProperties" ] . ToObjectFromJson < Dictionary < string , object > > ( ) ;
35+ var jsonElementObject = data . Details [ "innerProperties" ] . ToObjectFromJson < object > ( ) ;
36+ Assert . IsTrue ( jsonElementObject is JsonElement ) ;
37+ var jsonDictionary = data . Details [ "innerProperties" ] . ToObjectFromJson ( ) ;
38+ Assert . IsTrue ( jsonDictionary is Dictionary < string , object > ) ;
39+ Assert . IsTrue ( toObjectWithT [ "strValue" ] is JsonElement ) ;
40+
41+ var dict = data . Details [ "innerProperties" ] . ToObjectFromJson ( ) as Dictionary < string , object > ;
42+ Assert . AreEqual ( "2" , ( string ) dict [ "strValue" ] ) ;
43+ Assert . AreEqual ( 2 , ( int ) dict [ "intValue" ] ) ;
44+ Assert . AreEqual ( 2.2 , ( double ) dict [ "doubleValue" ] ) ;
45+ }
46+
47+ [ Test ]
48+ public void CanConvertInt ( )
49+ {
50+ using var fs = File . Open ( GetFileName ( "JsonFormattedStringInt.json" ) , FileMode . Open , FileAccess . Read , FileShare . Read ) ;
51+ using var document = JsonDocument . Parse ( fs ) ;
52+ var data = ModelWithBinaryData . DeserializeModelWithBinaryData ( document . RootElement ) ;
53+
54+ Assert . AreEqual ( "a.value" , data . A ) ;
55+ Assert . AreEqual ( 1 , data . Properties . ToObjectFromJson < int > ( ) ) ;
56+ }
57+
58+ [ Test ]
59+ public void CanConvertDouble ( )
60+ {
61+ using var fs = File . Open ( GetFileName ( "JsonFormattedStringDouble.json" ) , FileMode . Open , FileAccess . Read , FileShare . Read ) ;
62+ using var document = JsonDocument . Parse ( fs ) ;
63+ var data = ModelWithBinaryData . DeserializeModelWithBinaryData ( document . RootElement ) ;
64+
65+ Assert . AreEqual ( "a.value" , data . A ) ;
66+ Assert . AreEqual ( 1.1 , data . Properties . ToObjectFromJson < double > ( ) ) ;
67+ }
68+
69+ [ Test ]
70+ public void CanConvertString ( )
71+ {
72+ using var fs = File . Open ( GetFileName ( "JsonFormattedStringString.json" ) , FileMode . Open , FileAccess . Read , FileShare . Read ) ;
73+ using var document = JsonDocument . Parse ( fs ) ;
74+ var data = ModelWithBinaryData . DeserializeModelWithBinaryData ( document . RootElement ) ;
75+
76+ Assert . AreEqual ( "a.value" , data . A ) ;
77+ Assert . AreEqual ( "1" , data . Properties . ToObjectFromJson < string > ( ) ) ;
78+ }
79+
1980 [ Test ]
2081 public void CanConvertDifferentValueTypes ( )
2182 {
2283 var expected = File . ReadAllText ( GetFileName ( "PropertiesWithDifferentValueTypes.json" ) ) . TrimEnd ( ) ;
2384 var model = BinaryData . FromString ( expected ) ;
2485
25- var properties = model . ToDictionaryFromJson ( ) ;
86+ var properties = model . ToObjectFromJson ( ) as Dictionary < string , object > ;
2687 Assert . AreEqual ( typeof ( string ) , properties [ "stringValue" ] . GetType ( ) ) ;
2788 Assert . AreEqual ( typeof ( string ) , properties [ "dateTimeValue" ] . GetType ( ) ) ;
2889 Assert . AreEqual ( typeof ( int ) , properties [ "intValue" ] . GetType ( ) ) ;
@@ -41,7 +102,7 @@ public void CanConvertArrays()
41102 var expected = File . ReadAllText ( GetFileName ( "PropertiesWithArrays.json" ) ) . TrimEnd ( ) ;
42103 var model = BinaryData . FromString ( expected ) ;
43104
44- var properties = model . ToDictionaryFromJson ( ) ;
105+ var properties = model . ToObjectFromJson ( ) as Dictionary < string , object > ;
45106 Assert . IsTrue ( AllValuesAreType ( typeof ( string ) , properties [ "stringArray" ] ) ) ;
46107 Assert . IsTrue ( AllValuesAreType ( typeof ( string ) , properties [ "dateTimeArray" ] ) ) ;
47108 Assert . IsTrue ( AllValuesAreType ( typeof ( int ) , properties [ "intArray" ] ) ) ;
@@ -72,7 +133,7 @@ public void CanConvertArrayOfObjects()
72133 {
73134 var expected = File . ReadAllText ( GetFileName ( "PropertiesWithArraysOfObjects.json" ) ) . TrimEnd ( ) ;
74135 var model = BinaryData . FromString ( expected ) ;
75- var properties = model . ToDictionaryFromJson ( ) ;
136+ var properties = model . ToObjectFromJson ( ) as Dictionary < string , object > ;
76137 var objArray = properties [ "objectArray" ] as object [ ] ;
77138 for ( int i = 0 ; i < 3 ; i ++ )
78139 {
@@ -91,7 +152,7 @@ public void CanConvertArrayOfArrays()
91152 {
92153 var expected = File . ReadAllText ( GetFileName ( "PropertiesWithArraysOfArrays.json" ) ) . TrimEnd ( ) ;
93154 var model = BinaryData . FromString ( expected ) ;
94- var properties = model . ToDictionaryFromJson ( ) ;
155+ var properties = model . ToObjectFromJson ( ) as Dictionary < string , object > ;
95156 var arrayArray = properties [ "arrayArray" ] as object [ ] ;
96157 Assert . IsNotNull ( arrayArray ) ;
97158 for ( int i = 0 ; i < 2 ; i ++ )
@@ -141,7 +202,7 @@ public void DeserializeJsonFormattedStringWithBinaryData()
141202 var data = ModelWithBinaryData . DeserializeModelWithBinaryData ( document . RootElement ) ;
142203 Assert . AreEqual ( "a.value" , data . A ) ;
143204
144- var properties = data . Properties . ToDictionaryFromJson ( ) ;
205+ var properties = data . Properties . ToObjectFromJson ( ) as Dictionary < string , object > ;
145206 Assert . AreEqual ( "properties.a.value" , properties [ "a" ] ) ;
146207 var innerProperties = properties [ "innerProperties" ] as IDictionary < string , object > ;
147208 Assert . AreEqual ( "properties.innerProperties.a.value" , innerProperties [ "a" ] ) ;
@@ -177,6 +238,34 @@ public void SerailizeUsingJsonFormattedString()
177238 Assert . AreEqual ( expected , actual ) ;
178239 }
179240
241+ [ Test ]
242+ public void SerailizeUsingJsonFormattedStringForDictOfBinaryData ( )
243+ {
244+ #if NET461
245+ var expected = File . ReadAllText ( GetFileName ( "JsonFormattedStringDictOfBinaryDataNet461.json" ) ) . TrimEnd ( ) ;
246+ #else
247+ var expected = File . ReadAllText ( GetFileName ( "JsonFormattedStringDictOfBinaryData.json" ) ) . TrimEnd ( ) ;
248+ #endif
249+
250+ var payload = new ModelWithBinaryDataInDictionary { A = "a.value" } ;
251+
252+ var details = new Dictionary < string , BinaryData > ( ) ;
253+ details [ "strValue" ] = BinaryData . FromObjectAsJson ( "1" ) ;
254+ details [ "intValue" ] = BinaryData . FromObjectAsJson ( 1 ) ;
255+ details [ "doubleValue" ] = BinaryData . FromObjectAsJson ( 1.1 ) ;
256+
257+ var innerProperties = new Dictionary < string , object > ( ) ;
258+ innerProperties [ "strValue" ] = "2" ;
259+ innerProperties [ "intValue" ] = 2 ;
260+ innerProperties [ "doubleValue" ] = 2.2 ;
261+
262+ details [ "innerProperties" ] = BinaryData . FromObjectAsJson ( innerProperties ) ;
263+ payload . Details = details ;
264+
265+ string actual = GetSerializedString ( payload ) ;
266+ Assert . AreEqual ( expected , actual ) ;
267+ }
268+
180269 [ Test ]
181270 public void SerailizeUsingStream ( )
182271 {
0 commit comments