ServiceStack/ServiceStack.Text — Issue #71: hen I call names to occupy deserialize custom DataContract
Author: alabras
Created: 2012-02-13 16:05:21+00:00
State: open

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using NUnit.Framework;
using ServiceStack.Text;

namespace Test.ListFixture
{
[TestFixture]
public class DeserializeFromStringFixture
{
[Test]
public void deserialize_from_string_with_the_same_field_name()
{
const string jsonList =
"{\"Id\":1,\"List\":[{\"Name\":\"Name One\"},{\"Name\":\"Name Two\"}]}";

var classOne = JsonSerializer.DeserializeFromString(jsonList);

Assert.AreEqual(1, classOne.Id);
Assert.AreEqual(2, classOne.List.Count);
}
[Test]
public void deserialize_from_string_with_the_dataMember_name()
{
const string jsonList =
"{\"Id\":1,\"listClassTwo\":[{\"Name\":\"Name One\"},{\"Name\":\"Name Two\"}]}";

var classOne = JsonSerializer.DeserializeFromString(jsonList);

Assert.AreEqual(1, classOne.Id);
Assert.AreEqual(2, classOne.List.Count);
}
[Test]
public void serialize()
{
var venue = new ClassOne
{
Id = 1,
List = new List {new ClassTwo {Name = "Name One"}, new ClassTwo {Name = "Name Two"}}
};
Console.WriteLine(JsonSerializer.SerializeToString(venue));
}
}

[DataContract]
public class ClassOne
{
[DataMember]
public int Id { get; set; }
[DataMember(Name = "listClassTwo")]
public List List { get; set; }

public ClassOne()
{
List = new List();
}
}
[DataContract]
public class ClassTwo
{
[DataMember]
public string Name { get; set; }
}
}