Skip to content

Commit

Permalink
case insensitive property lookup (I guess we're already down that rab…
Browse files Browse the repository at this point in the history
…bit hole)
  • Loading branch information
Johannes Bader committed Aug 8, 2017
1 parent 377f174 commit d26f0e5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
8 changes: 0 additions & 8 deletions src/SDKs/Network/Network.Tests/Properties/launchSettings.json

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System.Linq;

namespace Microsoft.Rest.Serialization
{
Expand Down Expand Up @@ -47,6 +48,23 @@ public override bool CanConvert(Type objectType)
return typeof(T).GetTypeInfo().IsAssignableFrom(objectType.GetTypeInfo());
}

/// <summary>
/// Case insensitive (and reduced version) of JToken.SelectToken which unfortunately does not offer
/// such functionality and has made all potential extension points `internal`.
/// </summary>
private JToken SelectTokenCaseInsensitive(JObject obj, string path)
{
JToken result = obj;
foreach (var pathComponent in path.Split('.'))
{
result = (result as JObject)?
.Properties()
.FirstOrDefault(p => string.Equals(p.Name, pathComponent, StringComparison.OrdinalIgnoreCase))?
.Value;
}
return result;
}

/// <summary>
/// Reads a JSON field and deserializes into an appropriate object based on discriminator
/// field and object name. If JsonObject attribute is available, its value is used instead.
Expand Down Expand Up @@ -75,7 +93,7 @@ public override object ReadJson(JsonReader reader,
// parse properties
foreach (var expectedProperty in contract.Properties)
{
var property = item.SelectToken(expectedProperty.PropertyName);
var property = SelectTokenCaseInsensitive(item, expectedProperty.PropertyName);
if (property != null)
{
var propertyValue = property.ToObject(expectedProperty.PropertyType, serializer);
Expand Down

0 comments on commit d26f0e5

Please sign in to comment.