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

« back to all changes in this revision

Viewing changes to lib/Newtonsoft.Json/Src/Newtonsoft.Json/Converters/RegexConverter.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
 
using System;
2
 
using System.Collections.Generic;
3
 
using System.Linq;
4
 
using System.Text;
5
 
using System.Text.RegularExpressions;
6
 
using Newtonsoft.Json.Bson;
7
 
using System.Globalization;
8
 
 
9
 
namespace Newtonsoft.Json.Converters
10
 
{
11
 
  /// <summary>
12
 
  /// Converts a <see cref="Regex"/> to and from JSON and BSON.
13
 
  /// </summary>
14
 
  public class RegexConverter : JsonConverter
15
 
  {
16
 
    /// <summary>
17
 
    /// Writes the JSON representation of the object.
18
 
    /// </summary>
19
 
    /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
20
 
    /// <param name="value">The value.</param>
21
 
    /// <param name="serializer">The calling serializer.</param>
22
 
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
23
 
    {
24
 
      Regex regex = (Regex) value;
25
 
 
26
 
      BsonWriter bsonWriter = writer as BsonWriter;
27
 
      if (bsonWriter != null)
28
 
        WriteBson(bsonWriter, regex);
29
 
      else
30
 
        WriteJson(writer, regex);
31
 
    }
32
 
 
33
 
    private bool HasFlag(RegexOptions options, RegexOptions flag)
34
 
    {
35
 
      return ((options & flag) == flag);
36
 
    }
37
 
 
38
 
    private void WriteBson(BsonWriter writer, Regex regex)
39
 
    {
40
 
      // Regular expression - The first cstring is the regex pattern, the second
41
 
      // is the regex options string. Options are identified by characters, which 
42
 
      // must be stored in alphabetical order. Valid options are 'i' for case 
43
 
      // insensitive matching, 'm' for multiline matching, 'x' for verbose mode, 
44
 
      // 'l' to make \w, \W, etc. locale dependent, 's' for dotall mode 
45
 
      // ('.' matches everything), and 'u' to make \w, \W, etc. match unicode.
46
 
 
47
 
      string options = null;
48
 
 
49
 
      if (HasFlag(regex.Options, RegexOptions.IgnoreCase))
50
 
        options += "i";
51
 
 
52
 
      if (HasFlag(regex.Options, RegexOptions.Multiline))
53
 
        options += "m";
54
 
 
55
 
      if (HasFlag(regex.Options, RegexOptions.Singleline))
56
 
        options += "s";
57
 
 
58
 
      options += "u";
59
 
 
60
 
      if (HasFlag(regex.Options, RegexOptions.ExplicitCapture))
61
 
        options += "x";
62
 
 
63
 
      writer.WriteRegex(regex.ToString(), options);
64
 
    }
65
 
 
66
 
    private void WriteJson(JsonWriter writer, Regex regex)
67
 
    {
68
 
      writer.WriteStartObject();
69
 
      writer.WritePropertyName("Pattern");
70
 
      writer.WriteValue(regex.ToString());
71
 
      writer.WritePropertyName("Options");
72
 
      writer.WriteValue(regex.Options);
73
 
      writer.WriteEndObject();
74
 
    }
75
 
 
76
 
    /// <summary>
77
 
    /// Reads the JSON representation of the object.
78
 
    /// </summary>
79
 
    /// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
80
 
    /// <param name="objectType">Type of the object.</param>
81
 
    /// <param name="existingValue">The existing value of object being read.</param>
82
 
    /// <param name="serializer">The calling serializer.</param>
83
 
    /// <returns>The object value.</returns>
84
 
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
85
 
    {
86
 
      BsonReader bsonReader = reader as BsonReader;
87
 
 
88
 
      if (bsonReader != null)
89
 
        return ReadBson(bsonReader);
90
 
      else
91
 
        return ReadJson(reader);
92
 
    }
93
 
 
94
 
    private object ReadBson(BsonReader reader)
95
 
    {
96
 
      string regexText = (string)reader.Value;
97
 
      int patternOptionDelimiterIndex = regexText.LastIndexOf(@"/");
98
 
 
99
 
      string patternText = regexText.Substring(1, patternOptionDelimiterIndex - 1);
100
 
      string optionsText = regexText.Substring(patternOptionDelimiterIndex + 1);
101
 
 
102
 
      RegexOptions options = RegexOptions.None;
103
 
      foreach (char c in optionsText)
104
 
      {
105
 
        switch (c)
106
 
        {
107
 
          case 'i':
108
 
            options |= RegexOptions.IgnoreCase;
109
 
            break;
110
 
          case 'm':
111
 
            options |= RegexOptions.Multiline;
112
 
            break;
113
 
          case 's':
114
 
            options |= RegexOptions.Singleline;
115
 
            break;
116
 
          case 'x':
117
 
            options |= RegexOptions.ExplicitCapture;
118
 
            break;
119
 
        }
120
 
      }
121
 
 
122
 
      return new Regex(patternText, options);
123
 
    }
124
 
 
125
 
    private Regex ReadJson(JsonReader reader)
126
 
    {
127
 
      reader.Read();
128
 
      reader.Read();
129
 
      string pattern = (string) reader.Value;
130
 
 
131
 
      reader.Read();
132
 
      reader.Read();
133
 
      int options = Convert.ToInt32(reader.Value, CultureInfo.InvariantCulture);
134
 
 
135
 
      reader.Read();
136
 
 
137
 
      return new Regex(pattern, (RegexOptions)options);
138
 
    }
139
 
 
140
 
    /// <summary>
141
 
    /// Determines whether this instance can convert the specified object type.
142
 
    /// </summary>
143
 
    /// <param name="objectType">Type of the object.</param>
144
 
    /// <returns>
145
 
    ///         <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
146
 
    /// </returns>
147
 
    public override bool CanConvert(Type objectType)
148
 
    {
149
 
      return (objectType == typeof (Regex));
150
 
    }
151
 
  }
152
 
}
 
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.Text.RegularExpressions;
 
28
using Newtonsoft.Json.Bson;
 
29
using System.Globalization;
 
30
 
 
31
namespace Newtonsoft.Json.Converters
 
32
{
 
33
  /// <summary>
 
34
  /// Converts a <see cref="Regex"/> to and from JSON and BSON.
 
35
  /// </summary>
 
36
  public class RegexConverter : JsonConverter
 
37
  {
 
38
    /// <summary>
 
39
    /// Writes the JSON representation of the object.
 
40
    /// </summary>
 
41
    /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
 
42
    /// <param name="value">The value.</param>
 
43
    /// <param name="serializer">The calling serializer.</param>
 
44
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
 
45
    {
 
46
      Regex regex = (Regex) value;
 
47
 
 
48
      BsonWriter bsonWriter = writer as BsonWriter;
 
49
      if (bsonWriter != null)
 
50
        WriteBson(bsonWriter, regex);
 
51
      else
 
52
        WriteJson(writer, regex);
 
53
    }
 
54
 
 
55
    private bool HasFlag(RegexOptions options, RegexOptions flag)
 
56
    {
 
57
      return ((options & flag) == flag);
 
58
    }
 
59
 
 
60
    private void WriteBson(BsonWriter writer, Regex regex)
 
61
    {
 
62
      // Regular expression - The first cstring is the regex pattern, the second
 
63
      // is the regex options string. Options are identified by characters, which 
 
64
      // must be stored in alphabetical order. Valid options are 'i' for case 
 
65
      // insensitive matching, 'm' for multiline matching, 'x' for verbose mode, 
 
66
      // 'l' to make \w, \W, etc. locale dependent, 's' for dotall mode 
 
67
      // ('.' matches everything), and 'u' to make \w, \W, etc. match unicode.
 
68
 
 
69
      string options = null;
 
70
 
 
71
      if (HasFlag(regex.Options, RegexOptions.IgnoreCase))
 
72
        options += "i";
 
73
 
 
74
      if (HasFlag(regex.Options, RegexOptions.Multiline))
 
75
        options += "m";
 
76
 
 
77
      if (HasFlag(regex.Options, RegexOptions.Singleline))
 
78
        options += "s";
 
79
 
 
80
      options += "u";
 
81
 
 
82
      if (HasFlag(regex.Options, RegexOptions.ExplicitCapture))
 
83
        options += "x";
 
84
 
 
85
      writer.WriteRegex(regex.ToString(), options);
 
86
    }
 
87
 
 
88
    private void WriteJson(JsonWriter writer, Regex regex)
 
89
    {
 
90
      writer.WriteStartObject();
 
91
      writer.WritePropertyName("Pattern");
 
92
      writer.WriteValue(regex.ToString());
 
93
      writer.WritePropertyName("Options");
 
94
      writer.WriteValue(regex.Options);
 
95
      writer.WriteEndObject();
 
96
    }
 
97
 
 
98
    /// <summary>
 
99
    /// Reads the JSON representation of the object.
 
100
    /// </summary>
 
101
    /// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
 
102
    /// <param name="objectType">Type of the object.</param>
 
103
    /// <param name="existingValue">The existing value of object being read.</param>
 
104
    /// <param name="serializer">The calling serializer.</param>
 
105
    /// <returns>The object value.</returns>
 
106
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
 
107
    {
 
108
      BsonReader bsonReader = reader as BsonReader;
 
109
 
 
110
      if (bsonReader != null)
 
111
        return ReadBson(bsonReader);
 
112
      else
 
113
        return ReadJson(reader);
 
114
    }
 
115
 
 
116
    private object ReadBson(BsonReader reader)
 
117
    {
 
118
      string regexText = (string)reader.Value;
 
119
      int patternOptionDelimiterIndex = regexText.LastIndexOf('/');
 
120
 
 
121
      string patternText = regexText.Substring(1, patternOptionDelimiterIndex - 1);
 
122
      string optionsText = regexText.Substring(patternOptionDelimiterIndex + 1);
 
123
 
 
124
      RegexOptions options = RegexOptions.None;
 
125
      foreach (char c in optionsText)
 
126
      {
 
127
        switch (c)
 
128
        {
 
129
          case 'i':
 
130
            options |= RegexOptions.IgnoreCase;
 
131
            break;
 
132
          case 'm':
 
133
            options |= RegexOptions.Multiline;
 
134
            break;
 
135
          case 's':
 
136
            options |= RegexOptions.Singleline;
 
137
            break;
 
138
          case 'x':
 
139
            options |= RegexOptions.ExplicitCapture;
 
140
            break;
 
141
        }
 
142
      }
 
143
 
 
144
      return new Regex(patternText, options);
 
145
    }
 
146
 
 
147
    private Regex ReadJson(JsonReader reader)
 
148
    {
 
149
      reader.Read();
 
150
      reader.Read();
 
151
      string pattern = (string)reader.Value;
 
152
 
 
153
      reader.Read();
 
154
      reader.Read();
 
155
      int options = Convert.ToInt32(reader.Value, CultureInfo.InvariantCulture);
 
156
 
 
157
      reader.Read();
 
158
 
 
159
      return new Regex(pattern, (RegexOptions) options);
 
160
    }
 
161
 
 
162
    /// <summary>
 
163
    /// Determines whether this instance can convert the specified object type.
 
164
    /// </summary>
 
165
    /// <param name="objectType">Type of the object.</param>
 
166
    /// <returns>
 
167
    ///         <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
 
168
    /// </returns>
 
169
    public override bool CanConvert(Type objectType)
 
170
    {
 
171
      return (objectType == typeof (Regex));
 
172
    }
 
173
  }
 
174
}
 
 
b'\\ No newline at end of file'