Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,25 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c
{
using (var stream = new MemoryStream(Encoding.Unicode.GetBytes(data)))
{
var serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(Dictionary<string, string>));
var dict = serializer.ReadObject(stream) as Dictionary<string, string>;
// Converting Json data to array of KeyValuePairs with duplicate keys.
var serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(TraitObject[]));
var listOfTraitObjects = serializer.ReadObject(stream) as TraitObject[];

return dict?.ToArray();
return listOfTraitObjects.Select(i => new KeyValuePair<string, string>(i.Key, i.Value)).ToArray();;
}
}

return base.ConvertFrom(context, culture, value);
}

[System.Runtime.Serialization.DataContract]
private class TraitObject
{
[System.Runtime.Serialization.DataMember(Name = "Key")]
public string Key { get; set; }

[System.Runtime.Serialization.DataMember(Name = "Value")]
public string Value { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,30 @@ public void CustomKeyValueConverterShouldDeserializeEmptyArray()
}

[TestMethod]
public void CustomKeyValueConverterShouldThrowOnDeserializeNullKeyOrValue()
public void CustomKeyValueConverterShouldDeserializeEmptyKeyOrValue()
{
var json = "[{ \"Key\": null, \"Value\": \"\" }]";
var json = "[{ \"Key\": \"\", \"Value\": \"\" }]";

var action = new Action(() => this.customKeyValueConverter.ConvertFrom(null, CultureInfo.InvariantCulture, json));
var data = this.customKeyValueConverter.ConvertFrom(null, CultureInfo.InvariantCulture, json) as KeyValuePair<string, string>[];

Assert.ThrowsException<ArgumentNullException>(action);
Assert.AreEqual(1, data.Length);
Assert.AreEqual(string.Empty, data[0].Key);
Assert.AreEqual(string.Empty, data[0].Value);
}

[TestMethod]
public void CustomKeyValueConverterShouldDeserializeEmptyKeyOrValue()
public void CustomKeyValueConverterShouldDeserializeDuplicateKeysKvps()
{
var json = "[{ \"Key\": \"\", \"Value\": \"\" }]";
var json = "[{ \"Key\": \"key1\", \"Value\": \"val1\" }, { \"Key\": \"key1\", \"Value\": \"val2\" }]";

var data = this.customKeyValueConverter.ConvertFrom(null, CultureInfo.InvariantCulture, json) as KeyValuePair<string, string>[];

Assert.AreEqual(1, data.Length);
Assert.AreEqual(string.Empty, data[0].Key);
Assert.AreEqual(string.Empty, data[0].Value);
Assert.IsNotNull(data);
Assert.AreEqual(2, data.Length);
Assert.AreEqual("key1", data[0].Key);
Assert.AreEqual("val1", data[0].Value);
Assert.AreEqual("key1", data[1].Key);
Assert.AreEqual("val2", data[1].Value);
}
}
}