#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
#if HAVE_DYNAMIC
using System;
using System.Dynamic;
using System.Runtime.CompilerServices;
using LC.Newtonsoft.Json.Utilities;
namespace LC.Newtonsoft.Json.Serialization
{
///
/// Contract details for a used by the .
///
public class JsonDynamicContract : JsonContainerContract
{
///
/// Gets the object's properties.
///
/// The object's properties.
public JsonPropertyCollection Properties { get; }
///
/// Gets or sets the property name resolver.
///
/// The property name resolver.
public Func PropertyNameResolver { get; set; }
private readonly ThreadSafeStore>> _callSiteGetters =
new ThreadSafeStore>>(CreateCallSiteGetter);
private readonly ThreadSafeStore>> _callSiteSetters =
new ThreadSafeStore>>(CreateCallSiteSetter);
private static CallSite> CreateCallSiteGetter(string name)
{
GetMemberBinder getMemberBinder = (GetMemberBinder)DynamicUtils.BinderWrapper.GetMember(name, typeof(DynamicUtils));
return CallSite>.Create(new NoThrowGetBinderMember(getMemberBinder));
}
private static CallSite> CreateCallSiteSetter(string name)
{
SetMemberBinder binder = (SetMemberBinder)DynamicUtils.BinderWrapper.SetMember(name, typeof(DynamicUtils));
return CallSite>.Create(new NoThrowSetBinderMember(binder));
}
///
/// Initializes a new instance of the class.
///
/// The underlying type for the contract.
public JsonDynamicContract(Type underlyingType)
: base(underlyingType)
{
ContractType = JsonContractType.Dynamic;
Properties = new JsonPropertyCollection(UnderlyingType);
}
internal bool TryGetMember(IDynamicMetaObjectProvider dynamicProvider, string name, out object value)
{
ValidationUtils.ArgumentNotNull(dynamicProvider, nameof(dynamicProvider));
CallSite> callSite = _callSiteGetters.Get(name);
object result = callSite.Target(callSite, dynamicProvider);
if (!ReferenceEquals(result, NoThrowExpressionVisitor.ErrorResult))
{
value = result;
return true;
}
else
{
value = null;
return false;
}
}
internal bool TrySetMember(IDynamicMetaObjectProvider dynamicProvider, string name, object value)
{
ValidationUtils.ArgumentNotNull(dynamicProvider, nameof(dynamicProvider));
CallSite> callSite = _callSiteSetters.Get(name);
object result = callSite.Target(callSite, dynamicProvider, value);
return !ReferenceEquals(result, NoThrowExpressionVisitor.ErrorResult);
}
}
}
#endif