2021-03-29 14:54:12 +08:00
|
|
|
#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.Reflection;
|
|
|
|
using LC.Newtonsoft.Json.Utilities;
|
|
|
|
|
|
|
|
#if !HAVE_LINQ
|
|
|
|
using LC.Newtonsoft.Json.Utilities.LinqBridge;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace LC.Newtonsoft.Json.Serialization
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Maps a JSON property to a .NET member or constructor parameter.
|
|
|
|
/// </summary>
|
|
|
|
public class JsonProperty
|
|
|
|
{
|
|
|
|
internal Required? _required;
|
|
|
|
internal bool _hasExplicitDefaultValue;
|
|
|
|
|
2021-03-30 10:54:25 +08:00
|
|
|
private object _defaultValue;
|
2021-03-29 14:54:12 +08:00
|
|
|
private bool _hasGeneratedDefaultValue;
|
2021-03-30 10:54:25 +08:00
|
|
|
private string _propertyName;
|
2021-03-29 14:54:12 +08:00
|
|
|
internal bool _skipPropertyNameEscape;
|
2021-03-30 10:54:25 +08:00
|
|
|
private Type _propertyType;
|
2021-03-29 14:54:12 +08:00
|
|
|
|
|
|
|
// use to cache contract during deserialization
|
2021-03-30 10:54:25 +08:00
|
|
|
internal JsonContract PropertyContract { get; set; }
|
2021-03-29 14:54:12 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the name of the property.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The name of the property.</value>
|
2021-03-30 10:54:25 +08:00
|
|
|
public string PropertyName
|
2021-03-29 14:54:12 +08:00
|
|
|
{
|
|
|
|
get => _propertyName;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_propertyName = value;
|
|
|
|
_skipPropertyNameEscape = !JavaScriptUtils.ShouldEscapeJavaScriptString(_propertyName, JavaScriptUtils.HtmlCharEscapeFlags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the type that declared this property.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The type that declared this property.</value>
|
2021-03-30 10:54:25 +08:00
|
|
|
public Type DeclaringType { get; set; }
|
2021-03-29 14:54:12 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the order of serialization of a member.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The numeric order of serialization.</value>
|
|
|
|
public int? Order { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the name of the underlying member or parameter.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The name of the underlying member or parameter.</value>
|
2021-03-30 10:54:25 +08:00
|
|
|
public string UnderlyingName { get; set; }
|
2021-03-29 14:54:12 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the <see cref="IValueProvider"/> that will get and set the <see cref="JsonProperty"/> during serialization.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The <see cref="IValueProvider"/> that will get and set the <see cref="JsonProperty"/> during serialization.</value>
|
2021-03-30 10:54:25 +08:00
|
|
|
public IValueProvider ValueProvider { get; set; }
|
2021-03-29 14:54:12 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the <see cref="IAttributeProvider"/> for this property.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The <see cref="IAttributeProvider"/> for this property.</value>
|
2021-03-30 10:54:25 +08:00
|
|
|
public IAttributeProvider AttributeProvider { get; set; }
|
2021-03-29 14:54:12 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the type of the property.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The type of the property.</value>
|
2021-03-30 10:54:25 +08:00
|
|
|
public Type PropertyType
|
2021-03-29 14:54:12 +08:00
|
|
|
{
|
|
|
|
get => _propertyType;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (_propertyType != value)
|
|
|
|
{
|
|
|
|
_propertyType = value;
|
|
|
|
_hasGeneratedDefaultValue = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the <see cref="JsonConverter" /> for the property.
|
|
|
|
/// If set this converter takes precedence over the contract converter for the property type.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The converter.</value>
|
2021-03-30 10:54:25 +08:00
|
|
|
public JsonConverter Converter { get; set; }
|
2021-03-29 14:54:12 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the member converter.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The member converter.</value>
|
|
|
|
[Obsolete("MemberConverter is obsolete. Use Converter instead.")]
|
2021-03-30 10:54:25 +08:00
|
|
|
public JsonConverter MemberConverter
|
2021-03-29 14:54:12 +08:00
|
|
|
{
|
|
|
|
get => Converter;
|
|
|
|
set => Converter = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets a value indicating whether this <see cref="JsonProperty"/> is ignored.
|
|
|
|
/// </summary>
|
|
|
|
/// <value><c>true</c> if ignored; otherwise, <c>false</c>.</value>
|
|
|
|
public bool Ignored { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets a value indicating whether this <see cref="JsonProperty"/> is readable.
|
|
|
|
/// </summary>
|
|
|
|
/// <value><c>true</c> if readable; otherwise, <c>false</c>.</value>
|
|
|
|
public bool Readable { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets a value indicating whether this <see cref="JsonProperty"/> is writable.
|
|
|
|
/// </summary>
|
|
|
|
/// <value><c>true</c> if writable; otherwise, <c>false</c>.</value>
|
|
|
|
public bool Writable { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets a value indicating whether this <see cref="JsonProperty"/> has a member attribute.
|
|
|
|
/// </summary>
|
|
|
|
/// <value><c>true</c> if has a member attribute; otherwise, <c>false</c>.</value>
|
|
|
|
public bool HasMemberAttribute { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the default value.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The default value.</value>
|
2021-03-30 10:54:25 +08:00
|
|
|
public object DefaultValue
|
2021-03-29 14:54:12 +08:00
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (!_hasExplicitDefaultValue)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _defaultValue;
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_hasExplicitDefaultValue = true;
|
|
|
|
_defaultValue = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 10:54:25 +08:00
|
|
|
internal object GetResolvedDefaultValue()
|
2021-03-29 14:54:12 +08:00
|
|
|
{
|
|
|
|
if (_propertyType == null)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_hasExplicitDefaultValue && !_hasGeneratedDefaultValue)
|
|
|
|
{
|
2021-03-30 10:54:25 +08:00
|
|
|
_defaultValue = ReflectionUtils.GetDefaultValue(PropertyType);
|
2021-03-29 14:54:12 +08:00
|
|
|
_hasGeneratedDefaultValue = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _defaultValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets a value indicating whether this <see cref="JsonProperty"/> is required.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>A value indicating whether this <see cref="JsonProperty"/> is required.</value>
|
|
|
|
public Required Required
|
|
|
|
{
|
|
|
|
get => _required ?? Required.Default;
|
|
|
|
set => _required = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a value indicating whether <see cref="Required"/> has a value specified.
|
|
|
|
/// </summary>
|
|
|
|
public bool IsRequiredSpecified => _required != null;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets a value indicating whether this property preserves object references.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>
|
|
|
|
/// <c>true</c> if this instance is reference; otherwise, <c>false</c>.
|
|
|
|
/// </value>
|
|
|
|
public bool? IsReference { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the property null value handling.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The null value handling.</value>
|
|
|
|
public NullValueHandling? NullValueHandling { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the property default value handling.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The default value handling.</value>
|
|
|
|
public DefaultValueHandling? DefaultValueHandling { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the property reference loop handling.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The reference loop handling.</value>
|
|
|
|
public ReferenceLoopHandling? ReferenceLoopHandling { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the property object creation handling.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The object creation handling.</value>
|
|
|
|
public ObjectCreationHandling? ObjectCreationHandling { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets or sets the type name handling.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The type name handling.</value>
|
|
|
|
public TypeNameHandling? TypeNameHandling { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets a predicate used to determine whether the property should be serialized.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>A predicate used to determine whether the property should be serialized.</value>
|
2021-03-30 10:54:25 +08:00
|
|
|
public Predicate<object> ShouldSerialize { get; set; }
|
2021-03-29 14:54:12 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets a predicate used to determine whether the property should be deserialized.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>A predicate used to determine whether the property should be deserialized.</value>
|
2021-03-30 10:54:25 +08:00
|
|
|
public Predicate<object> ShouldDeserialize { get; set; }
|
2021-03-29 14:54:12 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets a predicate used to determine whether the property should be serialized.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>A predicate used to determine whether the property should be serialized.</value>
|
2021-03-30 10:54:25 +08:00
|
|
|
public Predicate<object> GetIsSpecified { get; set; }
|
2021-03-29 14:54:12 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets an action used to set whether the property has been deserialized.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>An action used to set whether the property has been deserialized.</value>
|
2021-03-30 10:54:25 +08:00
|
|
|
public Action<object, object> SetIsSpecified { get; set; }
|
2021-03-29 14:54:12 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns a <see cref="String"/> that represents this instance.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>
|
|
|
|
/// A <see cref="String"/> that represents this instance.
|
|
|
|
/// </returns>
|
|
|
|
public override string ToString()
|
|
|
|
{
|
2021-03-30 10:54:25 +08:00
|
|
|
return PropertyName;
|
2021-03-29 14:54:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the converter used when serializing the property's collection items.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The collection's items converter.</value>
|
2021-03-30 10:54:25 +08:00
|
|
|
public JsonConverter ItemConverter { get; set; }
|
2021-03-29 14:54:12 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets whether this property's collection items are serialized as a reference.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>Whether this property's collection items are serialized as a reference.</value>
|
|
|
|
public bool? ItemIsReference { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the type name handling used when serializing the property's collection items.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The collection's items type name handling.</value>
|
|
|
|
public TypeNameHandling? ItemTypeNameHandling { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the reference loop handling used when serializing the property's collection items.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The collection's items reference loop handling.</value>
|
|
|
|
public ReferenceLoopHandling? ItemReferenceLoopHandling { get; set; }
|
|
|
|
|
|
|
|
internal void WritePropertyName(JsonWriter writer)
|
|
|
|
{
|
|
|
|
if (_skipPropertyNameEscape)
|
|
|
|
{
|
2021-03-30 10:54:25 +08:00
|
|
|
writer.WritePropertyName(PropertyName, false);
|
2021-03-29 14:54:12 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-30 10:54:25 +08:00
|
|
|
writer.WritePropertyName(PropertyName);
|
2021-03-29 14:54:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|