2 // Copyright (c) 2007 James Newton-King
4 // Permission is hereby granted, free of charge, to any person
5 // obtaining a copy of this software and associated documentation
6 // files (the "Software"), to deal in the Software without
7 // restriction, including without limitation the rights to use,
8 // copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the
10 // Software is furnished to do so, subject to the following
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 // OTHER DEALINGS IN THE SOFTWARE.
27 using System.Collections.Generic;
29 using System.Runtime.CompilerServices;
30 using Newtonsoft.Json.Utilities;
32 namespace Newtonsoft.Json.Serialization
34 internal abstract class JsonSerializerInternalBase
36 private class ReferenceEqualsEqualityComparer : IEqualityComparer<object>
38 bool IEqualityComparer<object>.Equals(object x, object y)
40 return ReferenceEquals(x, y);
43 int IEqualityComparer<object>.GetHashCode(object obj)
46 // put objects in a bucket based on their reference
47 return RuntimeHelpers.GetHashCode(obj);
49 // put all objects in the same bucket so ReferenceEquals is called on all
55 private ErrorContext _currentErrorContext;
56 private BidirectionalDictionary<string, object> _mappings;
58 internal JsonSerializer Serializer { get; private set; }
60 protected JsonSerializerInternalBase(JsonSerializer serializer)
62 ValidationUtils.ArgumentNotNull(serializer, "serializer");
64 Serializer = serializer;
67 internal BidirectionalDictionary<string, object> DefaultReferenceMappings
71 // override equality comparer for object key dictionary
72 // object will be modified as it deserializes and might have mutable hashcode
73 if (_mappings == null)
74 _mappings = new BidirectionalDictionary<string, object>(
75 EqualityComparer<string>.Default,
76 new ReferenceEqualsEqualityComparer());
82 protected ErrorContext GetErrorContext(object currentObject, object member, Exception error)
84 if (_currentErrorContext == null)
85 _currentErrorContext = new ErrorContext(currentObject, member, error);
87 if (_currentErrorContext.Error != error)
88 throw new InvalidOperationException("Current error context error is different to requested error.");
90 return _currentErrorContext;
93 protected void ClearErrorContext()
95 if (_currentErrorContext == null)
96 throw new InvalidOperationException("Could not clear error context. Error context is already null.");
98 _currentErrorContext = null;
101 protected bool IsErrorHandled(object currentObject, JsonContract contract, object keyValue, Exception ex)
103 ErrorContext errorContext = GetErrorContext(currentObject, keyValue, ex);
104 contract.InvokeOnError(currentObject, Serializer.Context, errorContext);
106 if (!errorContext.Handled)
107 Serializer.OnError(new ErrorEventArgs(currentObject, errorContext));
109 return errorContext.Handled;