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

Commit fcac204

Browse files
committed
restore KVP > Dictionary behavior
1 parent 23625fc commit fcac204

File tree

3 files changed

+13
-15
lines changed

3 files changed

+13
-15
lines changed

src/ServiceStack.Text/AutoMappingUtils.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,8 @@ public static object ChangeValueType(object from, Type toType)
260260
{
261261

262262
var toKvpArgs = toKvpType.GetGenericArguments();
263-
var toKeyType = toKvpArgs[0];
264-
var toValueType = toKvpArgs[1];
265263
var toCtor = toKvpType.GetConstructor(toKvpArgs);
266-
var toKey = fromKey.ConvertTo(toKeyType);
267-
var toValue = fromValue.ConvertTo(toValueType);
268-
var to = toCtor.Invoke(new[] {toKey,toValue});
264+
var to = toCtor.Invoke(new[] { fromKey.ConvertTo(toKvpArgs[0]), fromValue.ConvertTo(toKvpArgs[1]) });
269265
return to;
270266
}
271267

@@ -277,7 +273,8 @@ public static object ChangeValueType(object from, Type toType)
277273
var toValueType = toArgs[1];
278274

279275
var to = (IDictionary)toType.CreateInstance();
280-
to[fromKey.ConvertTo(toKeyType)] = fromValue.ConvertTo(toValueType);
276+
to["Key"] = fromKey.ConvertTo(toKeyType);
277+
to["Value"] = fromValue.ConvertTo(toValueType);
281278
return to;
282279
}
283280
}

src/ServiceStack.Text/PlatformExtensions.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -738,14 +738,15 @@ public static Dictionary<string, object> ToObjectDictionary(this object obj)
738738

739739

740740
if (obj is KeyValuePair<string, object> objKvp)
741-
return new Dictionary<string, object> { {objKvp.Key, objKvp.Value} };
741+
return new Dictionary<string, object> { { nameof(objKvp.Key), objKvp.Key }, { nameof(objKvp.Value), objKvp.Value } };
742742
if (obj is KeyValuePair<string, string> strKvp)
743-
return new Dictionary<string, object> { {strKvp.Key, strKvp.Value} };
743+
return new Dictionary<string, object> { { nameof(strKvp.Key), strKvp.Key }, { nameof(strKvp.Value), strKvp.Value } };
744744

745745
if (type.GetKeyValuePairTypes(out _, out var _))
746746
{
747747
return new Dictionary<string, object> {
748-
{ TypeProperties.Get(type).GetPublicGetter("Key")(obj).ConvertTo<string>(), TypeProperties.Get(type).GetPublicGetter("Value")(obj) },
748+
{ "Key", TypeProperties.Get(type).GetPublicGetter("Key")(obj).ConvertTo<string>() },
749+
{ "Value", TypeProperties.Get(type).GetPublicGetter("Value")(obj) },
749750
};
750751
}
751752

tests/ServiceStack.Text.Tests/AutoMappingTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,9 +1251,9 @@ public void Can_ToObjectDictionary_KVPs()
12511251
var strKvp = new KeyValuePair<string, string>("A", "1");
12521252
var intKvp = new KeyValuePair<string, int>("A", 1);
12531253

1254-
Assert.That(objKvp.ToObjectDictionary(), Is.EquivalentTo(new Dictionary<string, object> { {"A", 1} }));
1255-
Assert.That(strKvp.ToObjectDictionary(), Is.EquivalentTo(new Dictionary<string, object> { {"A", "1"} }));
1256-
Assert.That(intKvp.ToObjectDictionary(), Is.EquivalentTo(new Dictionary<string, object> { {"A", 1} }));
1254+
Assert.That(objKvp.ToObjectDictionary(), Is.EquivalentTo(new Dictionary<string, object> { {"Key", "A" }, { "Value", 1 } } ));
1255+
Assert.That(strKvp.ToObjectDictionary(), Is.EquivalentTo(new Dictionary<string, object> { {"Key", "A" }, { "Value", "1"} } ));
1256+
Assert.That(intKvp.ToObjectDictionary(), Is.EquivalentTo(new Dictionary<string, object> { {"Key", "A" }, { "Value", 1 } } ));
12571257

12581258
var objKvp2 = new KeyValuePair<string, object>("B", 2);
12591259
var strKvp2 = new KeyValuePair<string, string>("B", "2");
@@ -1271,9 +1271,9 @@ public void Can_convert_KVPs_to_ObjectDictionary()
12711271
var strKvp = new KeyValuePair<string, string>("A", "1");
12721272
var intKvp = new KeyValuePair<string, int>("A", 1);
12731273

1274-
Assert.That(objKvp.ConvertTo<Dictionary<string,object>>(), Is.EquivalentTo(new Dictionary<string, object> { {"A", 1} }));
1275-
Assert.That(strKvp.ConvertTo<Dictionary<string,object>>(), Is.EquivalentTo(new Dictionary<string, object> { {"A", "1"} }));
1276-
Assert.That(intKvp.ConvertTo<Dictionary<string,object>>(), Is.EquivalentTo(new Dictionary<string, object> { {"A", 1} }));
1274+
Assert.That(objKvp.ConvertTo<Dictionary<string,object>>(), Is.EquivalentTo(new Dictionary<string, object> { {"Key", "A" }, { "Value", 1 } } ));
1275+
Assert.That(strKvp.ConvertTo<Dictionary<string,object>>(), Is.EquivalentTo(new Dictionary<string, object> { {"Key", "A" }, { "Value", "1"} } ));
1276+
Assert.That(intKvp.ConvertTo<Dictionary<string,object>>(), Is.EquivalentTo(new Dictionary<string, object> { {"Key", "A" }, { "Value", 1 } } ));
12771277

12781278
var objKvp2 = new KeyValuePair<string, object>("B", 2);
12791279
var strKvp2 = new KeyValuePair<string, string>("B", "2");

0 commit comments

Comments
 (0)