Skip to content

Commit 48fbd2a

Browse files
committed
make JSON support regex JAVA-57
1 parent 3e84520 commit 48fbd2a

3 files changed

Lines changed: 70 additions & 0 deletions

File tree

src/main/com/mongodb/Bytes.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,19 @@ public static int patternFlags( String flags ){
186186
return fint;
187187
}
188188

189+
public static int getFlag( char c ){
190+
Flag flag = Flag.getByCharacter( c );
191+
if ( flag == null )
192+
throw new IllegalArgumentException( "unrecognized flag: " + c );
193+
194+
if ( flag.unsupported != null ){
195+
_warnUnsupported( flag.unsupported );
196+
return 0;
197+
}
198+
199+
return flag.javaFlag;
200+
}
201+
189202
/** Converts Java regular expression flags into a string of flags for the database
190203
* @param flags Java flags
191204
* @return the flags for the database

src/main/com/mongodb/util/JSON.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.mongodb.*;
66
import java.text.*;
77
import java.util.*;
8+
import java.util.regex.*;
89

910
/**
1011
* Helper methods for JSON serialization and de-serialization
@@ -146,6 +147,11 @@ public static void serialize( Object o , StringBuilder buf ){
146147
return;
147148
}
148149

150+
if (o instanceof Pattern) {
151+
buf.append("/").append(o.toString()).append("/").append(Bytes.patternFlags( ((Pattern)o).flags() ));
152+
return;
153+
}
154+
149155
throw new RuntimeException( "json can't serialize type : " + o.getClass() );
150156
}
151157

@@ -230,6 +236,9 @@ public Object parse() {
230236
case '{':
231237
value = parseObject();
232238
break;
239+
case '/':
240+
value = parsePatter();
241+
break;
233242
default:
234243
throw new JSONParseException(s, pos);
235244
}
@@ -265,6 +274,31 @@ public DBObject parseObject() {
265274

266275
return obj;
267276
}
277+
278+
public Pattern parsePatter(){
279+
read( '/' );
280+
281+
StringBuilder buf = new StringBuilder();
282+
283+
char current = read();
284+
while( current != '/'){
285+
buf.append( current );
286+
current = read();
287+
}
288+
289+
int flags = 0;
290+
291+
while ( pos < s.length() ){
292+
current = s.charAt( pos );
293+
if ( Character.isWhitespace( current ) ||
294+
! Character.isLetter( current ) )
295+
break;
296+
flags |= Bytes.getFlag( current );
297+
current = read();
298+
}
299+
300+
return Pattern.compile( buf.toString() , flags );
301+
}
268302

269303
/**
270304
* Read the current character, making sure that it is the expected character.
@@ -281,6 +315,12 @@ public void read(char ch) {
281315
pos++;
282316
}
283317

318+
public char read(){
319+
if ( pos >= s.length() )
320+
throw new IllegalStateException( "string done" );
321+
return s.charAt( pos++ );
322+
}
323+
284324
/**
285325
* Read the current character, making sure that it is a hexidecimal character.
286326
*

src/test/com/mongodb/util/JSONTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
package com.mongodb.util;
2020

21+
import java.util.regex.*;
22+
2123
import com.mongodb.*;
2224

2325
import org.testng.annotations.Test;
@@ -244,6 +246,21 @@ public void testEscape1(){
244246
_escapeChar( "\\" );
245247
}
246248

249+
@org.testng.annotations.Test
250+
public void testPattern() {
251+
String x = "^Hello$";
252+
String y = "/" + x + "/i";
253+
254+
Pattern pattern = Pattern.compile( x , Pattern.CASE_INSENSITIVE);
255+
assertEquals( y , JSON.serialize(pattern));
256+
257+
BasicDBObject a = new BasicDBObject( "x" , pattern );
258+
assertEquals( "{ \"x\" : " + y + "}" , a.toString() );
259+
260+
DBObject b = (DBObject)JSON.parse( a.toString() );
261+
assertEquals( a.toString() , b.toString() );
262+
}
263+
247264

248265

249266
public static void main( String args[] ){

0 commit comments

Comments
 (0)