[Test]
[ExpectedException (typeof (SerializationException))]
- [Category ("NotDotNet")] // 0.0 is an invalid Colors value.
+ [Ignore ("NotDotNet")] // 0.0 is an invalid Colors value.
public void DeserializeEnumInvalid3 ()
{
//"0.0" instead of "0"
[Test]
[ExpectedException (typeof (SerializationException))]
- [Category ("NotDotNet")] // 4 is an invalid Colors value.
+ [Ignore ("NotDotNet")] // 4 is an invalid Colors value.
[Category ("NotWorking")]
public void DeserializeEnumWithDCInvalid ()
{
var ms = new MemoryStream ();
DataContractJsonSerializer serializer = new DataContractJsonSerializer (typeof (Query));
Query query = new Query () {
- StartDate = new DateTime (2010, 3, 4, 5, 6, 7),
- EndDate = new DateTime (2010, 4, 5, 6, 7, 8)
+ StartDate = DateTime.SpecifyKind (new DateTime (2010, 3, 4, 5, 6, 7), DateTimeKind.Utc),
+ EndDate = DateTime.SpecifyKind (new DateTime (2010, 4, 5, 6, 7, 8), DateTimeKind.Utc)
};
serializer.WriteObject (ms, query);
Assert.AreEqual ("{\"StartDate\":\"\\/Date(1267679167000)\\/\",\"EndDate\":\"\\/Date(1270447628000)\\/\"}", Encoding.UTF8.GetString (ms.ToArray ()), "#1");
[Test]
public void BugXamarin163 ()
{
- string json = @"{""should_have_value"":""\/Date(1277355600000-0500)\/""}";
+ string json = @"{""should_have_value"":""\/Date(1277355600000)\/""}";
byte[] bytes = global::System.Text.Encoding.UTF8.GetBytes(json);
Stream inputStream = new MemoryStream(bytes);
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(DateTest));
DateTest t = serializer.ReadObject(inputStream) as DateTest;
- Assert.AreEqual (634129344000000000, t.ShouldHaveValue.Value.Ticks, "#1");
+ Assert.AreEqual (634129524000000000, t.ShouldHaveValue.Value.Ticks, "#1");
}
[Test]
string serializedObj = @"{""PolymorphicProperty"":{""__type"":""UnknownDerivedType:#MonoTests.System.Runtime.Serialization.Json"",""BaseTypeProperty"":""Base"",""DerivedProperty"":""Derived 1""},""Name"":""Parent2""}";
ParentType deserializedObj = Deserialize<ParentType> (serializedObj);
}
-
+
+ [Test]
+ public void SubclassTest ()
+ {
+ var knownTypes = new List<Type> { typeof(IntList) };
+ var serializer = new DataContractJsonSerializer(typeof(ListOfNumbers), knownTypes);
+
+ string json = "{\"Numbers\": [85]}";
+ using (var stream = new MemoryStream(UTF8Encoding.Default.GetBytes(json)))
+ {
+ var nums = (ListOfNumbers)serializer.ReadObject(stream);
+ Assert.AreEqual (1, nums.Numbers.Count);
+ }
+ }
+ [DataContract]
+ public class ListOfNumbers
+ {
+ [DataMember]
+ public IntList Numbers;
+ }
+
+ public class IntList : List<int>{}
#endregion
+
+ [Test]
+ public void DefaultValueDeserialization ()
+ {
+ // value type
+ var person = new Person { name = "John" };
+ using (var ms = new MemoryStream()) {
+ var serializer = new DataContractJsonSerializer (typeof (Person), new DataContractJsonSerializerSettings {
+ SerializeReadOnlyTypes = true,
+ UseSimpleDictionaryFormat = true
+ });
+ serializer.WriteObject (ms, person);
+ }
+
+ // reference type
+ var person2 = new PersonWithContact {
+ name = "Jane",
+ contact = new Contact { url = "localhost", email = "jane@localhost" } };
+ using (var ms = new MemoryStream ()) {
+ var serializer = new DataContractJsonSerializer (typeof (PersonWithContact), new DataContractJsonSerializerSettings {
+ SerializeReadOnlyTypes = true,
+ UseSimpleDictionaryFormat = true
+ });
+ serializer.WriteObject (ms, person2);
+ }
+ }
+
+ [Test]
+ public void Bug15028()
+ {
+ DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Bug15028));
+ using (MemoryStream memoryStream = new MemoryStream())
+ {
+ ser.WriteObject(memoryStream, new Bug15028());
+ string output = Encoding.Default.GetString(memoryStream.ToArray());
+ Assert.AreEqual(@"{""Int0"":1,""Int1"":1,""IntZero1"":0,""Str0"":"""",""Str1"":"""",""StrNull1"":null}", output);
+ }
+ }
}
+
+ [DataContract]
+ public class Bug15028
+ {
+ [DataMember(EmitDefaultValue = false)]
+ public string StrNull0 { get; private set; }
+
+ [DataMember(EmitDefaultValue = false)]
+ public string Str0 { get; private set; }
+
+ [DataMember(EmitDefaultValue = true)]
+ public string StrNull1 { get; private set; }
+
+ [DataMember(EmitDefaultValue = true)]
+ public string Str1 { get; private set; }
+
+ [DataMember(EmitDefaultValue = false)]
+ public int IntZero0 { get; private set; }
+
+ [DataMember(EmitDefaultValue = false)]
+ public int Int0 { get; private set; }
+
+ [DataMember(EmitDefaultValue = true)]
+ public int IntZero1 { get; private set; }
+
+ [DataMember(EmitDefaultValue = true)]
+ public int Int1 { get; private set; }
+
+ public Bug15028()
+ {
+ Str0 = string.Empty;
+ Str1 = string.Empty;
+ Int0 = 1;
+ Int1 = 1;
+ }
+ }
+
public class CharTest
{
public char Foo;
void Init ()
{
C = true;
+ ServerTimeUTC = DateTime.SpecifyKind (DateTime.MinValue, DateTimeKind.Utc);
}
[OnDeserializing]
}
}
-#endregion
\ No newline at end of file
+#endregion
+
+#region DefaultValueDeserialization
+ [DataContract]
+ public class Person
+ {
+ [DataMember(EmitDefaultValue = false)]
+ public string name { get; set; }
+ }
+
+ [DataContract]
+ public class PersonWithContact
+ {
+ [DataMember(EmitDefaultValue = false)]
+ public string name { get; set; }
+
+ [DataMember(EmitDefaultValue = false)]
+ public Contact contact { get; set; }
+ }
+
+ [DataContract]
+ public class Contact
+ {
+ [DataMember(EmitDefaultValue = false)]
+ public string url { get; set; }
+
+ [DataMember(EmitDefaultValue = false)]
+ public string email{ get; set; }
+ }
+#endregion