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;
28
using System.Diagnostics;
30
using System.Runtime.CompilerServices;
31
using Newtonsoft.Json.Utilities;
33
namespace Newtonsoft.Json.Serialization
35
internal abstract class JsonSerializerInternalBase
37
private class ReferenceEqualsEqualityComparer : IEqualityComparer<object>
39
bool IEqualityComparer<object>.Equals(object x, object y)
41
return ReferenceEquals(x, y);
44
int IEqualityComparer<object>.GetHashCode(object obj)
46
#if !(NETFX_CORE || PORTABLE)
47
// put objects in a bucket based on their reference
48
return RuntimeHelpers.GetHashCode(obj);
50
// put all objects in the same bucket so ReferenceEquals is called on all
56
private ErrorContext _currentErrorContext;
57
private BidirectionalDictionary<string, object> _mappings;
58
private bool _serializing;
60
internal readonly JsonSerializer Serializer;
61
internal readonly ITraceWriter TraceWriter;
63
protected JsonSerializerInternalBase(JsonSerializer serializer)
65
ValidationUtils.ArgumentNotNull(serializer, "serializer");
67
Serializer = serializer;
68
TraceWriter = serializer.TraceWriter;
70
// kind of a hack but meh. might clean this up later
71
_serializing = (GetType() == typeof (JsonSerializerInternalWriter));
74
internal BidirectionalDictionary<string, object> DefaultReferenceMappings
78
// override equality comparer for object key dictionary
79
// object will be modified as it deserializes and might have mutable hashcode
80
if (_mappings == null)
81
_mappings = new BidirectionalDictionary<string, object>(
82
EqualityComparer<string>.Default,
83
new ReferenceEqualsEqualityComparer(),
84
"A different value already has the Id '{0}'.",
85
"A different Id has already been assigned for value '{0}'.");
91
private ErrorContext GetErrorContext(object currentObject, object member, string path, Exception error)
93
if (_currentErrorContext == null)
94
_currentErrorContext = new ErrorContext(currentObject, member, path, error);
96
if (_currentErrorContext.Error != error)
97
throw new InvalidOperationException("Current error context error is different to requested error.");
99
return _currentErrorContext;
102
protected void ClearErrorContext()
104
if (_currentErrorContext == null)
105
throw new InvalidOperationException("Could not clear error context. Error context is already null.");
107
_currentErrorContext = null;
110
protected bool IsErrorHandled(object currentObject, JsonContract contract, object keyValue, IJsonLineInfo lineInfo, string path, Exception ex)
112
ErrorContext errorContext = GetErrorContext(currentObject, keyValue, path, ex);
114
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Error && !errorContext.Traced)
116
// only write error once
117
errorContext.Traced = true;
119
string message = (_serializing) ? "Error serializing" : "Error deserializing";
120
if (contract != null)
121
message += " " + contract.UnderlyingType;
122
message += ". " + ex.Message;
124
// add line information to non-json.net exception message
125
if (!(ex is JsonException))
126
message = JsonPosition.FormatMessage(lineInfo, path, message);
128
TraceWriter.Trace(TraceLevel.Error, message, ex);
131
if (contract != null)
132
contract.InvokeOnError(currentObject, Serializer.Context, errorContext);
134
if (!errorContext.Handled)
135
Serializer.OnError(new ErrorEventArgs(currentObject, errorContext));
137
return errorContext.Handled;
b'\\ No newline at end of file'