ÿþ/ *  
         j s o n . j s  
         2 0 0 7 - 0 2 - 1 8  
  
         P u b l i c   D o m a i n  
  
         T h i s   f i l e   a d d s   t h e s e   m e t h o d s   t o   J a v a S c r i p t :  
  
                 a r r a y . t o J S O N S t r i n g ( )  
                 b o o l e a n . t o J S O N S t r i n g ( )  
                 d a t e . t o J S O N S t r i n g ( )  
                 n u m b e r . t o J S O N S t r i n g ( )  
                 o b j e c t . t o J S O N S t r i n g ( )  
                 s t r i n g . t o J S O N S t r i n g ( )  
                         T h e s e   m e t h o d s   p r o d u c e   a   J S O N   t e x t   f r o m   a   J a v a S c r i p t   v a l u e .  
                         I t   m u s t   n o t   c o n t a i n   a n y   c y c l i c a l   r e f e r e n c e s .   I l l e g a l   v a l u e s  
                         w i l l   b e   e x c l u d e d .  
  
                         T h e   d e f a u l t   c o n v e r s i o n   f o r   d a t e s   i s   t o   a n   I S O   s t r i n g .   Y o u   c a n  
                         a d d   a   t o J S O N S t r i n g   m e t h o d   t o   a n y   d a t e   o b j e c t   t o   g e t   a   d i f f e r e n t  
                         r e p r e s e n t a t i o n .  
  
                 s t r i n g . p a r s e J S O N ( f i l t e r )  
                         T h i s   m e t h o d   p a r s e s   a   J S O N   t e x t   t o   p r o d u c e   a n   o b j e c t   o r  
                         a r r a y .   I t   c a n   t h r o w   a   S y n t a x E r r o r   e x c e p t i o n .  
  
                         T h e   o p t i o n a l   f i l t e r   p a r a m e t e r   i s   a   f u n c t i o n   w h i c h   c a n   f i l t e r   a n d  
                         t r a n s f o r m   t h e   r e s u l t s .   I t   r e c e i v e s   e a c h   o f   t h e   k e y s   a n d   v a l u e s ,   a n d  
                         i t s   r e t u r n   v a l u e   i s   u s e d   i n s t e a d   o f   t h e   o r i g i n a l   v a l u e .   I f   i t  
                         r e t u r n s   w h a t   i t   r e c e i v e d ,   t h e n   s t r u c t u r e   i s   n o t   m o d i f i e d .   I f   i t  
                         r e t u r n s   u n d e f i n e d   t h e n   t h e   m e m b e r   i s   d e l e t e d .  
  
                         E x a m p l e :  
  
                         / /   P a r s e   t h e   t e x t .   I f   a   k e y   c o n t a i n s   t h e   s t r i n g   ' d a t e '   t h e n  
                         / /   c o n v e r t   t h e   v a l u e   t o   a   d a t e .  
  
                         m y D a t a   =   t e x t . p a r s e J S O N ( f u n c t i o n   ( k e y ,   v a l u e )   {  
                                 r e t u r n   k e y . i n d e x O f ( ' d a t e ' )   > =   0   ?   n e w   D a t e ( v a l u e )   :   v a l u e ;  
                         } ) ;  
  
         I t   i s   e x p e c t e d   t h a t   t h e s e   m e t h o d s   w i l l   f o r m a l l y   b e c o m e   p a r t   o f   t h e  
         J a v a S c r i p t   P r o g r a m m i n g   L a n g u a g e   i n   t h e   F o u r t h   E d i t i o n   o f   t h e  
         E C M A S c r i p t   s t a n d a r d   i n   2 0 0 8 .  
 * /  
  
 / /   A u g m e n t   t h e   b a s i c   p r o t o t y p e s   i f   t h e y   h a v e   n o t   a l r e a d y   b e e n   a u g m e n t e d .  
  
 i f   ( ! O b j e c t . p r o t o t y p e . t o J S O N S t r i n g )   {  
  
         A r r a y . p r o t o t y p e . t o J S O N S t r i n g   =   f u n c t i o n   ( )   {  
                 v a r   a   =   [ ' [ ' ] ,     / /   T h e   a r r a y   h o l d i n g   t h e   t e x t   f r a g m e n t s .  
                         b ,                     / /   A   b o o l e a n   i n d i c a t i n g   t h a t   a   c o m m a   i s   r e q u i r e d .  
                         i ,                     / /   L o o p   c o u n t e r .  
                         l   =   t h i s . l e n g t h ,  
                         v ;                     / /   T h e   v a l u e   t o   b e   s t r i n g i f i e d .  
  
                 f u n c t i o n   p ( s )   {  
  
 / /   p   a c c u m u l a t e s   t e x t   f r a g m e n t s   i n   a n   a r r a y .   I t   i n s e r t s   a   c o m m a   b e f o r e   a l l  
 / /   e x c e p t   t h e   f i r s t   f r a g m e n t .  
  
                         i f   ( b )   {  
                                 a . p u s h ( ' , ' ) ;  
                         }  
                         a . p u s h ( s ) ;  
                         b   =   t r u e ;  
                 }  
  
 / /   F o r   e a c h   v a l u e   i n   t h i s   a r r a y . . .  
  
                 f o r   ( i   =   0 ;   i   <   l ;   i   + =   1 )   {  
                         v   =   t h i s [ i ] ;  
                         s w i t c h   ( t y p e o f   v )   {  
  
 / /   V a l u e s   w i t h o u t   a   J S O N   r e p r e s e n t a t i o n   a r e   i g n o r e d .  
  
                         c a s e   ' u n d e f i n e d ' :  
                         c a s e   ' f u n c t i o n ' :  
                         c a s e   ' u n k n o w n ' :  
                                 b r e a k ;  
  
 / /   S e r i a l i z e   a   J a v a S c r i p t   o b j e c t   v a l u e .   I g n o r e   o b j e c t s   t h a t s   l a c k   t h e  
 / /   t o J S O N S t r i n g   m e t h o d .   D u e   t o   a   s p e c i f i c a t i o n   e r r o r   i n   E C M A S c r i p t ,  
 / /   t y p e o f   n u l l   i s   ' o b j e c t ' ,   s o   w a t c h   o u t   f o r   t h a t   c a s e .  
  
                         c a s e   ' o b j e c t ' :  
                                 i f   ( v )   {  
                                         i f   ( t y p e o f   v . t o J S O N S t r i n g   = = =   ' f u n c t i o n ' )   {  
                                                 p ( v . t o J S O N S t r i n g ( ) ) ;  
                                         }  
                                 }   e l s e   {  
                                         p ( " n u l l " ) ;  
                                 }  
                                 b r e a k ;  
  
 / /   O t h e r w i s e ,   s e r i a l i z e   t h e   v a l u e .  
  
                         d e f a u l t :  
                                 p ( v . t o J S O N S t r i n g ( ) ) ;  
                         }  
                 }  
  
 / /   J o i n   a l l   o f   t h e   f r a g m e n t s   t o g e t h e r   a n d   r e t u r n .  
  
                 a . p u s h ( ' ] ' ) ;  
                 r e t u r n   a . j o i n ( ' ' ) ;  
         } ;  
  
  
         B o o l e a n . p r o t o t y p e . t o J S O N S t r i n g   =   f u n c t i o n   ( )   {  
                 r e t u r n   S t r i n g ( t h i s ) ;  
         } ;  
  
  
         D a t e . p r o t o t y p e . t o J S O N S t r i n g   =   f u n c t i o n   ( )   {  
  
 / /   U l t i m a t e l y ,   t h i s   m e t h o d   w i l l   b e   e q u i v a l e n t   t o   t h e   d a t e . t o I S O S t r i n g   m e t h o d .  
  
                 f u n c t i o n   f ( n )   {  
  
 / /   F o r m a t   i n t e g e r s   t o   h a v e   a t   l e a s t   t w o   d i g i t s .  
  
                         r e t u r n   n   <   1 0   ?   ' 0 '   +   n   :   n ;  
                 }  
  
                 r e t u r n   ' " '   +   t h i s . g e t F u l l Y e a r ( )   +   ' - '   +  
                                 f ( t h i s . g e t M o n t h ( )   +   1 )   +   ' - '   +  
                                 f ( t h i s . g e t D a t e ( ) )   +   ' T '   +  
                                 f ( t h i s . g e t H o u r s ( ) )   +   ' : '   +  
                                 f ( t h i s . g e t M i n u t e s ( ) )   +   ' : '   +  
                                 f ( t h i s . g e t S e c o n d s ( ) )   +   ' " ' ;  
         } ;  
  
  
         N u m b e r . p r o t o t y p e . t o J S O N S t r i n g   =   f u n c t i o n   ( )   {  
  
 / /   J S O N   n u m b e r s   m u s t   b e   f i n i t e .   E n c o d e   n o n - f i n i t e   n u m b e r s   a s   n u l l .  
  
                 r e t u r n   i s F i n i t e ( t h i s )   ?   S t r i n g ( t h i s )   :   " n u l l " ;  
         } ;  
  
  
         O b j e c t . p r o t o t y p e . t o J S O N S t r i n g   =   f u n c t i o n   ( )   {  
                 v a r   a   =   [ ' { ' ] ,     / /   T h e   a r r a y   h o l d i n g   t h e   t e x t   f r a g m e n t s .  
                         b ,                     / /   A   b o o l e a n   i n d i c a t i n g   t h a t   a   c o m m a   i s   r e q u i r e d .  
                         k ,                     / /   T h e   c u r r e n t   k e y .  
                         v ;                     / /   T h e   c u r r e n t   v a l u e .  
  
                 f u n c t i o n   p ( s )   {  
  
 / /   p   a c c u m u l a t e s   t e x t   f r a g m e n t   p a i r s   i n   a n   a r r a y .   I t   i n s e r t s   a   c o m m a   b e f o r e   a l l  
 / /   e x c e p t   t h e   f i r s t   f r a g m e n t   p a i r .  
  
                         i f   ( b )   {  
                                 a . p u s h ( ' , ' ) ;  
                         }  
                         a . p u s h ( k . t o J S O N S t r i n g ( ) ,   ' : ' ,   s ) ;  
                         b   =   t r u e ;  
                 }  
  
 / /   I t e r a t e   t h r o u g h   a l l   o f   t h e   k e y s   i n   t h e   o b j e c t ,   i g n o r i n g   t h e   p r o t o   c h a i n .  
  
                 f o r   ( k   i n   t h i s )   {  
                         i f   ( t h i s . h a s O w n P r o p e r t y ( k ) )   {  
                                 v   =   t h i s [ k ] ;  
                                 s w i t c h   ( t y p e o f   v )   {  
  
 / /   V a l u e s   w i t h o u t   a   J S O N   r e p r e s e n t a t i o n   a r e   i g n o r e d .  
  
                                 c a s e   ' u n d e f i n e d ' :  
                                 c a s e   ' f u n c t i o n ' :  
                                 c a s e   ' u n k n o w n ' :  
                                         b r e a k ;  
  
 / /   S e r i a l i z e   a   J a v a S c r i p t   o b j e c t   v a l u e .   I g n o r e   o b j e c t s   t h a t   l a c k   t h e  
 / /   t o J S O N S t r i n g   m e t h o d .   D u e   t o   a   s p e c i f i c a t i o n   e r r o r   i n   E C M A S c r i p t ,  
 / /   t y p e o f   n u l l   i s   ' o b j e c t ' ,   s o   w a t c h   o u t   f o r   t h a t   c a s e .  
  
                                 c a s e   ' o b j e c t ' :  
                                         i f   ( v )   {  
                                                 i f   ( t y p e o f   v . t o J S O N S t r i n g   = = =   ' f u n c t i o n ' )   {  
                                                         p ( v . t o J S O N S t r i n g ( ) ) ;  
                                                 }  
                                         }   e l s e   {  
                                                 p ( " n u l l " ) ;  
                                         }  
                                         b r e a k ;  
                                 d e f a u l t :  
                                         p ( v . t o J S O N S t r i n g ( ) ) ;  
                                 }  
                         }  
                 }  
  
 / /   J o i n   a l l   o f   t h e   f r a g m e n t s   t o g e t h e r   a n d   r e t u r n .  
  
                 a . p u s h ( ' } ' ) ;  
                 r e t u r n   a . j o i n ( ' ' ) ;  
         } ;  
  
  
         ( f u n c t i o n   ( s )   {  
  
 / /   A u g m e n t   S t r i n g . p r o t o t y p e .   W e   d o   t h i s   i n   a n   i m m e d i a t e   a n o n y m o u s   f u n c t i o n   t o  
 / /   a v o i d   d e f i n i n g   g l o b a l   v a r i a b l e s .  
  
 / /   m   i s   a   t a b l e   o f   c h a r a c t e r   s u b s t i t u t i o n s .  
  
                 v a r   m   =   {  
                         ' \ b ' :   ' \ \ b ' ,  
                         ' \ t ' :   ' \ \ t ' ,  
                         ' \ n ' :   ' \ \ n ' ,  
                         ' \ f ' :   ' \ \ f ' ,  
                         ' \ r ' :   ' \ \ r ' ,  
                         ' " '   :   ' \ \ " ' ,  
                         ' \ \ ' :   ' \ \ \ \ '  
                 } ;  
  
  
                 s . p a r s e J S O N   =   f u n c t i o n   ( f i l t e r )   {  
  
 / /   P a r s i n g   h a p p e n s   i n   t h r e e   s t a g e s .   I n   t h e   f i r s t   s t a g e ,   w e   r u n   t h e   t e x t   a g a i n s t  
 / /   a   r e g u l a r   e x p r e s s i o n   w h i c h   l o o k s   f o r   n o n - J S O N   c h a r a c t e r s .   W e   a r e   e s p e c i a l l y  
 / /   c o n c e r n e d   w i t h   ' ( ) '   a n d   ' n e w '   b e c a u s e   t h e y   c a n   c a u s e   i n v o c a t i o n ,   a n d   ' = '  
 / /   b e c a u s e   i t   c a n   c a u s e   m u t a t i o n .   B u t   j u s t   t o   b e   s a f e ,   w e   w i l l   r e j e c t   a l l  
 / /   u n e x p e c t e d   c h a r a c t e r s .  
  
                         t r y   {  
                                 i f   ( / ^ ( " ( \ \ . | [ ^ " \ \ \ n \ r ] ) * ? " | [ , : { } \ [ \ ] 0 - 9 . \ - + E a e f l n r - u   \ n \ r \ t ] ) + ? $ / .  
                                                 t e s t ( t h i s ) )   {  
  
 / /   I n   t h e   s e c o n d   s t a g e   w e   u s e   t h e   e v a l   f u n c t i o n   t o   c o m p i l e   t h e   t e x t   i n t o   a  
 / /   J a v a S c r i p t   s t r u c t u r e .   T h e   ' { '   o p e r a t o r   i s   s u b j e c t   t o   a   s y n t a c t i c   a m b i g u i t y  
 / /   i n   J a v a S c r i p t :   i t   c a n   b e g i n   a   b l o c k   o r   a n   o b j e c t   l i t e r a l .   W e   w r a p   t h e   t e x t  
 / /   i n   p a r e n s   t o   e l i m i n a t e   t h e   a m b i g u i t y .  
  
                                         v a r   j   =   e v a l ( ' ( '   +   t h i s   +   ' ) ' ) ;  
  
 / /   I n   t h e   o p t i o n a l   t h i r d   s t a g e ,   w e   r e c u r s i v e l y   w a l k   t h e   n e w   s t r u c t u r e ,   p a s s i n g  
 / /   e a c h   n a m e / v a l u e   p a i r   t o   a   f i l t e r   f u n c t i o n   f o r   p o s s i b l e   t r a n s f o r m a t i o n .  
  
                                         i f   ( t y p e o f   f i l t e r   = = =   ' f u n c t i o n ' )   {  
  
                                                 f u n c t i o n   w a l k ( k ,   v )   {  
                                                         i f   ( v   & &   t y p e o f   v   = = =   ' o b j e c t ' )   {  
                                                                 f o r   ( v a r   i   i n   v )   {  
                                                                         i f   ( v . h a s O w n P r o p e r t y ( i ) )   {  
                                                                                 v [ i ]   =   w a l k ( i ,   v [ i ] ) ;  
                                                                         }  
                                                                 }  
                                                         }  
                                                         r e t u r n   f i l t e r ( k ,   v ) ;  
                                                 }  
  
                                                 w a l k ( ' ' ,   j ) ;  
                                         }  
                                         r e t u r n   j ;  
                                 }  
                         }   c a t c h   ( e )   {  
  
 / /   F a l l   t h r o u g h   i f   t h e   r e g e x p   t e s t   f a i l s .  
  
                         }  
                         t h r o w   n e w   S y n t a x E r r o r ( " p a r s e J S O N " ) ;  
                 } ;  
  
  
                 s . t o J S O N S t r i n g   =   f u n c t i o n   ( )   {  
  
 / /   I f   t h e   s t r i n g   c o n t a i n s   n o   c o n t r o l   c h a r a c t e r s ,   n o   q u o t e   c h a r a c t e r s ,   a n d   n o  
 / /   b a c k s l a s h   c h a r a c t e r s ,   t h e n   w e   c a n   s i m p l y   s l a p   s o m e   q u o t e s   a r o u n d   i t .  
 / /   O t h e r w i s e   w e   m u s t   a l s o   r e p l a c e   t h e   o f f e n d i n g   c h a r a c t e r s   w i t h   s a f e  
 / /   s e q u e n c e s .  
  
                         i f   ( / [ " \ \ \ x 0 0 - \ x 1 f ] / . t e s t ( t h i s ) )   {  
                                 r e t u r n   ' " '   +   t h i s . r e p l a c e ( / ( [ \ x 0 0 - \ x 1 f \ \ " ] ) / g ,   f u n c t i o n ( a ,   b )   {  
                                         v a r   c   =   m [ b ] ;  
                                         i f   ( c )   {  
                                                 r e t u r n   c ;  
                                         }  
                                         c   =   b . c h a r C o d e A t ( ) ;  
                                         r e t u r n   ' \ \ u 0 0 '   +  
                                                 M a t h . f l o o r ( c   /   1 6 ) . t o S t r i n g ( 1 6 )   +  
                                                 ( c   %   1 6 ) . t o S t r i n g ( 1 6 ) ;  
                                 } )   +   ' " ' ;  
                         }  
                         r e t u r n   ' " '   +   t h i s   +   ' " ' ;  
                 } ;  
         } ) ( S t r i n g . p r o t o t y p e ) ;  
 } 
