using System;
using System.Globalization;
using System.Reflection;
using System.Runtime.Serialization;
using System.Security;
using DPumpHydr.WinFrmUI.WenSkin.Json.Linq;
using DPumpHydr.WinFrmUI.WenSkin.Json.Utilities;
namespace DPumpHydr.WinFrmUI.WenSkin.Json.Serialization
{
///
/// Contract details for a used by the .
///
public class JsonObjectContract : JsonContainerContract
{
internal bool ExtensionDataIsJToken;
private bool? _hasRequiredOrDefaultValueProperties;
private ConstructorInfo _parametrizedConstructor;
private ConstructorInfo _overrideConstructor;
private ObjectConstructor _overrideCreator;
private ObjectConstructor _parameterizedCreator;
private JsonPropertyCollection _creatorParameters;
private Type _extensionDataValueType;
///
/// Gets or sets the object member serialization.
///
/// The member object serialization.
public MemberSerialization MemberSerialization { get; set; }
///
/// Gets or sets a value that indicates whether the object's properties are required.
///
///
/// A value indicating whether the object's properties are required.
///
public Required? ItemRequired { get; set; }
///
/// Gets the object's properties.
///
/// The object's properties.
public JsonPropertyCollection Properties { get; private set; }
///
/// Gets the constructor parameters required for any non-default constructor
///
[Obsolete("ConstructorParameters is obsolete. Use CreatorParameters instead.")]
public JsonPropertyCollection ConstructorParameters => CreatorParameters;
///
/// Gets a collection of instances that define the parameters used with .
///
public JsonPropertyCollection CreatorParameters
{
get
{
if (_creatorParameters == null)
{
_creatorParameters = new JsonPropertyCollection(base.UnderlyingType);
}
return _creatorParameters;
}
}
///
/// Gets or sets the override constructor used to create the object.
/// This is set when a constructor is marked up using the
/// JsonConstructor attribute.
///
/// The override constructor.
[Obsolete("OverrideConstructor is obsolete. Use OverrideCreator instead.")]
public ConstructorInfo OverrideConstructor
{
get
{
return _overrideConstructor;
}
set
{
_overrideConstructor = value;
_overrideCreator = ((value != null) ? JsonTypeReflector.ReflectionDelegateFactory.CreateParameterizedConstructor(value) : null);
}
}
///
/// Gets or sets the parametrized constructor used to create the object.
///
/// The parametrized constructor.
[Obsolete("ParametrizedConstructor is obsolete. Use OverrideCreator instead.")]
public ConstructorInfo ParametrizedConstructor
{
get
{
return _parametrizedConstructor;
}
set
{
_parametrizedConstructor = value;
_parameterizedCreator = ((value != null) ? JsonTypeReflector.ReflectionDelegateFactory.CreateParameterizedConstructor(value) : null);
}
}
///
/// Gets or sets the function used to create the object. When set this function will override .
/// This function is called with a collection of arguments which are defined by the collection.
///
/// The function used to create the object.
public ObjectConstructor OverrideCreator
{
get
{
return _overrideCreator;
}
set
{
_overrideCreator = value;
_overrideConstructor = null;
}
}
internal ObjectConstructor ParameterizedCreator => _parameterizedCreator;
///
/// Gets or sets the extension data setter.
///
public ExtensionDataSetter ExtensionDataSetter { get; set; }
///
/// Gets or sets the extension data getter.
///
public ExtensionDataGetter ExtensionDataGetter { get; set; }
///
/// Gets or sets the extension data value type.
///
public Type ExtensionDataValueType
{
get
{
return _extensionDataValueType;
}
set
{
_extensionDataValueType = value;
ExtensionDataIsJToken = value != null && typeof(JToken).IsAssignableFrom(value);
}
}
internal bool HasRequiredOrDefaultValueProperties
{
get
{
if (!_hasRequiredOrDefaultValueProperties.HasValue)
{
_hasRequiredOrDefaultValueProperties = false;
if (ItemRequired.GetValueOrDefault(Required.Default) != 0)
{
_hasRequiredOrDefaultValueProperties = true;
}
else
{
foreach (JsonProperty property in Properties)
{
if (property.Required != 0 || ((uint?)property.DefaultValueHandling & 2u) == 2)
{
_hasRequiredOrDefaultValueProperties = true;
break;
}
}
}
}
return _hasRequiredOrDefaultValueProperties.GetValueOrDefault();
}
}
///
/// Initializes a new instance of the class.
///
/// The underlying type for the contract.
public JsonObjectContract(Type underlyingType)
: base(underlyingType)
{
ContractType = JsonContractType.Object;
Properties = new JsonPropertyCollection(base.UnderlyingType);
}
[SecuritySafeCritical]
internal object GetUninitializedObject()
{
if (!JsonTypeReflector.FullyTrusted)
{
throw new JsonException("Insufficient permissions. Creating an uninitialized '{0}' type requires full trust.".FormatWith(CultureInfo.InvariantCulture, NonNullableUnderlyingType));
}
return FormatterServices.GetUninitializedObject(NonNullableUnderlyingType);
}
}
}