|
| 1 | +using System.Globalization; |
| 2 | +using System.IO; |
| 3 | +using System.Runtime.Serialization; |
| 4 | +using System.Runtime.Serialization.Json; |
| 5 | +using UnitsNet.Units; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace UnitsNet.Tests.Serialization.Json |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// These tests demonstrate the default behavior of the DataContractJsonSerializer when dealing with quantities |
| 12 | + /// <remarks> |
| 13 | + /// <para>Note that the produced schema is different from the one generated using the UnitsNet.Json package</para> |
| 14 | + /// <para> |
| 15 | + /// The default schema can easily be modified using a converter, a.k.a. DataContractSurrogate (.NET Framework) |
| 16 | + /// </para> |
| 17 | + /// </remarks> |
| 18 | + /// </summary> |
| 19 | + public class DefaultDataContractJsonSerializerTests : SerializationTestsBase<string> |
| 20 | + { |
| 21 | + protected override string SerializeObject(object obj) |
| 22 | + { |
| 23 | + var serializer = new DataContractJsonSerializer(obj.GetType()); |
| 24 | + using var stream = new MemoryStream(); |
| 25 | + serializer.WriteObject(stream, obj); |
| 26 | + stream.Position = 0; |
| 27 | + using var streamReader = new StreamReader(stream); |
| 28 | + return streamReader.ReadToEnd(); |
| 29 | + } |
| 30 | + |
| 31 | + protected override T DeserializeObject<T>(string xml) |
| 32 | + { |
| 33 | + var serializer = new DataContractJsonSerializer(typeof(T)); |
| 34 | + using var stream = new MemoryStream(); |
| 35 | + using var writer = new StreamWriter(stream); |
| 36 | + writer.Write(xml); |
| 37 | + writer.Flush(); |
| 38 | + stream.Position = 0; |
| 39 | + return (T)serializer.ReadObject(stream); |
| 40 | + } |
| 41 | + |
| 42 | + [Fact] |
| 43 | + public void DoubleQuantity_SerializedWithDoubleValueAndIntegerUnit() |
| 44 | + { |
| 45 | + var quantity = new Mass(1.20, MassUnit.Milligram); |
| 46 | + var expectedJson = "{\"Value\":1.2,\"Unit\":16}"; |
| 47 | + |
| 48 | + var json = SerializeObject(quantity); |
| 49 | + |
| 50 | + Assert.Equal(expectedJson, json); |
| 51 | + } |
| 52 | + |
| 53 | + [Fact] |
| 54 | + public void DecimalQuantity_SerializedWithDecimalValueValueAndIntegerUnit() |
| 55 | + { |
| 56 | + var quantity = new Information(1.20m, InformationUnit.Exabyte); |
| 57 | + var expectedJson = "{\"Value\":1.20,\"Unit\":4}"; |
| 58 | + |
| 59 | + var json = SerializeObject(quantity); |
| 60 | + |
| 61 | + Assert.Equal(expectedJson, json); |
| 62 | + } |
| 63 | + |
| 64 | + [Fact] |
| 65 | + public void DoubleQuantity_InScientificNotation_SerializedWithExpandedValueAndIntegerUnit() |
| 66 | + { |
| 67 | + var quantity = new Mass(1E+9, MassUnit.Milligram); |
| 68 | + var expectedJson = "{\"Value\":1000000000,\"Unit\":16}"; |
| 69 | + |
| 70 | + var json = SerializeObject(quantity); |
| 71 | + |
| 72 | + Assert.Equal(expectedJson, json); |
| 73 | + } |
| 74 | + |
| 75 | + [Fact] |
| 76 | + public void DecimalQuantity_InScientificNotation_SerializedWithExpandedValueAndIntegerUnit() |
| 77 | + { |
| 78 | + var quantity = new Information(1E+9m, InformationUnit.Exabyte); |
| 79 | + var expectedJson = "{\"Value\":1000000000,\"Unit\":4}"; |
| 80 | + |
| 81 | + var json = SerializeObject(quantity); |
| 82 | + |
| 83 | + Assert.Equal(expectedJson, json); |
| 84 | + } |
| 85 | + |
| 86 | + [Fact] |
| 87 | + public void DoubleQuantity_DeserializedFromDoubleValueAndIntegerUnit() |
| 88 | + { |
| 89 | + var json = "{\"Value\":1.2,\"Unit\":16}"; |
| 90 | + |
| 91 | + var quantity = DeserializeObject<Mass>(json); |
| 92 | + |
| 93 | + Assert.Equal(1.2, quantity.Value); |
| 94 | + Assert.Equal(MassUnit.Milligram, quantity.Unit); |
| 95 | + } |
| 96 | + |
| 97 | + [Fact] |
| 98 | + public void DoubleQuantity_DeserializedFromQuotedDoubleValueAndIntegerUnit() |
| 99 | + { |
| 100 | + var json = "{\"Value\":\"1.2\",\"Unit\":16}"; |
| 101 | + |
| 102 | + var quantity = DeserializeObject<Mass>(json); |
| 103 | + |
| 104 | + Assert.Equal(1.2, quantity.Value); |
| 105 | + Assert.Equal(MassUnit.Milligram, quantity.Unit); |
| 106 | + } |
| 107 | + |
| 108 | + [Fact] |
| 109 | + public void DoubleZeroQuantity_DeserializedFromIntegerUnitAndNoValue() |
| 110 | + { |
| 111 | + var json = "{\"Unit\":16}"; |
| 112 | + |
| 113 | + var quantity = DeserializeObject<Mass>(json); |
| 114 | + |
| 115 | + Assert.Equal(0, quantity.Value); |
| 116 | + Assert.Equal(MassUnit.Milligram, quantity.Unit); |
| 117 | + } |
| 118 | + |
| 119 | + [Fact] |
| 120 | + public void DoubleBaseUnitQuantity_DeserializedFromValueAndNoUnit() |
| 121 | + { |
| 122 | + var json = "{\"Value\":1.2}"; |
| 123 | + |
| 124 | + var quantity = DeserializeObject<Mass>(json); |
| 125 | + |
| 126 | + Assert.Equal(1.2, quantity.Value); |
| 127 | + Assert.Equal(Mass.BaseUnit, quantity.Unit); |
| 128 | + } |
| 129 | + |
| 130 | + [Fact] |
| 131 | + public void DoubleZeroBaseQuantity_DeserializedFromEmptyInput() |
| 132 | + { |
| 133 | + var json = "{}"; |
| 134 | + |
| 135 | + var quantity = DeserializeObject<Mass>(json); |
| 136 | + |
| 137 | + Assert.Equal(0, quantity.Value); |
| 138 | + Assert.Equal(Mass.BaseUnit, quantity.Unit); |
| 139 | + } |
| 140 | + |
| 141 | + [Fact] |
| 142 | + public void DecimalQuantity_DeserializedFromDecimalValueAndIntegerUnit() |
| 143 | + { |
| 144 | + var json = "{\"Value\":1.200,\"Unit\":4}"; |
| 145 | + |
| 146 | + var quantity = DeserializeObject<Information>(json); |
| 147 | + |
| 148 | + Assert.Equal(1.200m, quantity.Value); |
| 149 | + Assert.Equal("1.200", quantity.Value.ToString(CultureInfo.InvariantCulture)); |
| 150 | + Assert.Equal(InformationUnit.Exabyte, quantity.Unit); |
| 151 | + } |
| 152 | + |
| 153 | + [Fact] |
| 154 | + public void DecimalQuantity_DeserializedFromQuotedDecimalValueAndIntegerUnit() |
| 155 | + { |
| 156 | + var json = "{\"Value\":\"1.200\",\"Unit\":4}"; |
| 157 | + |
| 158 | + var quantity = DeserializeObject<Information>(json); |
| 159 | + |
| 160 | + Assert.Equal(1.200m, quantity.Value); |
| 161 | + Assert.Equal("1.200", quantity.Value.ToString(CultureInfo.InvariantCulture)); |
| 162 | + Assert.Equal(InformationUnit.Exabyte, quantity.Unit); |
| 163 | + } |
| 164 | + |
| 165 | + [Fact] |
| 166 | + public void DecimalZeroQuantity_DeserializedFromIntegerUnitAndNoValue() |
| 167 | + { |
| 168 | + var json = "{\"Unit\":4}"; |
| 169 | + |
| 170 | + var quantity = DeserializeObject<Information>(json); |
| 171 | + |
| 172 | + Assert.Equal(0, quantity.Value); |
| 173 | + Assert.Equal(InformationUnit.Exabyte, quantity.Unit); |
| 174 | + } |
| 175 | + |
| 176 | + [Fact] |
| 177 | + public void DecimalBaseUnitQuantity_DeserializedFromDecimalValueAndNoUnit() |
| 178 | + { |
| 179 | + var json = "{\"Value\":1.200}"; |
| 180 | + |
| 181 | + var quantity = DeserializeObject<Information>(json); |
| 182 | + |
| 183 | + Assert.Equal(1.200m, quantity.Value); |
| 184 | + Assert.Equal("1.200", quantity.Value.ToString(CultureInfo.InvariantCulture)); |
| 185 | + Assert.Equal(Information.BaseUnit, quantity.Unit); |
| 186 | + } |
| 187 | + |
| 188 | + [Fact] |
| 189 | + public void DecimalZeroBaseQuantity_DeserializedFromEmptyInput() |
| 190 | + { |
| 191 | + var json = "{}"; |
| 192 | + |
| 193 | + var quantity = DeserializeObject<Information>(json); |
| 194 | + |
| 195 | + Assert.Equal(0, quantity.Value); |
| 196 | + Assert.Equal(Information.BaseUnit, quantity.Unit); |
| 197 | + } |
| 198 | + |
| 199 | + [Fact] |
| 200 | + public void InterfaceObject_IncludesTypeInformation() |
| 201 | + { |
| 202 | + var testObject = new TestInterfaceObject { Quantity = new Information(1.20m, InformationUnit.Exabyte) }; |
| 203 | + var expectedJson = "{\"Quantity\":{\"__type\":\"Information:#UnitsNet\",\"Value\":1.20,\"Unit\":4}}"; |
| 204 | + |
| 205 | + var json = SerializeObject(testObject); |
| 206 | + |
| 207 | + Assert.Equal(expectedJson, json); |
| 208 | + } |
| 209 | + |
| 210 | + [Fact] |
| 211 | + public void InterfaceObject_WithMissingKnownTypeInformation_ThrowsSerializationException() |
| 212 | + { |
| 213 | + var testObject = new TestInterfaceObject { Quantity = new Volume(1.2, VolumeUnit.Microliter) }; |
| 214 | + |
| 215 | + Assert.Throws<SerializationException>(() => SerializeObject(testObject)); |
| 216 | + } |
| 217 | + } |
| 218 | +} |
0 commit comments