Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit a9885ea

Browse files
committed
Merge pull request #408 from bjdodson/master
Support DataMember attribute on fields.
2 parents 0c10a96 + 08e1696 commit a9885ea

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

src/ServiceStack.Text/Common/DeserializeTypeRef.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ internal static SerializationException GetSerializationException(string property
3636
internal static Dictionary<string, TypeAccessor> GetTypeAccessorMap(TypeConfig typeConfig, ITypeSerializer serializer)
3737
{
3838
var type = typeConfig.Type;
39+
var isDataContract = type.IsDto();
3940

4041
var propertyInfos = type.GetSerializableProperties();
4142
var fieldInfos = type.GetSerializableFields();
@@ -45,7 +46,6 @@ internal static Dictionary<string, TypeAccessor> GetTypeAccessorMap(TypeConfig t
4546

4647
if (propertyInfos.Length != 0)
4748
{
48-
var isDataContract = type.IsDto();
4949
foreach (var propertyInfo in propertyInfos)
5050
{
5151
var propertyName = propertyInfo.Name;
@@ -66,6 +66,14 @@ internal static Dictionary<string, TypeAccessor> GetTypeAccessorMap(TypeConfig t
6666
foreach (var fieldInfo in fieldInfos)
6767
{
6868
var field = fieldInfo.Name;
69+
if (isDataContract)
70+
{
71+
var dcsDataMember = fieldInfo.GetDataMember();
72+
if (dcsDataMember != null && dcsDataMember.Name != null)
73+
{
74+
field = dcsDataMember.Name;
75+
}
76+
}
6977
map[field] = TypeAccessor.Create(serializer, typeConfig, fieldInfo);
7078
}
7179
}

tests/ServiceStack.Text.Tests/DataContractTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,13 @@ public class ClassThree
188188
public string Title { get; set; }
189189
}
190190

191+
[DataContract]
192+
public class ClassFour
193+
{
194+
[DataMember(Name = "some-title")]
195+
public string Title;
196+
}
197+
191198
[Test]
192199
public void Csv_Serialize_Should_Respects_DataContract_Name()
193200
{
@@ -218,6 +225,13 @@ public void Json_Serialize_Should_Respects_DataContract_Name_When_Deserialize()
218225
Assert.That(t.Title, Is.EqualTo("right"));
219226
}
220227

228+
[Test]
229+
public void Json_Serialize_Should_Respects_DataContract_Field_Name_When_Deserialize()
230+
{
231+
var t = JsonSerializer.DeserializeFromString<ClassFour>("{\"some-title\": \"right\", \"Title\": \"wrong\"}");
232+
Assert.That(t.Title, Is.EqualTo("right"));
233+
}
234+
221235
[Test]
222236
public void Json_Serialize_Should_Respects_DataContract_Name()
223237
{

tests/ServiceStack.Text.Tests/Support/MovieDtos.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ public Movie()
4040
public decimal Rating { get; set; }
4141

4242
[DataMember(Order = 5, EmitDefaultValue = true, IsRequired = false)]
43-
public string Director { get; set; }
43+
public string Director;
4444

4545
[DataMember(Order = 6, EmitDefaultValue = false, IsRequired = false)]
4646
public DateTime ReleaseDate { get; set; }
4747

4848
[DataMember(Order = 6, EmitDefaultValue = false, IsRequired = false)]
49-
public string TagLine { get; set; }
49+
public string TagLine;
5050

5151
[DataMember(Order = 8, EmitDefaultValue = false, IsRequired = false)]
5252
public List<string> Genres { get; set; }

0 commit comments

Comments
 (0)