From b65807bbd2be51e8fa5bfebb50af5786b3eeb9ed Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Sat, 18 Feb 2017 21:49:35 +1300 Subject: [PATCH] -Added JsonReaderException ctor with path, line number, line position -Added JsonWriterException ctor with path --- Src/Newtonsoft.Json/Bson/BsonObjectId.cs | 2 +- Src/Newtonsoft.Json/Bson/BsonWriter.cs | 8 +++----- .../Converters/XmlNodeConverter.cs | 2 +- Src/Newtonsoft.Json/JsonConverterAttribute.cs | 2 +- Src/Newtonsoft.Json/JsonReaderException.cs | 19 ++++++++++++++----- Src/Newtonsoft.Json/JsonValidatingReader.cs | 4 ++-- Src/Newtonsoft.Json/JsonWriterException.cs | 13 ++++++++++--- Src/Newtonsoft.Json/Linq/JsonPath/JPath.cs | 2 +- .../Schema/JsonSchemaException.cs | 6 +++--- .../Schema/JsonSchemaGenerator.cs | 4 ++-- Src/Newtonsoft.Json/Schema/JsonSchemaNode.cs | 10 +++++----- .../Serialization/ErrorContext.cs | 8 ++++---- .../Serialization/ErrorEventArgs.cs | 4 ++-- .../Serialization/JsonArrayContract.cs | 8 ++++---- .../Serialization/JsonContract.cs | 2 +- .../Serialization/JsonDictionaryContract.cs | 6 +++--- .../Serialization/JsonDynamicContract.cs | 2 +- .../Serialization/JsonObjectContract.cs | 2 +- .../Utilities/ReflectionObject.cs | 16 +++++++++------- 19 files changed, 68 insertions(+), 52 deletions(-) diff --git a/Src/Newtonsoft.Json/Bson/BsonObjectId.cs b/Src/Newtonsoft.Json/Bson/BsonObjectId.cs index 0bb603eb..3ed885b1 100644 --- a/Src/Newtonsoft.Json/Bson/BsonObjectId.cs +++ b/Src/Newtonsoft.Json/Bson/BsonObjectId.cs @@ -37,7 +37,7 @@ namespace Newtonsoft.Json.Bson /// Gets or sets the value of the Oid. /// /// The value of the Oid. - public byte[] Value { get; private set; } + public byte[] Value { get; } /// /// Initializes a new instance of the class. diff --git a/Src/Newtonsoft.Json/Bson/BsonWriter.cs b/Src/Newtonsoft.Json/Bson/BsonWriter.cs index a3de3a2a..78e86010 100644 --- a/Src/Newtonsoft.Json/Bson/BsonWriter.cs +++ b/Src/Newtonsoft.Json/Bson/BsonWriter.cs @@ -238,7 +238,7 @@ namespace Newtonsoft.Json.Bson #if HAVE_BIG_INTEGER if (value is BigInteger) { - InternalWriteValue(JsonToken.Integer); + SetWriteState(JsonToken.Integer, null); AddToken(new BsonBinary(((BigInteger)value).ToByteArray(), BsonBinaryType.Binary)); } else @@ -516,8 +516,7 @@ namespace Newtonsoft.Json.Bson } // hack to update the writer state - UpdateScopeWithFinishedValue(); - AutoComplete(JsonToken.Undefined); + SetWriteState(JsonToken.Undefined, null); AddValue(value, BsonType.Oid); } @@ -531,8 +530,7 @@ namespace Newtonsoft.Json.Bson ValidationUtils.ArgumentNotNull(pattern, nameof(pattern)); // hack to update the writer state - UpdateScopeWithFinishedValue(); - AutoComplete(JsonToken.Undefined); + SetWriteState(JsonToken.Undefined, null); AddToken(new BsonRegex(pattern, options)); } } diff --git a/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs b/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs index e494ac3d..77e89f96 100644 --- a/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs +++ b/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs @@ -435,7 +435,7 @@ namespace Newtonsoft.Json.Converters #if HAVE_XLINQ internal class XDeclarationWrapper : XObjectWrapper, IXmlDeclaration { - internal XDeclaration Declaration { get; private set; } + internal XDeclaration Declaration { get; } public XDeclarationWrapper(XDeclaration declaration) : base(null) diff --git a/Src/Newtonsoft.Json/JsonConverterAttribute.cs b/Src/Newtonsoft.Json/JsonConverterAttribute.cs index ef9d2c11..4ccceeb9 100644 --- a/Src/Newtonsoft.Json/JsonConverterAttribute.cs +++ b/Src/Newtonsoft.Json/JsonConverterAttribute.cs @@ -50,7 +50,7 @@ namespace Newtonsoft.Json /// The parameter list to use when constructing the described by . /// If null, the default constructor is used. /// - public object[] ConverterParameters { get; private set; } + public object[] ConverterParameters { get; } /// /// Initializes a new instance of the class. diff --git a/Src/Newtonsoft.Json/JsonReaderException.cs b/Src/Newtonsoft.Json/JsonReaderException.cs index 940e14e8..3de4f168 100644 --- a/Src/Newtonsoft.Json/JsonReaderException.cs +++ b/Src/Newtonsoft.Json/JsonReaderException.cs @@ -42,19 +42,19 @@ namespace Newtonsoft.Json /// Gets the line number indicating where the error occurred. /// /// The line number indicating where the error occurred. - public int LineNumber { get; private set; } + public int LineNumber { get; } /// /// Gets the line position indicating where the error occurred. /// /// The line position indicating where the error occurred. - public int LinePosition { get; private set; } + public int LinePosition { get; } /// /// Gets the path to the JSON where the error occurred. /// /// The path to the JSON where the error occurred. - public string Path { get; private set; } + public string Path { get; } /// /// Initializes a new instance of the class. @@ -98,7 +98,16 @@ namespace Newtonsoft.Json } #endif - internal JsonReaderException(string message, Exception innerException, string path, int lineNumber, int linePosition) + /// + /// Initializes a new instance of the class + /// with a specified error message, JSON path, line number, line position, and a reference to the inner exception that is the cause of this exception. + /// + /// The error message that explains the reason for the exception. + /// The path to the JSON where the error occurred. + /// The line number indicating where the error occurred. + /// The line position indicating where the error occurred. + /// The exception that is the cause of the current exception, or null if no inner exception is specified. + public JsonReaderException(string message, string path, int lineNumber, int linePosition, Exception innerException) : base(message, innerException) { Path = path; @@ -133,7 +142,7 @@ namespace Newtonsoft.Json linePosition = 0; } - return new JsonReaderException(message, ex, path, lineNumber, linePosition); + return new JsonReaderException(message, path, lineNumber, linePosition, ex); } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json/JsonValidatingReader.cs b/Src/Newtonsoft.Json/JsonValidatingReader.cs index cca64386..677a49c6 100644 --- a/Src/Newtonsoft.Json/JsonValidatingReader.cs +++ b/Src/Newtonsoft.Json/JsonValidatingReader.cs @@ -62,8 +62,8 @@ namespace Newtonsoft.Json public string CurrentPropertyName { get; set; } public int ArrayItemCount { get; set; } - public bool IsUniqueArray { get; set; } - public IList UniqueArrayItems { get; set; } + public bool IsUniqueArray { get; } + public IList UniqueArrayItems { get; } public JTokenWriter CurrentItemWriter { get; set; } public IList Schemas diff --git a/Src/Newtonsoft.Json/JsonWriterException.cs b/Src/Newtonsoft.Json/JsonWriterException.cs index aed9866a..cbdce1ca 100644 --- a/Src/Newtonsoft.Json/JsonWriterException.cs +++ b/Src/Newtonsoft.Json/JsonWriterException.cs @@ -42,7 +42,7 @@ namespace Newtonsoft.Json /// Gets the path to the JSON where the error occurred. /// /// The path to the JSON where the error occurred. - public string Path { get; private set; } + public string Path { get; } /// /// Initializes a new instance of the class. @@ -86,7 +86,14 @@ namespace Newtonsoft.Json } #endif - internal JsonWriterException(string message, Exception innerException, string path) + /// + /// Initializes a new instance of the class + /// with a specified error message, JSON path and a reference to the inner exception that is the cause of this exception. + /// + /// The error message that explains the reason for the exception. + /// The path to the JSON where the error occurred. + /// The exception that is the cause of the current exception, or null if no inner exception is specified. + public JsonWriterException(string message, string path, Exception innerException) : base(message, innerException) { Path = path; @@ -101,7 +108,7 @@ namespace Newtonsoft.Json { message = JsonPosition.FormatMessage(null, path, message); - return new JsonWriterException(message, ex, path); + return new JsonWriterException(message, path, ex); } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Linq/JsonPath/JPath.cs b/Src/Newtonsoft.Json/Linq/JsonPath/JPath.cs index 4656fc48..0002e0d1 100644 --- a/Src/Newtonsoft.Json/Linq/JsonPath/JPath.cs +++ b/Src/Newtonsoft.Json/Linq/JsonPath/JPath.cs @@ -34,7 +34,7 @@ namespace Newtonsoft.Json.Linq.JsonPath internal class JPath { private readonly string _expression; - public List Filters { get; private set; } + public List Filters { get; } private int _currentIndex; diff --git a/Src/Newtonsoft.Json/Schema/JsonSchemaException.cs b/Src/Newtonsoft.Json/Schema/JsonSchemaException.cs index bf523d29..aa5f2349 100644 --- a/Src/Newtonsoft.Json/Schema/JsonSchemaException.cs +++ b/Src/Newtonsoft.Json/Schema/JsonSchemaException.cs @@ -46,19 +46,19 @@ namespace Newtonsoft.Json.Schema /// Gets the line number indicating where the error occurred. /// /// The line number indicating where the error occurred. - public int LineNumber { get; private set; } + public int LineNumber { get; } /// /// Gets the line position indicating where the error occurred. /// /// The line position indicating where the error occurred. - public int LinePosition { get; private set; } + public int LinePosition { get; } /// /// Gets the path to the JSON where the error occurred. /// /// The path to the JSON where the error occurred. - public string Path { get; private set; } + public string Path { get; } /// /// Initializes a new instance of the class. diff --git a/Src/Newtonsoft.Json/Schema/JsonSchemaGenerator.cs b/Src/Newtonsoft.Json/Schema/JsonSchemaGenerator.cs index 0bbcc3b9..b19c7e07 100644 --- a/Src/Newtonsoft.Json/Schema/JsonSchemaGenerator.cs +++ b/Src/Newtonsoft.Json/Schema/JsonSchemaGenerator.cs @@ -77,8 +77,8 @@ namespace Newtonsoft.Json.Schema private class TypeSchema { - public Type Type { get; private set; } - public JsonSchema Schema { get; private set; } + public Type Type { get; } + public JsonSchema Schema { get; } public TypeSchema(Type type, JsonSchema schema) { diff --git a/Src/Newtonsoft.Json/Schema/JsonSchemaNode.cs b/Src/Newtonsoft.Json/Schema/JsonSchemaNode.cs index cbe9ba2f..4acad1f8 100644 --- a/Src/Newtonsoft.Json/Schema/JsonSchemaNode.cs +++ b/Src/Newtonsoft.Json/Schema/JsonSchemaNode.cs @@ -38,11 +38,11 @@ namespace Newtonsoft.Json.Schema [Obsolete("JSON Schema validation has been moved to its own package. See http://www.newtonsoft.com/jsonschema for more details.")] internal class JsonSchemaNode { - public string Id { get; private set; } - public ReadOnlyCollection Schemas { get; private set; } - public Dictionary Properties { get; private set; } - public Dictionary PatternProperties { get; private set; } - public List Items { get; private set; } + public string Id { get; } + public ReadOnlyCollection Schemas { get; } + public Dictionary Properties { get; } + public Dictionary PatternProperties { get; } + public List Items { get; } public JsonSchemaNode AdditionalProperties { get; set; } public JsonSchemaNode AdditionalItems { get; set; } diff --git a/Src/Newtonsoft.Json/Serialization/ErrorContext.cs b/Src/Newtonsoft.Json/Serialization/ErrorContext.cs index bcb2fbbe..366064a0 100644 --- a/Src/Newtonsoft.Json/Serialization/ErrorContext.cs +++ b/Src/Newtonsoft.Json/Serialization/ErrorContext.cs @@ -46,25 +46,25 @@ namespace Newtonsoft.Json.Serialization /// Gets the error. /// /// The error. - public Exception Error { get; private set; } + public Exception Error { get; } /// /// Gets the original object that caused the error. /// /// The original object that caused the error. - public object OriginalObject { get; private set; } + public object OriginalObject { get; } /// /// Gets the member that caused the error. /// /// The member that caused the error. - public object Member { get; private set; } + public object Member { get; } /// /// Gets the path of the JSON location where the error occurred. /// /// The path of the JSON location where the error occurred. - public string Path { get; private set; } + public string Path { get; } /// /// Gets or sets a value indicating whether this is handled. diff --git a/Src/Newtonsoft.Json/Serialization/ErrorEventArgs.cs b/Src/Newtonsoft.Json/Serialization/ErrorEventArgs.cs index cae7d035..1b16c607 100644 --- a/Src/Newtonsoft.Json/Serialization/ErrorEventArgs.cs +++ b/Src/Newtonsoft.Json/Serialization/ErrorEventArgs.cs @@ -36,13 +36,13 @@ namespace Newtonsoft.Json.Serialization /// Gets the current object the error event is being raised against. /// /// The current object the error event is being raised against. - public object CurrentObject { get; private set; } + public object CurrentObject { get; } /// /// Gets the error context. /// /// The error context. - public ErrorContext ErrorContext { get; private set; } + public ErrorContext ErrorContext { get; } /// /// Initializes a new instance of the class. diff --git a/Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs b/Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs index 96afac5b..c5ab7176 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs @@ -49,13 +49,13 @@ namespace Newtonsoft.Json.Serialization /// Gets the of the collection items. /// /// The of the collection items. - public Type CollectionItemType { get; private set; } + public Type CollectionItemType { get; } /// /// Gets a value indicating whether the collection type is a multidimensional array. /// /// true if the collection type is a multidimensional array; otherwise, false. - public bool IsMultidimensionalArray { get; private set; } + public bool IsMultidimensionalArray { get; } private readonly Type _genericCollectionDefinitionType; @@ -63,8 +63,8 @@ namespace Newtonsoft.Json.Serialization private ObjectConstructor _genericWrapperCreator; private Func _genericTemporaryCollectionCreator; - internal bool IsArray { get; private set; } - internal bool ShouldCreateWrapper { get; private set; } + internal bool IsArray { get; } + internal bool ShouldCreateWrapper { get; } internal bool CanDeserialize { get; private set; } private readonly ConstructorInfo _parameterizedConstructor; diff --git a/Src/Newtonsoft.Json/Serialization/JsonContract.cs b/Src/Newtonsoft.Json/Serialization/JsonContract.cs index 8dbc3df8..fb957095 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonContract.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonContract.cs @@ -101,7 +101,7 @@ namespace Newtonsoft.Json.Serialization /// Gets the underlying type for the contract. /// /// The underlying type for the contract. - public Type UnderlyingType { get; private set; } + public Type UnderlyingType { get; } /// /// Gets or sets the type created during deserialization. diff --git a/Src/Newtonsoft.Json/Serialization/JsonDictionaryContract.cs b/Src/Newtonsoft.Json/Serialization/JsonDictionaryContract.cs index 5236a9a6..4d58beee 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonDictionaryContract.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonDictionaryContract.cs @@ -51,13 +51,13 @@ namespace Newtonsoft.Json.Serialization /// Gets the of the dictionary keys. /// /// The of the dictionary keys. - public Type DictionaryKeyType { get; private set; } + public Type DictionaryKeyType { get; } /// /// Gets the of the dictionary values. /// /// The of the dictionary values. - public Type DictionaryValueType { get; private set; } + public Type DictionaryValueType { get; } internal JsonContract KeyContract { get; set; } @@ -68,7 +68,7 @@ namespace Newtonsoft.Json.Serialization private Func _genericTemporaryDictionaryCreator; - internal bool ShouldCreateWrapper { get; private set; } + internal bool ShouldCreateWrapper { get; } private readonly ConstructorInfo _parameterizedConstructor; diff --git a/Src/Newtonsoft.Json/Serialization/JsonDynamicContract.cs b/Src/Newtonsoft.Json/Serialization/JsonDynamicContract.cs index 5c2d578e..1133f9a3 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonDynamicContract.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonDynamicContract.cs @@ -43,7 +43,7 @@ namespace Newtonsoft.Json.Serialization /// Gets the object's properties. /// /// The object's properties. - public JsonPropertyCollection Properties { get; private set; } + public JsonPropertyCollection Properties { get; } /// /// Gets or sets the property name resolver. diff --git a/Src/Newtonsoft.Json/Serialization/JsonObjectContract.cs b/Src/Newtonsoft.Json/Serialization/JsonObjectContract.cs index e7aecc9f..602951b7 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonObjectContract.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonObjectContract.cs @@ -56,7 +56,7 @@ namespace Newtonsoft.Json.Serialization /// Gets the object's properties. /// /// The object's properties. - public JsonPropertyCollection Properties { get; private set; } + public JsonPropertyCollection Properties { get; } /// /// Gets a collection of instances that define the parameters used with . diff --git a/Src/Newtonsoft.Json/Utilities/ReflectionObject.cs b/Src/Newtonsoft.Json/Utilities/ReflectionObject.cs index ebd166d9..b39a3da9 100644 --- a/Src/Newtonsoft.Json/Utilities/ReflectionObject.cs +++ b/Src/Newtonsoft.Json/Utilities/ReflectionObject.cs @@ -47,12 +47,13 @@ namespace Newtonsoft.Json.Utilities internal class ReflectionObject { - public ObjectConstructor Creator { get; private set; } - public IDictionary Members { get; private set; } + public ObjectConstructor Creator { get; } + public IDictionary Members { get; } - public ReflectionObject() + private ReflectionObject(ObjectConstructor creator) { Members = new Dictionary(); + Creator = creator; } public object GetValue(object target, string member) @@ -79,13 +80,12 @@ namespace Newtonsoft.Json.Utilities public static ReflectionObject Create(Type t, MethodBase creator, params string[] memberNames) { - ReflectionObject d = new ReflectionObject(); - ReflectionDelegateFactory delegateFactory = JsonTypeReflector.ReflectionDelegateFactory; + ObjectConstructor creatorConstructor = null; if (creator != null) { - d.Creator = delegateFactory.CreateParameterizedConstructor(creator); + creatorConstructor = delegateFactory.CreateParameterizedConstructor(creator); } else { @@ -93,10 +93,12 @@ namespace Newtonsoft.Json.Utilities { Func ctor = delegateFactory.CreateDefaultConstructor(t); - d.Creator = args => ctor(); + creatorConstructor = args => ctor(); } } + ReflectionObject d = new ReflectionObject(creatorConstructor); + foreach (string memberName in memberNames) { MemberInfo[] members = t.GetMember(memberName, BindingFlags.Instance | BindingFlags.Public);