All files
[pithos-ms-client] / trunk / Libraries / Json40r2 / Source / Src / Newtonsoft.Json / Serialization / JsonSerializerInternalBase.cs
1 #region License
2 // Copyright (c) 2007 James Newton-King
3 //
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
11 // conditions:
12 //
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
15 //
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.
24 #endregion
25
26 using System;
27 using System.Collections.Generic;
28 using System.Net;
29 using System.Runtime.CompilerServices;
30 using Newtonsoft.Json.Utilities;
31
32 namespace Newtonsoft.Json.Serialization
33 {
34   internal abstract class JsonSerializerInternalBase
35   {
36     private class ReferenceEqualsEqualityComparer : IEqualityComparer<object>
37     {
38       bool IEqualityComparer<object>.Equals(object x, object y)
39       {
40         return ReferenceEquals(x, y);
41       }
42
43       int IEqualityComparer<object>.GetHashCode(object obj)
44       {
45 #if !PocketPC
46         // put objects in a bucket based on their reference
47         return RuntimeHelpers.GetHashCode(obj);
48 #else
49         // put all objects in the same bucket so ReferenceEquals is called on all
50         return -1;
51 #endif
52       }
53     }
54
55     private ErrorContext _currentErrorContext;
56     private BidirectionalDictionary<string, object> _mappings;
57
58     internal JsonSerializer Serializer { get; private set; }
59
60     protected JsonSerializerInternalBase(JsonSerializer serializer)
61     {
62       ValidationUtils.ArgumentNotNull(serializer, "serializer");
63
64       Serializer = serializer;
65     }
66
67     internal BidirectionalDictionary<string, object> DefaultReferenceMappings
68     {
69       get
70       {
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());
77
78         return _mappings;
79       }
80     }
81
82     protected ErrorContext GetErrorContext(object currentObject, object member, Exception error)
83     {
84       if (_currentErrorContext == null)
85         _currentErrorContext = new ErrorContext(currentObject, member, error);
86
87       if (_currentErrorContext.Error != error)
88         throw new InvalidOperationException("Current error context error is different to requested error.");
89
90       return _currentErrorContext;
91     }
92
93     protected void ClearErrorContext()
94     {
95       if (_currentErrorContext == null)
96         throw new InvalidOperationException("Could not clear error context. Error context is already null.");
97
98       _currentErrorContext = null;
99     }
100
101     protected bool IsErrorHandled(object currentObject, JsonContract contract, object keyValue, Exception ex)
102     {
103       ErrorContext errorContext = GetErrorContext(currentObject, keyValue, ex);
104       contract.InvokeOnError(currentObject, Serializer.Context, errorContext);
105
106       if (!errorContext.Handled)
107         Serializer.OnError(new ErrorEventArgs(currentObject, errorContext));
108
109       return errorContext.Handled;
110     }
111   }
112 }