2
using System.Collections.Generic;
5
using System.Text.RegularExpressions;
6
using Newtonsoft.Json.Bson;
7
using System.Globalization;
9
namespace Newtonsoft.Json.Converters
12
/// Converts a <see cref="Regex"/> to and from JSON and BSON.
14
public class RegexConverter : JsonConverter
17
/// Writes the JSON representation of the object.
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)
24
Regex regex = (Regex) value;
26
BsonWriter bsonWriter = writer as BsonWriter;
27
if (bsonWriter != null)
28
WriteBson(bsonWriter, regex);
30
WriteJson(writer, regex);
33
private bool HasFlag(RegexOptions options, RegexOptions flag)
35
return ((options & flag) == flag);
38
private void WriteBson(BsonWriter writer, Regex regex)
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.
47
string options = null;
49
if (HasFlag(regex.Options, RegexOptions.IgnoreCase))
52
if (HasFlag(regex.Options, RegexOptions.Multiline))
55
if (HasFlag(regex.Options, RegexOptions.Singleline))
60
if (HasFlag(regex.Options, RegexOptions.ExplicitCapture))
63
writer.WriteRegex(regex.ToString(), options);
66
private void WriteJson(JsonWriter writer, Regex regex)
68
writer.WriteStartObject();
69
writer.WritePropertyName("Pattern");
70
writer.WriteValue(regex.ToString());
71
writer.WritePropertyName("Options");
72
writer.WriteValue(regex.Options);
73
writer.WriteEndObject();
77
/// Reads the JSON representation of the object.
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)
86
BsonReader bsonReader = reader as BsonReader;
88
if (bsonReader != null)
89
return ReadBson(bsonReader);
91
return ReadJson(reader);
94
private object ReadBson(BsonReader reader)
96
string regexText = (string)reader.Value;
97
int patternOptionDelimiterIndex = regexText.LastIndexOf(@"/");
99
string patternText = regexText.Substring(1, patternOptionDelimiterIndex - 1);
100
string optionsText = regexText.Substring(patternOptionDelimiterIndex + 1);
102
RegexOptions options = RegexOptions.None;
103
foreach (char c in optionsText)
108
options |= RegexOptions.IgnoreCase;
111
options |= RegexOptions.Multiline;
114
options |= RegexOptions.Singleline;
117
options |= RegexOptions.ExplicitCapture;
122
return new Regex(patternText, options);
125
private Regex ReadJson(JsonReader reader)
129
string pattern = (string) reader.Value;
133
int options = Convert.ToInt32(reader.Value, CultureInfo.InvariantCulture);
137
return new Regex(pattern, (RegexOptions)options);
141
/// Determines whether this instance can convert the specified object type.
143
/// <param name="objectType">Type of the object.</param>
145
/// <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
147
public override bool CanConvert(Type objectType)
149
return (objectType == typeof (Regex));
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.Text.RegularExpressions;
28
using Newtonsoft.Json.Bson;
29
using System.Globalization;
31
namespace Newtonsoft.Json.Converters
34
/// Converts a <see cref="Regex"/> to and from JSON and BSON.
36
public class RegexConverter : JsonConverter
39
/// Writes the JSON representation of the object.
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)
46
Regex regex = (Regex) value;
48
BsonWriter bsonWriter = writer as BsonWriter;
49
if (bsonWriter != null)
50
WriteBson(bsonWriter, regex);
52
WriteJson(writer, regex);
55
private bool HasFlag(RegexOptions options, RegexOptions flag)
57
return ((options & flag) == flag);
60
private void WriteBson(BsonWriter writer, Regex regex)
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.
69
string options = null;
71
if (HasFlag(regex.Options, RegexOptions.IgnoreCase))
74
if (HasFlag(regex.Options, RegexOptions.Multiline))
77
if (HasFlag(regex.Options, RegexOptions.Singleline))
82
if (HasFlag(regex.Options, RegexOptions.ExplicitCapture))
85
writer.WriteRegex(regex.ToString(), options);
88
private void WriteJson(JsonWriter writer, Regex regex)
90
writer.WriteStartObject();
91
writer.WritePropertyName("Pattern");
92
writer.WriteValue(regex.ToString());
93
writer.WritePropertyName("Options");
94
writer.WriteValue(regex.Options);
95
writer.WriteEndObject();
99
/// Reads the JSON representation of the object.
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)
108
BsonReader bsonReader = reader as BsonReader;
110
if (bsonReader != null)
111
return ReadBson(bsonReader);
113
return ReadJson(reader);
116
private object ReadBson(BsonReader reader)
118
string regexText = (string)reader.Value;
119
int patternOptionDelimiterIndex = regexText.LastIndexOf('/');
121
string patternText = regexText.Substring(1, patternOptionDelimiterIndex - 1);
122
string optionsText = regexText.Substring(patternOptionDelimiterIndex + 1);
124
RegexOptions options = RegexOptions.None;
125
foreach (char c in optionsText)
130
options |= RegexOptions.IgnoreCase;
133
options |= RegexOptions.Multiline;
136
options |= RegexOptions.Singleline;
139
options |= RegexOptions.ExplicitCapture;
144
return new Regex(patternText, options);
147
private Regex ReadJson(JsonReader reader)
151
string pattern = (string)reader.Value;
155
int options = Convert.ToInt32(reader.Value, CultureInfo.InvariantCulture);
159
return new Regex(pattern, (RegexOptions) options);
163
/// Determines whether this instance can convert the specified object type.
165
/// <param name="objectType">Type of the object.</param>
167
/// <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
169
public override bool CanConvert(Type objectType)
171
return (objectType == typeof (Regex));
b'\\ No newline at end of file'