Skip to content

Commit

Permalink
Merge pull request #328 from navin22/dev/navb/discoveryabortfix
Browse files Browse the repository at this point in the history
Supporting Multiple TestProperty with the same key value.
  • Loading branch information
navin22 committed Jan 10, 2017
2 parents 55f75e9 + a83b782 commit fd4207b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
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);
}
}
}

0 comments on commit fd4207b

Please sign in to comment.