~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/Newtonsoft.Json/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalBase.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.Diagnostics;
 
29
using System.Net;
 
30
using System.Runtime.CompilerServices;
 
31
using Newtonsoft.Json.Utilities;
 
32
 
 
33
namespace Newtonsoft.Json.Serialization
 
34
{
 
35
  internal abstract class JsonSerializerInternalBase
 
36
  {
 
37
    private class ReferenceEqualsEqualityComparer : IEqualityComparer<object>
 
38
    {
 
39
      bool IEqualityComparer<object>.Equals(object x, object y)
 
40
      {
 
41
        return ReferenceEquals(x, y);
 
42
      }
 
43
 
 
44
      int IEqualityComparer<object>.GetHashCode(object obj)
 
45
      {
 
46
#if !(NETFX_CORE || PORTABLE)
 
47
        // put objects in a bucket based on their reference
 
48
        return RuntimeHelpers.GetHashCode(obj);
 
49
#else
 
50
        // put all objects in the same bucket so ReferenceEquals is called on all
 
51
        return -1;
 
52
#endif
 
53
      }
 
54
    }
 
55
 
 
56
    private ErrorContext _currentErrorContext;
 
57
    private BidirectionalDictionary<string, object> _mappings;
 
58
    private bool _serializing;
 
59
 
 
60
    internal readonly JsonSerializer Serializer;
 
61
    internal readonly ITraceWriter TraceWriter;
 
62
 
 
63
    protected JsonSerializerInternalBase(JsonSerializer serializer)
 
64
    {
 
65
      ValidationUtils.ArgumentNotNull(serializer, "serializer");
 
66
 
 
67
      Serializer = serializer;
 
68
      TraceWriter = serializer.TraceWriter;
 
69
 
 
70
      // kind of a hack but meh. might clean this up later
 
71
      _serializing = (GetType() == typeof (JsonSerializerInternalWriter));
 
72
    }
 
73
 
 
74
    internal BidirectionalDictionary<string, object> DefaultReferenceMappings
 
75
    {
 
76
      get
 
77
      {
 
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}'.");
 
86
 
 
87
        return _mappings;
 
88
      }
 
89
    }
 
90
 
 
91
    private ErrorContext GetErrorContext(object currentObject, object member, string path, Exception error)
 
92
    {
 
93
      if (_currentErrorContext == null)
 
94
        _currentErrorContext = new ErrorContext(currentObject, member, path, error);
 
95
 
 
96
      if (_currentErrorContext.Error != error)
 
97
        throw new InvalidOperationException("Current error context error is different to requested error.");
 
98
 
 
99
      return _currentErrorContext;
 
100
    }
 
101
 
 
102
    protected void ClearErrorContext()
 
103
    {
 
104
      if (_currentErrorContext == null)
 
105
        throw new InvalidOperationException("Could not clear error context. Error context is already null.");
 
106
 
 
107
      _currentErrorContext = null;
 
108
    }
 
109
 
 
110
    protected bool IsErrorHandled(object currentObject, JsonContract contract, object keyValue, IJsonLineInfo lineInfo, string path, Exception ex)
 
111
    {
 
112
      ErrorContext errorContext = GetErrorContext(currentObject, keyValue, path, ex);
 
113
 
 
114
      if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Error && !errorContext.Traced)
 
115
      {
 
116
        // only write error once
 
117
        errorContext.Traced = true;
 
118
 
 
119
        string message = (_serializing) ? "Error serializing" : "Error deserializing";
 
120
        if (contract != null)
 
121
          message += " " + contract.UnderlyingType;
 
122
        message += ". " + ex.Message;
 
123
 
 
124
        // add line information to non-json.net exception message
 
125
        if (!(ex is JsonException))
 
126
          message = JsonPosition.FormatMessage(lineInfo, path, message);
 
127
 
 
128
        TraceWriter.Trace(TraceLevel.Error, message, ex);
 
129
      }
 
130
 
 
131
      if (contract != null)
 
132
        contract.InvokeOnError(currentObject, Serializer.Context, errorContext);
 
133
 
 
134
      if (!errorContext.Handled)
 
135
        Serializer.OnError(new ErrorEventArgs(currentObject, errorContext));
 
136
 
 
137
      return errorContext.Handled;
 
138
    }
 
139
  }
 
140
}
 
 
b'\\ No newline at end of file'