Merge branch 'special-properties'
This commit is contained in:
@@ -28,6 +28,7 @@ using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
@@ -356,7 +357,7 @@ namespace Newtonsoft.Json.Bson
|
||||
{
|
||||
case State.ObjectStart:
|
||||
{
|
||||
SetToken(JsonToken.PropertyName, "$ref");
|
||||
SetToken(JsonToken.PropertyName, JsonTypeReflector.RefPropertyName);
|
||||
_bsonReaderState = BsonReaderState.ReferenceRef;
|
||||
return true;
|
||||
}
|
||||
@@ -381,7 +382,7 @@ namespace Newtonsoft.Json.Bson
|
||||
{
|
||||
if (_bsonReaderState == BsonReaderState.ReferenceRef)
|
||||
{
|
||||
SetToken(JsonToken.PropertyName, "$id");
|
||||
SetToken(JsonToken.PropertyName, JsonTypeReflector.IdPropertyName);
|
||||
_bsonReaderState = BsonReaderState.ReferenceId;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Globalization;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
#if NET20
|
||||
using Newtonsoft.Json.Utilities.LinqBridge;
|
||||
@@ -420,7 +421,7 @@ namespace Newtonsoft.Json
|
||||
if (t == JsonToken.Date)
|
||||
{
|
||||
if (Value is DateTime)
|
||||
SetToken(JsonToken.Date, new DateTimeOffset((DateTime)Value));
|
||||
SetToken(JsonToken.Date, new DateTimeOffset((DateTime)Value), false);
|
||||
|
||||
return (DateTimeOffset)Value;
|
||||
}
|
||||
@@ -438,15 +439,21 @@ namespace Newtonsoft.Json
|
||||
return null;
|
||||
}
|
||||
|
||||
if (DateTimeOffset.TryParse(s, Culture, DateTimeStyles.RoundtripKind, out dt))
|
||||
object temp;
|
||||
if (DateTimeUtils.TryParseDateTime(s, DateParseHandling.DateTimeOffset, DateTimeZoneHandling, out temp))
|
||||
{
|
||||
SetToken(JsonToken.Date, dt);
|
||||
dt = (DateTimeOffset)temp;
|
||||
SetToken(JsonToken.Date, dt, false);
|
||||
return dt;
|
||||
}
|
||||
else
|
||||
|
||||
if (DateTimeOffset.TryParse(s, Culture, DateTimeStyles.RoundtripKind, out dt))
|
||||
{
|
||||
throw JsonReaderException.Create(this, "Could not convert string to DateTimeOffset: {0}.".FormatWith(CultureInfo.InvariantCulture, Value));
|
||||
SetToken(JsonToken.Date, dt, false);
|
||||
return dt;
|
||||
}
|
||||
|
||||
throw JsonReaderException.Create(this, "Could not convert string to DateTimeOffset: {0}.".FormatWith(CultureInfo.InvariantCulture, Value));
|
||||
}
|
||||
|
||||
if (t == JsonToken.EndArray)
|
||||
@@ -479,7 +486,7 @@ namespace Newtonsoft.Json
|
||||
{
|
||||
byte[] data = ReadAsBytes();
|
||||
ReadInternal();
|
||||
SetToken(JsonToken.Bytes, data);
|
||||
SetToken(JsonToken.Bytes, data, false);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -488,7 +495,7 @@ namespace Newtonsoft.Json
|
||||
{
|
||||
string s = (string)Value;
|
||||
byte[] data = (s.Length == 0) ? new byte[0] : Convert.FromBase64String(s);
|
||||
SetToken(JsonToken.Bytes, data);
|
||||
SetToken(JsonToken.Bytes, data, false);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -512,7 +519,7 @@ namespace Newtonsoft.Json
|
||||
break;
|
||||
case JsonToken.EndArray:
|
||||
byte[] d = data.ToArray();
|
||||
SetToken(JsonToken.Bytes, d);
|
||||
SetToken(JsonToken.Bytes, d, false);
|
||||
return d;
|
||||
case JsonToken.Comment:
|
||||
// skip
|
||||
@@ -553,7 +560,7 @@ namespace Newtonsoft.Json
|
||||
if (t == JsonToken.Integer || t == JsonToken.Float)
|
||||
{
|
||||
if (!(Value is decimal))
|
||||
SetToken(JsonToken.Float, Convert.ToDecimal(Value, CultureInfo.InvariantCulture));
|
||||
SetToken(JsonToken.Float, Convert.ToDecimal(Value, CultureInfo.InvariantCulture), false);
|
||||
|
||||
return (decimal)Value;
|
||||
}
|
||||
@@ -573,7 +580,7 @@ namespace Newtonsoft.Json
|
||||
|
||||
if (decimal.TryParse(s, NumberStyles.Number, Culture, out d))
|
||||
{
|
||||
SetToken(JsonToken.Float, d);
|
||||
SetToken(JsonToken.Float, d, false);
|
||||
return d;
|
||||
}
|
||||
else
|
||||
@@ -610,7 +617,7 @@ namespace Newtonsoft.Json
|
||||
if (t == JsonToken.Integer || t == JsonToken.Float)
|
||||
{
|
||||
if (!(Value is int))
|
||||
SetToken(JsonToken.Integer, Convert.ToInt32(Value, CultureInfo.InvariantCulture));
|
||||
SetToken(JsonToken.Integer, Convert.ToInt32(Value, CultureInfo.InvariantCulture), false);
|
||||
|
||||
return (int)Value;
|
||||
}
|
||||
@@ -630,7 +637,7 @@ namespace Newtonsoft.Json
|
||||
|
||||
if (int.TryParse(s, NumberStyles.Integer, Culture, out i))
|
||||
{
|
||||
SetToken(JsonToken.Integer, i);
|
||||
SetToken(JsonToken.Integer, i, false);
|
||||
return i;
|
||||
}
|
||||
else
|
||||
@@ -680,7 +687,7 @@ namespace Newtonsoft.Json
|
||||
else
|
||||
s = Value.ToString();
|
||||
|
||||
SetToken(JsonToken.String, s);
|
||||
SetToken(JsonToken.String, s, false);
|
||||
return s;
|
||||
}
|
||||
}
|
||||
@@ -720,16 +727,23 @@ namespace Newtonsoft.Json
|
||||
return null;
|
||||
}
|
||||
|
||||
object temp;
|
||||
if (DateTimeUtils.TryParseDateTime(s, DateParseHandling.DateTime, DateTimeZoneHandling, out temp))
|
||||
{
|
||||
dt = (DateTime)temp;
|
||||
dt = DateTimeUtils.EnsureDateTime(dt, DateTimeZoneHandling);
|
||||
SetToken(JsonToken.Date, dt, false);
|
||||
return dt;
|
||||
}
|
||||
|
||||
if (DateTime.TryParse(s, Culture, DateTimeStyles.RoundtripKind, out dt))
|
||||
{
|
||||
dt = DateTimeUtils.EnsureDateTime(dt, DateTimeZoneHandling);
|
||||
SetToken(JsonToken.Date, dt);
|
||||
SetToken(JsonToken.Date, dt, false);
|
||||
return dt;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw JsonReaderException.Create(this, "Could not convert string to DateTime: {0}.".FormatWith(CultureInfo.InvariantCulture, Value));
|
||||
}
|
||||
|
||||
throw JsonReaderException.Create(this, "Could not convert string to DateTime: {0}.".FormatWith(CultureInfo.InvariantCulture, Value));
|
||||
}
|
||||
|
||||
if (TokenType == JsonToken.EndArray)
|
||||
@@ -747,13 +761,13 @@ namespace Newtonsoft.Json
|
||||
if (!ReadInternal())
|
||||
throw JsonReaderException.Create(this, "Unexpected end when reading bytes.");
|
||||
|
||||
if (Value.ToString() == "$type")
|
||||
if (Value.ToString() == JsonTypeReflector.TypePropertyName)
|
||||
{
|
||||
ReadInternal();
|
||||
if (Value != null && Value.ToString().StartsWith("System.Byte[]"))
|
||||
{
|
||||
ReadInternal();
|
||||
if (Value.ToString() == "$value")
|
||||
if (Value.ToString() == JsonTypeReflector.ValuePropertyName)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@@ -790,7 +804,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="newToken">The new token.</param>
|
||||
protected void SetToken(JsonToken newToken)
|
||||
{
|
||||
SetToken(newToken, null);
|
||||
SetToken(newToken, null, true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -799,6 +813,11 @@ namespace Newtonsoft.Json
|
||||
/// <param name="newToken">The new token.</param>
|
||||
/// <param name="value">The value.</param>
|
||||
protected void SetToken(JsonToken newToken, object value)
|
||||
{
|
||||
SetToken(newToken, value, true);
|
||||
}
|
||||
|
||||
internal void SetToken(JsonToken newToken, object value, bool updateIndex)
|
||||
{
|
||||
_tokenType = newToken;
|
||||
_value = value;
|
||||
@@ -845,7 +864,8 @@ namespace Newtonsoft.Json
|
||||
else
|
||||
SetFinished();
|
||||
|
||||
UpdateScopeWithFinishedValue();
|
||||
if (updateIndex)
|
||||
UpdateScopeWithFinishedValue();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,6 +53,7 @@ namespace Newtonsoft.Json
|
||||
internal NullValueHandling _nullValueHandling;
|
||||
internal DefaultValueHandling _defaultValueHandling;
|
||||
internal ConstructorHandling _constructorHandling;
|
||||
internal SpecialPropertyHandling _specialPropertyHandling;
|
||||
internal JsonConverterCollection _converters;
|
||||
internal IContractResolver _contractResolver;
|
||||
internal ITraceWriter _traceWriter;
|
||||
@@ -257,6 +258,22 @@ namespace Newtonsoft.Json
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets how special properties are used during deserialization.
|
||||
/// </summary>
|
||||
/// <value>The special properties handling.</value>
|
||||
public virtual SpecialPropertyHandling SpecialPropertyHandling
|
||||
{
|
||||
get { return _specialPropertyHandling; }
|
||||
set
|
||||
{
|
||||
if (value < SpecialPropertyHandling.Default || value > SpecialPropertyHandling.ReadAhead)
|
||||
throw new ArgumentOutOfRangeException("value");
|
||||
|
||||
_specialPropertyHandling = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collection <see cref="JsonConverter"/> that will be used during serialization.
|
||||
/// </summary>
|
||||
@@ -426,6 +443,7 @@ namespace Newtonsoft.Json
|
||||
_preserveReferencesHandling = JsonSerializerSettings.DefaultPreserveReferencesHandling;
|
||||
_constructorHandling = JsonSerializerSettings.DefaultConstructorHandling;
|
||||
_typeNameHandling = JsonSerializerSettings.DefaultTypeNameHandling;
|
||||
_specialPropertyHandling = JsonSerializerSettings.DefaultSpecialPropertyHandling;
|
||||
_context = JsonSerializerSettings.DefaultContext;
|
||||
_binder = DefaultSerializationBinder.Instance;
|
||||
|
||||
@@ -515,6 +533,8 @@ namespace Newtonsoft.Json
|
||||
// serializer specific
|
||||
if (settings._typeNameHandling != null)
|
||||
serializer.TypeNameHandling = settings.TypeNameHandling;
|
||||
if (settings._specialPropertyHandling != null)
|
||||
serializer.SpecialPropertyHandling = settings.SpecialPropertyHandling;
|
||||
if (settings._typeNameAssemblyFormat != null)
|
||||
serializer.TypeNameAssemblyFormat = settings.TypeNameAssemblyFormat;
|
||||
if (settings._preserveReferencesHandling != null)
|
||||
|
||||
@@ -45,6 +45,7 @@ namespace Newtonsoft.Json
|
||||
internal const PreserveReferencesHandling DefaultPreserveReferencesHandling = PreserveReferencesHandling.None;
|
||||
internal const ConstructorHandling DefaultConstructorHandling = ConstructorHandling.Default;
|
||||
internal const TypeNameHandling DefaultTypeNameHandling = TypeNameHandling.None;
|
||||
internal const SpecialPropertyHandling DefaultSpecialPropertyHandling = SpecialPropertyHandling.Default;
|
||||
internal const FormatterAssemblyStyle DefaultTypeNameAssemblyFormat = FormatterAssemblyStyle.Simple;
|
||||
internal static readonly StreamingContext DefaultContext;
|
||||
|
||||
@@ -83,6 +84,7 @@ namespace Newtonsoft.Json
|
||||
internal StreamingContext? _context;
|
||||
internal ConstructorHandling? _constructorHandling;
|
||||
internal TypeNameHandling? _typeNameHandling;
|
||||
internal SpecialPropertyHandling? _specialPropertyHandling;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets how reference loops (e.g. a class referencing itself) is handled.
|
||||
@@ -160,6 +162,16 @@ namespace Newtonsoft.Json
|
||||
set { _typeNameHandling = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets how special properties are used during deserialization.
|
||||
/// </summary>
|
||||
/// <value>The special properties handling.</value>
|
||||
public SpecialPropertyHandling SpecialPropertyHandling
|
||||
{
|
||||
get { return _specialPropertyHandling ?? DefaultSpecialPropertyHandling; }
|
||||
set { _specialPropertyHandling = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets how a type name assembly is written and resolved by the serializer.
|
||||
/// </summary>
|
||||
|
||||
@@ -687,7 +687,7 @@ namespace Newtonsoft.Json.Linq
|
||||
break;
|
||||
case JsonToken.StartConstructor:
|
||||
JConstructor constructor = new JConstructor(r.Value.ToString());
|
||||
constructor.SetLineInfo(constructor);
|
||||
constructor.SetLineInfo(lineInfo);
|
||||
parent.Add(constructor);
|
||||
parent = constructor;
|
||||
break;
|
||||
|
||||
@@ -130,6 +130,10 @@ namespace Newtonsoft.Json.Linq
|
||||
|
||||
internal override void InsertItem(int index, JToken item, bool skipParentCheck)
|
||||
{
|
||||
// don't add comments to JProperty
|
||||
if (item != null && item.Type == JTokenType.Comment)
|
||||
return;
|
||||
|
||||
if (Value != null)
|
||||
throw new JsonException("{0} cannot have multiple values.".FormatWith(CultureInfo.InvariantCulture, typeof(JProperty)));
|
||||
|
||||
|
||||
@@ -190,7 +190,11 @@ namespace Newtonsoft.Json.Linq
|
||||
for (int i = 0; i < ancestors.Count; i++)
|
||||
{
|
||||
JToken current = ancestors[i];
|
||||
JToken next = (i + 1 < ancestors.Count) ? ancestors[i + 1] : null;
|
||||
JToken next = null;
|
||||
if (i + 1 < ancestors.Count)
|
||||
next = ancestors[i + 1];
|
||||
else if (ancestors[i].Type == JTokenType.Property)
|
||||
next = ancestors[i];
|
||||
|
||||
if (next != null)
|
||||
{
|
||||
@@ -1636,7 +1640,7 @@ namespace Newtonsoft.Json.Linq
|
||||
/// <returns>An <see cref="JsonReader"/> that can be used to read this token and its descendants.</returns>
|
||||
public JsonReader CreateReader()
|
||||
{
|
||||
return new JTokenReader(this);
|
||||
return new JTokenReader(this, Path);
|
||||
}
|
||||
|
||||
internal static JToken FromObjectInternal(object o, JsonSerializer jsonSerializer)
|
||||
|
||||
@@ -33,9 +33,10 @@ namespace Newtonsoft.Json.Linq
|
||||
/// </summary>
|
||||
public class JTokenReader : JsonReader, IJsonLineInfo
|
||||
{
|
||||
private readonly string _initialPath;
|
||||
private readonly JToken _root;
|
||||
private JToken _parent;
|
||||
private JToken _current;
|
||||
internal JToken _current;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="JTokenReader"/> class.
|
||||
@@ -49,6 +50,12 @@ namespace Newtonsoft.Json.Linq
|
||||
_current = token;
|
||||
}
|
||||
|
||||
internal JTokenReader(JToken token, string initialPath)
|
||||
: this(token)
|
||||
{
|
||||
_initialPath = initialPath;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the next JSON token from the stream as a <see cref="T:Byte[]"/>.
|
||||
/// </summary>
|
||||
@@ -227,7 +234,7 @@ namespace Newtonsoft.Json.Linq
|
||||
SetToken(JsonToken.StartArray);
|
||||
break;
|
||||
case JTokenType.Constructor:
|
||||
SetToken(JsonToken.StartConstructor);
|
||||
SetToken(JsonToken.StartConstructor, ((JConstructor)token).Name);
|
||||
break;
|
||||
case JTokenType.Property:
|
||||
SetToken(JsonToken.PropertyName, ((JProperty)token).Name);
|
||||
@@ -319,5 +326,30 @@ namespace Newtonsoft.Json.Linq
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the path of the current JSON token.
|
||||
/// </summary>
|
||||
public override string Path
|
||||
{
|
||||
get
|
||||
{
|
||||
string path = base.Path;
|
||||
|
||||
if (!string.IsNullOrEmpty(_initialPath))
|
||||
{
|
||||
if (string.IsNullOrEmpty(path))
|
||||
return _initialPath;
|
||||
|
||||
if (_initialPath.EndsWith("]", StringComparison.OrdinalIgnoreCase)
|
||||
|| path.StartsWith("[", StringComparison.OrdinalIgnoreCase))
|
||||
path = _initialPath + path;
|
||||
else
|
||||
path = _initialPath + "." + path;
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -126,6 +126,7 @@
|
||||
<Compile Include="Serialization\MemoryTraceWriter.cs" />
|
||||
<Compile Include="Serialization\TraceJsonReader.cs" />
|
||||
<Compile Include="Serialization\TraceJsonWriter.cs" />
|
||||
<Compile Include="SpecialPropertyHandling.cs" />
|
||||
<Compile Include="StringEscapeHandling.cs" />
|
||||
<Compile Include="Utilities\DateTimeParser.cs" />
|
||||
<Compile Include="Utilities\DynamicProxy.cs" />
|
||||
|
||||
@@ -137,6 +137,7 @@
|
||||
<Compile Include="Serialization\MemoryTraceWriter.cs" />
|
||||
<Compile Include="Serialization\TraceJsonReader.cs" />
|
||||
<Compile Include="Serialization\TraceJsonWriter.cs" />
|
||||
<Compile Include="SpecialPropertyHandling.cs" />
|
||||
<Compile Include="StringEscapeHandling.cs" />
|
||||
<Compile Include="TraceLevel.cs" />
|
||||
<Compile Include="Utilities\DateTimeParser.cs" />
|
||||
|
||||
@@ -137,6 +137,7 @@
|
||||
<Compile Include="Serialization\MemoryTraceWriter.cs" />
|
||||
<Compile Include="Serialization\TraceJsonReader.cs" />
|
||||
<Compile Include="Serialization\TraceJsonWriter.cs" />
|
||||
<Compile Include="SpecialPropertyHandling.cs" />
|
||||
<Compile Include="StringEscapeHandling.cs" />
|
||||
<Compile Include="Utilities\DateTimeParser.cs" />
|
||||
<Compile Include="Utilities\DynamicProxy.cs" />
|
||||
|
||||
@@ -192,6 +192,7 @@
|
||||
<Compile Include="Serialization\ReflectionValueProvider.cs" />
|
||||
<Compile Include="Serialization\TraceJsonReader.cs" />
|
||||
<Compile Include="Serialization\TraceJsonWriter.cs" />
|
||||
<Compile Include="SpecialPropertyHandling.cs" />
|
||||
<Compile Include="StringEscapeHandling.cs" />
|
||||
<Compile Include="TraceLevel.cs" />
|
||||
<Compile Include="TypeNameHandling.cs" />
|
||||
|
||||
@@ -197,6 +197,7 @@
|
||||
<Compile Include="Serialization\ReflectionValueProvider.cs" />
|
||||
<Compile Include="Serialization\TraceJsonReader.cs" />
|
||||
<Compile Include="Serialization\TraceJsonWriter.cs" />
|
||||
<Compile Include="SpecialPropertyHandling.cs" />
|
||||
<Compile Include="StringEscapeHandling.cs" />
|
||||
<Compile Include="TraceLevel.cs" />
|
||||
<Compile Include="TypeNameHandling.cs" />
|
||||
|
||||
@@ -136,6 +136,7 @@
|
||||
<Compile Include="MissingMemberHandling.cs" />
|
||||
<Compile Include="NullValueHandling.cs" />
|
||||
<Compile Include="ObjectCreationHandling.cs" />
|
||||
<Compile Include="SpecialPropertyHandling.cs" />
|
||||
<Compile Include="PreserveReferencesHandling.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="ReferenceLoopHandling.cs" />
|
||||
|
||||
@@ -91,5 +91,5 @@ using System.Security;
|
||||
// by using the '*' as shown below:
|
||||
|
||||
[assembly: AssemblyVersion("6.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("6.0.1.16929")]
|
||||
[assembly: AssemblyFileVersion("6.0.1.17101")]
|
||||
[assembly: CLSCompliant(true)]
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
#if NET20
|
||||
using Newtonsoft.Json.Utilities.LinqBridge;
|
||||
#else
|
||||
@@ -194,7 +195,7 @@ namespace Newtonsoft.Json.Schema
|
||||
throw JsonException.Create(token, token.Path, "Expected object while parsing schema object, got {0}.".FormatWith(CultureInfo.InvariantCulture, token.Type));
|
||||
|
||||
JToken referenceToken;
|
||||
if (schemaObject.TryGetValue(JsonSchemaConstants.ReferencePropertyName, out referenceToken))
|
||||
if (schemaObject.TryGetValue(JsonTypeReflector.RefPropertyName, out referenceToken))
|
||||
{
|
||||
JsonSchema deferredSchema = new JsonSchema();
|
||||
deferredSchema.DeferredReference = (string)referenceToken;
|
||||
|
||||
@@ -63,8 +63,6 @@ namespace Newtonsoft.Json.Schema
|
||||
public const string OptionValuePropertyName = "value";
|
||||
public const string OptionLabelPropertyName = "label";
|
||||
|
||||
public const string ReferencePropertyName = "$ref";
|
||||
|
||||
public static readonly IDictionary<string, JsonSchemaType> JsonSchemaTypeMapping = new Dictionary<string, JsonSchemaType>
|
||||
{
|
||||
{ "string", JsonSchemaType.String },
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
|
||||
using System.Collections.Generic;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
#if NET20
|
||||
using Newtonsoft.Json.Utilities.LinqBridge;
|
||||
@@ -52,7 +53,7 @@ namespace Newtonsoft.Json.Schema
|
||||
if (schema.Id != null && _resolver.GetSchema(schema.Id) != null)
|
||||
{
|
||||
_writer.WriteStartObject();
|
||||
_writer.WritePropertyName(JsonSchemaConstants.ReferencePropertyName);
|
||||
_writer.WritePropertyName(JsonTypeReflector.RefPropertyName);
|
||||
_writer.WriteValue(schema.Id);
|
||||
_writer.WriteEndObject();
|
||||
}
|
||||
|
||||
@@ -227,20 +227,33 @@ namespace Newtonsoft.Json.Serialization
|
||||
ValidationUtils.ArgumentNotNull(reader, "reader");
|
||||
|
||||
// this is needed because we've already read inside the object, looking for special properties
|
||||
JToken token;
|
||||
using (JTokenWriter writer = new JTokenWriter())
|
||||
{
|
||||
writer.WriteStartObject();
|
||||
|
||||
if (reader.TokenType == JsonToken.PropertyName)
|
||||
writer.WriteToken(reader, reader.Depth - 1, true, true);
|
||||
else
|
||||
writer.WriteEndObject();
|
||||
do
|
||||
{
|
||||
if (reader.TokenType == JsonToken.PropertyName)
|
||||
{
|
||||
string propertyName = (string)reader.Value;
|
||||
if (!reader.Read())
|
||||
break;
|
||||
|
||||
token = writer.Token;
|
||||
if (CheckPropertyName(reader, propertyName))
|
||||
continue;
|
||||
|
||||
writer.WritePropertyName(propertyName);
|
||||
writer.WriteToken(reader, true, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.WriteEndObject();
|
||||
return writer.Token;
|
||||
}
|
||||
} while (reader.Read());
|
||||
|
||||
throw JsonSerializationException.Create(reader, "Unexpected end when deserializing object.");
|
||||
}
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
private object CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, object existingValue)
|
||||
@@ -252,8 +265,8 @@ namespace Newtonsoft.Json.Serialization
|
||||
{
|
||||
switch (reader.TokenType)
|
||||
{
|
||||
// populate a typed object or generic dictionary/array
|
||||
// depending upon whether an objectType was supplied
|
||||
// populate a typed object or generic dictionary/array
|
||||
// depending upon whether an objectType was supplied
|
||||
case JsonToken.StartObject:
|
||||
return CreateObject(reader, objectType, contract, member, containerContract, containerMember, existingValue);
|
||||
case JsonToken.StartArray:
|
||||
@@ -359,12 +372,32 @@ namespace Newtonsoft.Json.Serialization
|
||||
|
||||
private object CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, object existingValue)
|
||||
{
|
||||
CheckedRead(reader);
|
||||
|
||||
string id;
|
||||
object newValue;
|
||||
if (ReadSpecialProperties(reader, ref objectType, ref contract, member, containerContract, containerMember, existingValue, out newValue, out id))
|
||||
return newValue;
|
||||
|
||||
if (Serializer.SpecialPropertyHandling == SpecialPropertyHandling.ReadAhead)
|
||||
{
|
||||
var tokenReader = reader as JTokenReader;
|
||||
if (tokenReader == null)
|
||||
{
|
||||
JToken t = JToken.ReadFrom(reader);
|
||||
tokenReader = (JTokenReader)t.CreateReader();
|
||||
|
||||
// start
|
||||
CheckedRead(tokenReader);
|
||||
|
||||
reader = tokenReader;
|
||||
}
|
||||
|
||||
if (ReadSpecialPropertiesToken(tokenReader, ref objectType, ref contract, member, containerContract, containerMember, existingValue, out newValue, out id))
|
||||
return newValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
CheckedRead(reader);
|
||||
if (ReadSpecialProperties(reader, ref objectType, ref contract, member, containerContract, containerMember, existingValue, out newValue, out id))
|
||||
return newValue;
|
||||
}
|
||||
|
||||
if (HasNoDefinedType(contract))
|
||||
return CreateJObject(reader);
|
||||
@@ -473,6 +506,90 @@ To fix this error either change the JSON to a {1} or change the deserialized typ
|
||||
".FormatWith(CultureInfo.InvariantCulture, objectType, GetExpectedDescription(contract)));
|
||||
}
|
||||
|
||||
private bool ReadSpecialPropertiesToken(JTokenReader reader, ref Type objectType, ref JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, object existingValue, out object newValue, out string id)
|
||||
{
|
||||
id = null;
|
||||
newValue = null;
|
||||
|
||||
if (reader.TokenType == JsonToken.StartObject)
|
||||
{
|
||||
JObject current = (JObject)reader._current;
|
||||
|
||||
JToken refToken = current[JsonTypeReflector.RefPropertyName];
|
||||
if (refToken != null)
|
||||
{
|
||||
if (refToken.Type != JTokenType.String && refToken.Type != JTokenType.Null)
|
||||
throw JsonSerializationException.Create(refToken, refToken.Path, "JSON reference {0} property must have a string or null value.".FormatWith(CultureInfo.InvariantCulture, JsonTypeReflector.RefPropertyName), null);
|
||||
|
||||
JToken property = refToken.Parent;
|
||||
JToken additionalContent = null;
|
||||
if (property.Next != null)
|
||||
additionalContent = property.Next;
|
||||
else if (property.Previous != null)
|
||||
additionalContent = property.Previous;
|
||||
|
||||
string reference = (string)refToken;
|
||||
|
||||
if (reference != null)
|
||||
{
|
||||
if (additionalContent != null)
|
||||
throw JsonSerializationException.Create(additionalContent, additionalContent.Path, "Additional content found in JSON reference object. A JSON reference object should only have a {0} property.".FormatWith(CultureInfo.InvariantCulture, JsonTypeReflector.RefPropertyName), null);
|
||||
|
||||
newValue = Serializer.GetReferenceResolver().ResolveReference(this, reference);
|
||||
|
||||
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Info)
|
||||
TraceWriter.Trace(TraceLevel.Info, JsonPosition.FormatMessage(reader as IJsonLineInfo, reader.Path, "Resolved object reference '{0}' to {1}.".FormatWith(CultureInfo.InvariantCulture, reference, newValue.GetType())), null);
|
||||
|
||||
reader.Skip();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
JToken typeToken = current[JsonTypeReflector.TypePropertyName];
|
||||
if (typeToken != null)
|
||||
{
|
||||
string qualifiedTypeName = (string)typeToken;
|
||||
JsonReader typeTokenReader = typeToken.CreateReader();
|
||||
CheckedRead(typeTokenReader);
|
||||
ResolveTypeName(typeTokenReader, ref objectType, ref contract, member, containerContract, containerMember, qualifiedTypeName);
|
||||
|
||||
JToken valueToken = current[JsonTypeReflector.ValuePropertyName];
|
||||
if (valueToken != null)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
CheckedRead(reader);
|
||||
if (reader.TokenType == JsonToken.PropertyName)
|
||||
{
|
||||
if ((string)reader.Value == JsonTypeReflector.ValuePropertyName)
|
||||
return false;
|
||||
}
|
||||
|
||||
CheckedRead(reader);
|
||||
reader.Skip();
|
||||
}
|
||||
}
|
||||
}
|
||||
JToken idToken = current[JsonTypeReflector.IdPropertyName];
|
||||
if (idToken != null)
|
||||
{
|
||||
id = (string)idToken;
|
||||
}
|
||||
JToken valuesToken = current[JsonTypeReflector.ArrayValuesPropertyName];
|
||||
if (valuesToken != null)
|
||||
{
|
||||
JsonReader listReader = valuesToken.CreateReader();
|
||||
CheckedRead(listReader);
|
||||
newValue = CreateList(listReader, objectType, contract, member, existingValue, id);
|
||||
|
||||
reader.Skip();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
CheckedRead(reader);
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool ReadSpecialProperties(JsonReader reader, ref Type objectType, ref JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, object existingValue, out object newValue, out string id)
|
||||
{
|
||||
id = null;
|
||||
@@ -524,44 +641,7 @@ To fix this error either change the JSON to a {1} or change the deserialized typ
|
||||
CheckedRead(reader);
|
||||
string qualifiedTypeName = reader.Value.ToString();
|
||||
|
||||
TypeNameHandling resolvedTypeNameHandling =
|
||||
((member != null) ? member.TypeNameHandling : null)
|
||||
?? ((containerContract != null) ? containerContract.ItemTypeNameHandling : null)
|
||||
?? ((containerMember != null) ? containerMember.ItemTypeNameHandling : null)
|
||||
?? Serializer._typeNameHandling;
|
||||
|
||||
if (resolvedTypeNameHandling != TypeNameHandling.None)
|
||||
{
|
||||
string typeName;
|
||||
string assemblyName;
|
||||
ReflectionUtils.SplitFullyQualifiedTypeName(qualifiedTypeName, out typeName, out assemblyName);
|
||||
|
||||
Type specifiedType;
|
||||
try
|
||||
{
|
||||
specifiedType = Serializer._binder.BindToType(assemblyName, typeName);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw JsonSerializationException.Create(reader, "Error resolving type specified in JSON '{0}'.".FormatWith(CultureInfo.InvariantCulture, qualifiedTypeName), ex);
|
||||
}
|
||||
|
||||
if (specifiedType == null)
|
||||
throw JsonSerializationException.Create(reader, "Type specified in JSON '{0}' was not resolved.".FormatWith(CultureInfo.InvariantCulture, qualifiedTypeName));
|
||||
|
||||
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Verbose)
|
||||
TraceWriter.Trace(TraceLevel.Verbose, JsonPosition.FormatMessage(reader as IJsonLineInfo, reader.Path, "Resolved type '{0}' to {1}.".FormatWith(CultureInfo.InvariantCulture, qualifiedTypeName, specifiedType)), null);
|
||||
|
||||
if (objectType != null
|
||||
#if !(NET35 || NET20 || PORTABLE40)
|
||||
&& objectType != typeof(IDynamicMetaObjectProvider)
|
||||
#endif
|
||||
&& !objectType.IsAssignableFrom(specifiedType))
|
||||
throw JsonSerializationException.Create(reader, "Type specified in JSON '{0}' is not compatible with '{1}'.".FormatWith(CultureInfo.InvariantCulture, specifiedType.AssemblyQualifiedName, objectType.AssemblyQualifiedName));
|
||||
|
||||
objectType = specifiedType;
|
||||
contract = GetContractSafe(specifiedType);
|
||||
}
|
||||
ResolveTypeName(reader, ref objectType, ref contract, member, containerContract, containerMember, qualifiedTypeName);
|
||||
|
||||
CheckedRead(reader);
|
||||
|
||||
@@ -595,6 +675,48 @@ To fix this error either change the JSON to a {1} or change the deserialized typ
|
||||
return false;
|
||||
}
|
||||
|
||||
private void ResolveTypeName(JsonReader reader, ref Type objectType, ref JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, string qualifiedTypeName)
|
||||
{
|
||||
TypeNameHandling resolvedTypeNameHandling =
|
||||
((member != null) ? member.TypeNameHandling : null)
|
||||
?? ((containerContract != null) ? containerContract.ItemTypeNameHandling : null)
|
||||
?? ((containerMember != null) ? containerMember.ItemTypeNameHandling : null)
|
||||
?? Serializer._typeNameHandling;
|
||||
|
||||
if (resolvedTypeNameHandling != TypeNameHandling.None)
|
||||
{
|
||||
string typeName;
|
||||
string assemblyName;
|
||||
ReflectionUtils.SplitFullyQualifiedTypeName(qualifiedTypeName, out typeName, out assemblyName);
|
||||
|
||||
Type specifiedType;
|
||||
try
|
||||
{
|
||||
specifiedType = Serializer._binder.BindToType(assemblyName, typeName);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw JsonSerializationException.Create(reader, "Error resolving type specified in JSON '{0}'.".FormatWith(CultureInfo.InvariantCulture, qualifiedTypeName), ex);
|
||||
}
|
||||
|
||||
if (specifiedType == null)
|
||||
throw JsonSerializationException.Create(reader, "Type specified in JSON '{0}' was not resolved.".FormatWith(CultureInfo.InvariantCulture, qualifiedTypeName));
|
||||
|
||||
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Verbose)
|
||||
TraceWriter.Trace(TraceLevel.Verbose, JsonPosition.FormatMessage(reader as IJsonLineInfo, reader.Path, "Resolved type '{0}' to {1}.".FormatWith(CultureInfo.InvariantCulture, qualifiedTypeName, specifiedType)), null);
|
||||
|
||||
if (objectType != null
|
||||
#if !(NET35 || NET20 || PORTABLE40)
|
||||
&& objectType != typeof(IDynamicMetaObjectProvider)
|
||||
#endif
|
||||
&& !objectType.IsAssignableFrom(specifiedType))
|
||||
throw JsonSerializationException.Create(reader, "Type specified in JSON '{0}' is not compatible with '{1}'.".FormatWith(CultureInfo.InvariantCulture, specifiedType.AssemblyQualifiedName, objectType.AssemblyQualifiedName));
|
||||
|
||||
objectType = specifiedType;
|
||||
contract = GetContractSafe(specifiedType);
|
||||
}
|
||||
}
|
||||
|
||||
private JsonArrayContract EnsureArrayContract(JsonReader reader, Type objectType, JsonContract contract)
|
||||
{
|
||||
if (contract == null)
|
||||
@@ -997,6 +1119,8 @@ To fix this error either change the JSON to a {1} or change the deserialized typ
|
||||
{
|
||||
case JsonToken.PropertyName:
|
||||
object keyValue = reader.Value;
|
||||
if (CheckPropertyName(reader, keyValue.ToString()))
|
||||
continue;
|
||||
try
|
||||
{
|
||||
try
|
||||
@@ -1751,6 +1875,9 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
{
|
||||
string memberName = reader.Value.ToString();
|
||||
|
||||
if (CheckPropertyName(reader, memberName))
|
||||
continue;
|
||||
|
||||
try
|
||||
{
|
||||
// attempt exact case match first
|
||||
@@ -1793,8 +1920,8 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
else
|
||||
throw;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case JsonToken.EndObject:
|
||||
finished = true;
|
||||
break;
|
||||
@@ -1815,6 +1942,23 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
return newObject;
|
||||
}
|
||||
|
||||
private bool CheckPropertyName(JsonReader reader, string memberName)
|
||||
{
|
||||
if (Serializer.SpecialPropertyHandling == SpecialPropertyHandling.ReadAhead)
|
||||
{
|
||||
switch (memberName)
|
||||
{
|
||||
case JsonTypeReflector.IdPropertyName:
|
||||
case JsonTypeReflector.RefPropertyName:
|
||||
case JsonTypeReflector.TypePropertyName:
|
||||
case JsonTypeReflector.ArrayValuesPropertyName:
|
||||
reader.Skip();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void SetExtensionData(JsonObjectContract contract, JsonProperty member, JsonReader reader, string memberName, object o)
|
||||
{
|
||||
if (contract.ExtensionDataSetter != null)
|
||||
|
||||
@@ -108,6 +108,12 @@ namespace Newtonsoft.Json.Serialization
|
||||
set { _serializer.TypeNameHandling = value; }
|
||||
}
|
||||
|
||||
public override SpecialPropertyHandling SpecialPropertyHandling
|
||||
{
|
||||
get { return _serializer.SpecialPropertyHandling; }
|
||||
set { _serializer.SpecialPropertyHandling = value; }
|
||||
}
|
||||
|
||||
public override FormatterAssemblyStyle TypeNameAssemblyFormat
|
||||
{
|
||||
get { return _serializer.TypeNameAssemblyFormat; }
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
#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.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Newtonsoft.Json
|
||||
{
|
||||
/// <summary>
|
||||
/// Specifies special property handling options for the <see cref="JsonSerializer"/>.
|
||||
/// </summary>
|
||||
public enum SpecialPropertyHandling
|
||||
{
|
||||
/// <summary>
|
||||
/// Read special properties located at the start of a JSON object.
|
||||
/// </summary>
|
||||
Default = 0,
|
||||
|
||||
/// <summary>
|
||||
/// Read special properties located anywhere in a JSON object. Note that this setting will impact performance.
|
||||
/// </summary>
|
||||
ReadAhead = 1,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user