This repository has been archived by the owner on Nov 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
1,628 additions
and
222 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
src/Microsoft.AspNetCore.JsonPatch/Internal/ExpandoObjectAdapter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Generic; | ||
using System.Dynamic; | ||
using Newtonsoft.Json.Serialization; | ||
|
||
namespace Microsoft.AspNetCore.JsonPatch.Internal | ||
{ | ||
public class ExpandoObjectAdapter : IAdapter | ||
{ | ||
public bool TryAdd( | ||
object target, | ||
string segment, | ||
IContractResolver contractResolver, | ||
object value, | ||
out string errorMessage) | ||
{ | ||
var dictionary = (IDictionary<string, object>)target; | ||
|
||
var key = dictionary.GetKeyUsingCaseInsensitiveSearch(segment); | ||
|
||
// As per JsonPatch spec, if a key already exists, adding should replace the existing value | ||
dictionary[key] = ConvertValue(dictionary, key, value); | ||
|
||
errorMessage = null; | ||
return true; | ||
} | ||
|
||
public bool TryGet( | ||
object target, | ||
string segment, | ||
IContractResolver contractResolver, | ||
out object value, | ||
out string errorMessage) | ||
{ | ||
var dictionary = (IDictionary<string, object>)target; | ||
|
||
var key = dictionary.GetKeyUsingCaseInsensitiveSearch(segment); | ||
value = dictionary[key]; | ||
|
||
errorMessage = null; | ||
return true; | ||
} | ||
|
||
public bool TryRemove( | ||
object target, | ||
string segment, | ||
IContractResolver contractResolver, | ||
out string errorMessage) | ||
{ | ||
var dictionary = (IDictionary<string, object>)target; | ||
|
||
var key = dictionary.GetKeyUsingCaseInsensitiveSearch(segment); | ||
|
||
// As per JsonPatch spec, the target location must exist for remove to be successful | ||
if (!dictionary.ContainsKey(key)) | ||
{ | ||
errorMessage = Resources.FormatTargetLocationAtPathSegmentNotFound(segment); | ||
return false; | ||
} | ||
|
||
dictionary.Remove(key); | ||
|
||
errorMessage = null; | ||
return true; | ||
} | ||
|
||
public bool TryReplace( | ||
object target, | ||
string segment, | ||
IContractResolver contractResolver, | ||
object value, | ||
out string errorMessage) | ||
{ | ||
var dictionary = (IDictionary<string, object>)target; | ||
|
||
var key = dictionary.GetKeyUsingCaseInsensitiveSearch(segment); | ||
|
||
// As per JsonPatch spec, the target location must exist for remove to be successful | ||
if (!dictionary.ContainsKey(key)) | ||
{ | ||
errorMessage = Resources.FormatTargetLocationAtPathSegmentNotFound(segment); | ||
return false; | ||
} | ||
|
||
dictionary[key] = ConvertValue(dictionary, key, value); | ||
|
||
errorMessage = null; | ||
return true; | ||
} | ||
|
||
public bool TryTraverse( | ||
object target, | ||
string segment, | ||
IContractResolver contractResolver, | ||
out object nextTarget, | ||
out string errorMessage) | ||
{ | ||
var expandoObject = target as ExpandoObject; | ||
if (expandoObject == null) | ||
{ | ||
errorMessage = null; | ||
nextTarget = null; | ||
return false; | ||
} | ||
|
||
var dictionary = (IDictionary<string, object>)expandoObject; | ||
|
||
var key = dictionary.GetKeyUsingCaseInsensitiveSearch(segment); | ||
|
||
if (dictionary.ContainsKey(key)) | ||
{ | ||
nextTarget = dictionary[key]; | ||
errorMessage = null; | ||
return true; | ||
} | ||
else | ||
{ | ||
nextTarget = null; | ||
errorMessage = null; | ||
return false; | ||
} | ||
} | ||
|
||
private object ConvertValue(IDictionary<string, object> dictionary, string key, object newValue) | ||
{ | ||
object existingValue = null; | ||
if (dictionary.TryGetValue(key, out existingValue)) | ||
{ | ||
if (existingValue != null) | ||
{ | ||
var conversionResult = ConversionResultProvider.ConvertTo(newValue, existingValue.GetType()); | ||
if (conversionResult.CanBeConverted) | ||
{ | ||
return conversionResult.ConvertedInstance; | ||
} | ||
} | ||
} | ||
return newValue; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.