~ubuntu-branches/ubuntu/trusty/smuxi/trusty-proposed

« back to all changes in this revision

Viewing changes to lib/Newtonsoft.Json/Src/Newtonsoft.Json/Serialization/DefaultReferenceResolver.cs

  • Committer: Package Import Robot
  • Author(s): Mirco Bauer
  • Date: 2013-05-25 22:11:31 UTC
  • mfrom: (1.2.12)
  • Revision ID: package-import@ubuntu.com-20130525221131-nd2mc0kzubuwyx20
Tags: 0.8.11-1
* [22d13d5] Imported Upstream version 0.8.11
* [6d2b95a] Refreshed patches
* [89eb66e] Added ServiceStack libraries to smuxi-engine package
* [848ab10] Enable Campfire engine
* [c6dbdc7] Always build db4o for predictable build result
* [13ec489] Exclude OS X specific libraries from dh_clideps

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.Linq;
29
 
using System.Runtime.CompilerServices;
30
 
using System.Text;
31
 
using Newtonsoft.Json.Utilities;
32
 
using System.Globalization;
33
 
 
34
 
namespace Newtonsoft.Json.Serialization
35
 
{
36
 
  internal class DefaultReferenceResolver : IReferenceResolver
37
 
  {
38
 
    private class ReferenceEqualsEqualityComparer : IEqualityComparer<object>
39
 
    {
40
 
      bool IEqualityComparer<object>.Equals(object x, object y)
41
 
      {
42
 
        return ReferenceEquals(x, y);
43
 
      }
44
 
 
45
 
      int IEqualityComparer<object>.GetHashCode(object obj)
46
 
      {
47
 
#if !PocketPC
48
 
        // put objects in a bucket based on their reference
49
 
        return RuntimeHelpers.GetHashCode(obj);
50
 
#else
51
 
        // put all objects in the same bucket so ReferenceEquals is called on all
52
 
        return -1;
53
 
#endif
54
 
      }
55
 
    }
56
 
 
57
 
    private int _referenceCount;
58
 
    private BidirectionalDictionary<string, object> _mappings;
59
 
 
60
 
    private BidirectionalDictionary<string, object> Mappings
61
 
    {
62
 
      get
63
 
      {
64
 
        // override equality comparer for object key dictionary
65
 
        // object will be modified as it deserializes and might have mutable hashcode
66
 
        if (_mappings == null)
67
 
          _mappings = new BidirectionalDictionary<string, object>(
68
 
            EqualityComparer<string>.Default,
69
 
            new ReferenceEqualsEqualityComparer());
70
 
 
71
 
        return _mappings;
72
 
      }
73
 
    }
74
 
 
75
 
    public object ResolveReference(string reference)
76
 
    {
77
 
      object value;
78
 
      Mappings.TryGetByFirst(reference, out value);
79
 
      return value;
80
 
    }
81
 
 
82
 
    public string GetReference(object value)
83
 
    {
84
 
      string reference;
85
 
      if (!Mappings.TryGetBySecond(value, out reference))
86
 
      {
87
 
        _referenceCount++;
88
 
        reference = _referenceCount.ToString(CultureInfo.InvariantCulture);
89
 
        Mappings.Add(reference, value);
90
 
      }
91
 
 
92
 
      return reference;
93
 
    }
94
 
 
95
 
    public void AddReference(string reference, object value)
96
 
    {
97
 
      Mappings.Add(reference, value);
98
 
    }
99
 
 
100
 
    public bool IsReferenced(object value)
101
 
    {
102
 
      string reference;
103
 
      return Mappings.TryGetBySecond(value, out reference);
104
 
    }
105
 
  }
106
 
}
 
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 Newtonsoft.Json.Utilities;
 
28
using System.Globalization;
 
29
 
 
30
namespace Newtonsoft.Json.Serialization
 
31
{
 
32
  internal class DefaultReferenceResolver : IReferenceResolver
 
33
  {
 
34
    private int _referenceCount;
 
35
 
 
36
    private BidirectionalDictionary<string, object> GetMappings(object context)
 
37
    {
 
38
      JsonSerializerInternalBase internalSerializer;
 
39
 
 
40
      if (context is JsonSerializerInternalBase)
 
41
        internalSerializer = (JsonSerializerInternalBase) context;
 
42
      else if (context is JsonSerializerProxy)
 
43
        internalSerializer = ((JsonSerializerProxy) context).GetInternalSerializer();
 
44
      else
 
45
        throw new JsonException("The DefaultReferenceResolver can only be used internally.");
 
46
 
 
47
      return internalSerializer.DefaultReferenceMappings;
 
48
    }
 
49
 
 
50
    public object ResolveReference(object context, string reference)
 
51
    {
 
52
      object value;
 
53
      GetMappings(context).TryGetByFirst(reference, out value);
 
54
      return value;
 
55
    }
 
56
 
 
57
    public string GetReference(object context, object value)
 
58
    {
 
59
      var mappings = GetMappings(context);
 
60
 
 
61
      string reference;
 
62
      if (!mappings.TryGetBySecond(value, out reference))
 
63
      {
 
64
        _referenceCount++;
 
65
        reference = _referenceCount.ToString(CultureInfo.InvariantCulture);
 
66
        mappings.Add(reference, value);
 
67
      }
 
68
 
 
69
      return reference;
 
70
    }
 
71
 
 
72
    public void AddReference(object context, string reference, object value)
 
73
    {
 
74
      GetMappings(context).Add(reference, value);
 
75
    }
 
76
 
 
77
    public bool IsReferenced(object context, object value)
 
78
    {
 
79
      string reference;
 
80
      return GetMappings(context).TryGetBySecond(value, out reference);
 
81
    }
 
82
  }
 
83
}
 
 
b'\\ No newline at end of file'