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

Commit 8c7352b

Browse files
committed
Allow [DataMember] to use as alias without needing [DataContract]
1 parent 78c1f04 commit 8c7352b

File tree

6 files changed

+20
-31
lines changed

6 files changed

+20
-31
lines changed

src/ServiceStack.Text/Common/DeserializeTypeRef.cs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ internal static KeyValuePair<string, TypeAccessor>[] GetCachedTypeAccessors(Type
6161
internal static KeyValuePair<string, TypeAccessor>[] GetTypeAccessors(TypeConfig typeConfig, ITypeSerializer serializer)
6262
{
6363
var type = typeConfig.Type;
64-
var isDataContract = type.IsDto();
6564

6665
var propertyInfos = type.GetSerializableProperties();
6766
var fieldInfos = type.GetSerializableFields();
@@ -77,13 +76,10 @@ internal static KeyValuePair<string, TypeAccessor>[] GetTypeAccessors(TypeConfig
7776
{
7877
var propertyInfo = propertyInfos[i];
7978
var propertyName = propertyInfo.Name;
80-
if (isDataContract)
79+
var dcsDataMember = propertyInfo.GetDataMember();
80+
if (dcsDataMember?.Name != null)
8181
{
82-
var dcsDataMember = propertyInfo.GetDataMember();
83-
if (dcsDataMember?.Name != null)
84-
{
85-
propertyName = dcsDataMember.Name;
86-
}
82+
propertyName = dcsDataMember.Name;
8783
}
8884

8985
accessors[i] = new KeyValuePair<string, TypeAccessor>(propertyName, TypeAccessor.Create(serializer, typeConfig, propertyInfo));
@@ -96,13 +92,10 @@ internal static KeyValuePair<string, TypeAccessor>[] GetTypeAccessors(TypeConfig
9692
{
9793
var fieldInfo = fieldInfos[j];
9894
var fieldName = fieldInfo.Name;
99-
if (isDataContract)
95+
var dcsDataMember = fieldInfo.GetDataMember();
96+
if (dcsDataMember?.Name != null)
10097
{
101-
var dcsDataMember = fieldInfo.GetDataMember();
102-
if (dcsDataMember?.Name != null)
103-
{
104-
fieldName = dcsDataMember.Name;
105-
}
98+
fieldName = dcsDataMember.Name;
10699
}
107100

108101
accessors[i + j] = new KeyValuePair<string, TypeAccessor>(fieldName, TypeAccessor.Create(serializer, typeConfig, fieldInfo));

src/ServiceStack.Text/CsvReader.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,6 @@ internal static void Reset()
208208
PropertyConverters = new List<ParseStringDelegate>();
209209
PropertyConvertersMap = new Dictionary<string, ParseStringDelegate>(PclExport.Instance.InvariantComparerIgnoreCase);
210210

211-
var isDataContract = typeof(T).IsDto();
212211
foreach (var propertyInfo in TypeConfig<T>.Properties)
213212
{
214213
if (!propertyInfo.CanWrite || propertyInfo.GetSetMethod(nonPublic:true) == null) continue;
@@ -221,12 +220,9 @@ internal static void Reset()
221220
var converter = JsvReader.GetParseFn(propertyInfo.PropertyType);
222221
PropertyConverters.Add(converter);
223222

224-
if (isDataContract)
225-
{
226-
var dcsDataMemberName = propertyInfo.GetDataMemberName();
227-
if (dcsDataMemberName != null)
228-
propertyName = dcsDataMemberName;
229-
}
223+
var dcsDataMemberName = propertyInfo.GetDataMemberName();
224+
if (dcsDataMemberName != null)
225+
propertyName = dcsDataMemberName;
230226

231227
Headers.Add(propertyName);
232228
PropertySettersMap[propertyName] = setter;

src/ServiceStack.Text/CsvSerializer.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ internal static WriteObjectDelegate GetWriteFn(Type type)
3535
do
3636
{
3737
snapshot = WriteFnCache;
38-
newCache = new Dictionary<Type, WriteObjectDelegate>(WriteFnCache);
39-
newCache[type] = writeFn;
38+
newCache = new Dictionary<Type, WriteObjectDelegate>(WriteFnCache) {[type] = writeFn};
4039

4140
} while (!ReferenceEquals(
4241
Interlocked.CompareExchange(ref WriteFnCache, newCache, snapshot), snapshot));
@@ -68,8 +67,7 @@ internal static ParseStringDelegate GetReadFn(Type type)
6867
do
6968
{
7069
snapshot = ReadFnCache;
71-
newCache = new Dictionary<Type, ParseStringDelegate>(ReadFnCache);
72-
newCache[type] = writeFn;
70+
newCache = new Dictionary<Type, ParseStringDelegate>(ReadFnCache) {[type] = writeFn};
7371

7472
} while (!ReferenceEquals(
7573
Interlocked.CompareExchange(ref ReadFnCache, newCache, snapshot), snapshot));

src/ServiceStack.Text/CsvWriter.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,20 +162,16 @@ internal static void Reset()
162162
Headers = new List<string>();
163163

164164
PropertyGetters = new List<GetMemberDelegate<T>>();
165-
var isDataContract = typeof(T).IsDto();
166165
foreach (var propertyInfo in TypeConfig<T>.Properties)
167166
{
168167
if (!propertyInfo.CanRead || propertyInfo.GetGetMethod(nonPublic:true) == null) continue;
169168
if (!TypeSerializer.CanCreateFromString(propertyInfo.PropertyType)) continue;
170169

171170
PropertyGetters.Add(propertyInfo.CreateGetter<T>());
172171
var propertyName = propertyInfo.Name;
173-
if (isDataContract)
174-
{
175-
var dcsDataMemberName = propertyInfo.GetDataMemberName();
176-
if (dcsDataMemberName != null)
177-
propertyName = dcsDataMemberName;
178-
}
172+
var dcsDataMemberName = propertyInfo.GetDataMemberName();
173+
if (dcsDataMemberName != null)
174+
propertyName = dcsDataMemberName;
179175
Headers.Add(propertyName);
180176
}
181177
}

tests/ServiceStack.Text.Tests/DataContractTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,10 @@ public void Does_use_alias_and_is_not_optin_without_DataContract_Attribute()
329329
{
330330
var dto = new AliasWithoutDataContract { Id = 1, Name = "foo" };
331331
Assert.That(dto.ToJson(), Is.EqualTo("{\"Id\":1,\"alias\":\"foo\"}"));
332+
333+
Assert.That(dto.ToJsv(), Is.EqualTo("{Id:1,alias:foo}"));
334+
335+
Assert.That(dto.ToCsv(), Is.EqualTo("Id,alias\r\n1,foo\r\n"));
332336
}
333337

334338
}

tests/ServiceStack.Text.Tests/LicensingTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections;
66
using System.IO;
7+
using System.IO.Compression;
78
using System.Reflection;
89
using NUnit.Framework;
910

@@ -44,6 +45,7 @@ public class LicensingTests
4445
[SetUp]
4546
public void SetUp()
4647
{
48+
4749
LicenseUtils.RemoveLicense();
4850
}
4951

0 commit comments

Comments
 (0)