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.Collections.Generic;
28
#if !SILVERLIGHT && !PocketPC && !NET20 && !NETFX_CORE
29
using System.Data.Linq;
31
#if !SILVERLIGHT && !NETFX_CORE
32
using System.Data.SqlTypes;
35
using System.Text.RegularExpressions;
36
using Newtonsoft.Json.Bson;
37
using Newtonsoft.Json.Converters;
38
using Newtonsoft.Json.Utilities;
40
using NUnit.Framework;
42
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
43
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
44
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
46
using Newtonsoft.Json.Tests.TestObjects;
48
namespace Newtonsoft.Json.Tests.Converters
51
public class RegexConverterTests : TestFixtureBase
53
public class RegexTestClass
55
public Regex Regex { get; set; }
59
public void SerializeToText()
61
Regex regex = new Regex("abc", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
63
string json = JsonConvert.SerializeObject(regex, Formatting.Indented, new RegexConverter());
72
public void SerializeToBson()
74
Regex regex = new Regex("abc", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
76
MemoryStream ms = new MemoryStream();
77
BsonWriter writer = new BsonWriter(ms);
78
JsonSerializer serializer = new JsonSerializer();
79
serializer.Converters.Add(new RegexConverter());
81
serializer.Serialize(writer, new RegexTestClass { Regex = regex });
83
string expected = "13-00-00-00-0B-52-65-67-65-78-00-61-62-63-00-69-75-00-00";
84
string bson = MiscellaneousUtils.BytesToHex(ms.ToArray());
86
Assert.AreEqual(expected, bson);
90
public void DeserializeFromText()
97
Regex newRegex = JsonConvert.DeserializeObject<Regex>(json, new RegexConverter());
98
Assert.AreEqual("abc", newRegex.ToString());
99
Assert.AreEqual(RegexOptions.IgnoreCase | RegexOptions.CultureInvariant, newRegex.Options);
103
public void DeserializeFromBson()
105
MemoryStream ms = new MemoryStream(MiscellaneousUtils.HexToBytes("13-00-00-00-0B-52-65-67-65-78-00-61-62-63-00-69-75-00-00"));
106
BsonReader reader = new BsonReader(ms);
107
JsonSerializer serializer = new JsonSerializer();
108
serializer.Converters.Add(new RegexConverter());
110
RegexTestClass c = serializer.Deserialize<RegexTestClass>(reader);
112
Assert.AreEqual("abc", c.Regex.ToString());
113
Assert.AreEqual(RegexOptions.IgnoreCase, c.Regex.Options);
117
public void ConvertEmptyRegexBson()
119
Regex regex = new Regex(string.Empty);
121
MemoryStream ms = new MemoryStream();
122
BsonWriter writer = new BsonWriter(ms);
123
JsonSerializer serializer = new JsonSerializer();
124
serializer.Converters.Add(new RegexConverter());
126
serializer.Serialize(writer, new RegexTestClass { Regex = regex });
128
ms.Seek(0, SeekOrigin.Begin);
129
BsonReader reader = new BsonReader(ms);
130
serializer.Converters.Add(new RegexConverter());
132
RegexTestClass c = serializer.Deserialize<RegexTestClass>(reader);
134
Assert.AreEqual("", c.Regex.ToString());
135
Assert.AreEqual(RegexOptions.None, c.Regex.Options);
139
public void ConvertRegexWithAllOptionsBson()
141
Regex regex = new Regex(
143
RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.ExplicitCapture);
145
MemoryStream ms = new MemoryStream();
146
BsonWriter writer = new BsonWriter(ms);
147
JsonSerializer serializer = new JsonSerializer();
148
serializer.Converters.Add(new RegexConverter());
150
serializer.Serialize(writer, new RegexTestClass { Regex = regex });
152
string expected = "14-00-00-00-0B-52-65-67-65-78-00-2F-00-69-6D-73-75-78-00-00";
153
string bson = MiscellaneousUtils.BytesToHex(ms.ToArray());
155
Assert.AreEqual(expected, bson);
157
ms.Seek(0, SeekOrigin.Begin);
158
BsonReader reader = new BsonReader(ms);
159
serializer.Converters.Add(new RegexConverter());
161
RegexTestClass c = serializer.Deserialize<RegexTestClass>(reader);
163
Assert.AreEqual("/", c.Regex.ToString());
164
Assert.AreEqual(RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.ExplicitCapture, c.Regex.Options);
168
public void ConvertEmptyRegexJson()
170
Regex regex = new Regex("");
172
string json = JsonConvert.SerializeObject(new RegexTestClass { Regex = regex }, Formatting.Indented, new RegexConverter());
181
RegexTestClass newRegex = JsonConvert.DeserializeObject<RegexTestClass>(json, new RegexConverter());
182
Assert.AreEqual("", newRegex.Regex.ToString());
183
Assert.AreEqual(RegexOptions.None, newRegex.Regex.Options);
b'\\ No newline at end of file'