#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
using System;
using System.Diagnostics;
using System.Reflection;
using LC.Newtonsoft.Json.Utilities;
#if !HAVE_LINQ
using LC.Newtonsoft.Json.Utilities.LinqBridge;
#endif
namespace LC.Newtonsoft.Json.Serialization
{
///
/// Maps a JSON property to a .NET member or constructor parameter.
///
public class JsonProperty
{
internal Required? _required;
internal bool _hasExplicitDefaultValue;
private object? _defaultValue;
private bool _hasGeneratedDefaultValue;
private string? _propertyName;
internal bool _skipPropertyNameEscape;
private Type? _propertyType;
// use to cache contract during deserialization
internal JsonContract? PropertyContract { get; set; }
///
/// Gets or sets the name of the property.
///
/// The name of the property.
public string? PropertyName
{
get => _propertyName;
set
{
_propertyName = value;
_skipPropertyNameEscape = !JavaScriptUtils.ShouldEscapeJavaScriptString(_propertyName, JavaScriptUtils.HtmlCharEscapeFlags);
}
}
///
/// Gets or sets the type that declared this property.
///
/// The type that declared this property.
public Type? DeclaringType { get; set; }
///
/// Gets or sets the order of serialization of a member.
///
/// The numeric order of serialization.
public int? Order { get; set; }
///
/// Gets or sets the name of the underlying member or parameter.
///
/// The name of the underlying member or parameter.
public string? UnderlyingName { get; set; }
///
/// Gets the that will get and set the during serialization.
///
/// The that will get and set the during serialization.
public IValueProvider? ValueProvider { get; set; }
///
/// Gets or sets the for this property.
///
/// The for this property.
public IAttributeProvider? AttributeProvider { get; set; }
///
/// Gets or sets the type of the property.
///
/// The type of the property.
public Type? PropertyType
{
get => _propertyType;
set
{
if (_propertyType != value)
{
_propertyType = value;
_hasGeneratedDefaultValue = false;
}
}
}
///
/// Gets or sets the for the property.
/// If set this converter takes precedence over the contract converter for the property type.
///
/// The converter.
public JsonConverter? Converter { get; set; }
///
/// Gets or sets the member converter.
///
/// The member converter.
[Obsolete("MemberConverter is obsolete. Use Converter instead.")]
public JsonConverter? MemberConverter
{
get => Converter;
set => Converter = value;
}
///
/// Gets or sets a value indicating whether this is ignored.
///
/// true if ignored; otherwise, false.
public bool Ignored { get; set; }
///
/// Gets or sets a value indicating whether this is readable.
///
/// true if readable; otherwise, false.
public bool Readable { get; set; }
///
/// Gets or sets a value indicating whether this is writable.
///
/// true if writable; otherwise, false.
public bool Writable { get; set; }
///
/// Gets or sets a value indicating whether this has a member attribute.
///
/// true if has a member attribute; otherwise, false.
public bool HasMemberAttribute { get; set; }
///
/// Gets the default value.
///
/// The default value.
public object? DefaultValue
{
get
{
if (!_hasExplicitDefaultValue)
{
return null;
}
return _defaultValue;
}
set
{
_hasExplicitDefaultValue = true;
_defaultValue = value;
}
}
internal object? GetResolvedDefaultValue()
{
if (_propertyType == null)
{
return null;
}
if (!_hasExplicitDefaultValue && !_hasGeneratedDefaultValue)
{
_defaultValue = ReflectionUtils.GetDefaultValue(_propertyType);
_hasGeneratedDefaultValue = true;
}
return _defaultValue;
}
///
/// Gets or sets a value indicating whether this is required.
///
/// A value indicating whether this is required.
public Required Required
{
get => _required ?? Required.Default;
set => _required = value;
}
///
/// Gets a value indicating whether has a value specified.
///
public bool IsRequiredSpecified => _required != null;
///
/// Gets or sets a value indicating whether this property preserves object references.
///
///
/// true if this instance is reference; otherwise, false.
///
public bool? IsReference { get; set; }
///
/// Gets or sets the property null value handling.
///
/// The null value handling.
public NullValueHandling? NullValueHandling { get; set; }
///
/// Gets or sets the property default value handling.
///
/// The default value handling.
public DefaultValueHandling? DefaultValueHandling { get; set; }
///
/// Gets or sets the property reference loop handling.
///
/// The reference loop handling.
public ReferenceLoopHandling? ReferenceLoopHandling { get; set; }
///
/// Gets or sets the property object creation handling.
///
/// The object creation handling.
public ObjectCreationHandling? ObjectCreationHandling { get; set; }
///
/// Gets or sets or sets the type name handling.
///
/// The type name handling.
public TypeNameHandling? TypeNameHandling { get; set; }
///
/// Gets or sets a predicate used to determine whether the property should be serialized.
///
/// A predicate used to determine whether the property should be serialized.
public Predicate