55import com .mongodb .*;
66import java .text .*;
77import 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 *
0 commit comments