]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_rpc_json.cpp
Ignore all exceptions for now.. m_rpc_json is rather willing to throw
[user/henk/code/inspircd.git] / src / modules / m_rpc_json.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *          the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include <cstddef>
15 #include <cstdio>
16 #include <iostream>
17 #include <stdexcept>
18 #include <utility>
19
20 #include "inspircd.h"
21 #include "users.h"
22 #include "channels.h"
23 #include "configreader.h"
24 #include "modules.h"
25 #include "inspsocket.h"
26 #include "httpd.h"
27 #include "json.h"
28
29 /* $ModDesc: Provides a JSON-RPC interface for modules using m_httpd.so */
30
31 class ModuleRpcJson : public Module
32 {
33  public:
34         ModuleRpcJson(InspIRCd* Me) : Module(Me)
35         {
36                 json::rpc::init ();
37         }
38
39         void OnEvent(Event* event)
40         {
41                 std::stringstream data("");
42
43                 if (event->GetEventID() == "httpd_url")
44                 {
45                         HTTPRequest* http = (HTTPRequest*)event->GetData();
46
47                         if (http->GetURI() == "/jsonrpc" && http->GetType() == "POST")
48                         {
49                                 std::string response_text;
50                                 try
51                                 {
52                                         json::rpc::process (http, response_text, http->GetPostData().c_str());
53                                 }
54                                 catch (std::runtime_error &)
55                                 {
56                                         // ignore
57                                 }
58                                 data << response_text;
59
60                                 /* Send the document back to m_httpd */
61                                 HTTPDocument response(http->sock, &data, 200, "X-Powered-By: m_rpc_json.so\r\n"
62                                                                               "Content-Type: application/json; charset=iso-8859-1\r\n");
63                                 Request req((char*)&response, (Module*)this, event->GetSource());
64                                 req.Send();
65                         }
66                 }
67         }
68
69         void Implements(char* List)
70         {
71                 List[I_OnEvent] = 1;
72         }
73
74         virtual ~ModuleRpcJson()
75         {
76         }
77
78         virtual Version GetVersion()
79         {
80                 return Version(0, 1, 0, 0, VF_VENDOR, API_VERSION);
81         }
82 };
83
84 static void
85 unreachable_internal (char const *file, int line, char const *function)
86 {
87   char buf[1024];
88   snprintf (buf, 1024, "%s (%d) [%s] critical: Unreachable line reached.",
89             file, line, function);
90
91   throw std::runtime_error (buf);
92 }
93
94 static void
95 throw_unless_internal (char const *file, int line, char const *function, char const *condition)
96 {
97   char buf[1024];
98   snprintf (buf, 1024, "%s (%d) [%s] critical: Assertion `%s' failed.",
99             file, line, function, condition);
100
101   throw std::runtime_error (buf);
102 }
103
104 static void
105 throw_msg_unless_internal (char const *file, int line, char const *function, char const *message)
106 {
107   char buf[1024];
108   snprintf (buf, 1024, "%s (%d) [%s] critical: %s.",
109             file, line, function, message);
110
111   throw std::runtime_error (buf);
112 }
113
114
115 namespace json
116 {
117   ValueIteratorBase::ValueIteratorBase ()
118   {
119   }
120
121
122   ValueIteratorBase::ValueIteratorBase (const Value::ObjectValues::iterator &current)
123     : current_ (current)
124   {
125   }
126
127   Value &
128   ValueIteratorBase::deref () const
129   {
130     return current_->second;
131   }
132
133
134   void 
135   ValueIteratorBase::increment ()
136   {
137     ++current_;
138   }
139
140
141   void 
142   ValueIteratorBase::decrement ()
143   {
144     --current_;
145   }
146
147
148   ValueIteratorBase::difference_type 
149   ValueIteratorBase::computeDistance (const SelfType &other) const
150   {
151     return difference_type (std::distance (current_, other.current_));
152   }
153
154
155   bool 
156   ValueIteratorBase::isEqual (const SelfType &other) const
157   {
158     return current_ == other.current_;
159   }
160
161
162   void 
163   ValueIteratorBase::copy (const SelfType &other)
164   {
165     current_ = other.current_;
166   }
167
168
169   Value 
170   ValueIteratorBase::key () const
171   {
172     const Value::CZString czstring = (*current_).first;
173     if (czstring.c_str ())
174       {
175         if (czstring.isStaticString ())
176           return Value (StaticString (czstring.c_str ()));
177         return Value (czstring.c_str ());
178       }
179     return Value (czstring.index ());
180   }
181
182
183   unsigned 
184   ValueIteratorBase::index () const
185   {
186     const Value::CZString czstring = (*current_).first;
187     if (!czstring.c_str ())
188       return czstring.index ();
189     return unsigned (-1);
190   }
191
192
193   char const *
194   ValueIteratorBase::memberName () const
195   {
196     char const *name = (*current_).first.c_str ();
197     return name ? name : "";
198   }
199
200
201   ValueConstIterator::ValueConstIterator ()
202   {
203   }
204
205
206   ValueConstIterator::ValueConstIterator (const Value::ObjectValues::iterator &current)
207     : ValueIteratorBase (current)
208   {
209   }
210
211   ValueConstIterator &
212   ValueConstIterator::operator = (const ValueIteratorBase &other)
213   {
214     copy (other);
215     return *this;
216   }
217
218
219   ValueIterator::ValueIterator ()
220   {
221   }
222
223
224   ValueIterator::ValueIterator (const Value::ObjectValues::iterator &current)
225     : ValueIteratorBase (current)
226   {
227   }
228
229   ValueIterator::ValueIterator (const ValueConstIterator &other)
230     : ValueIteratorBase (other)
231   {
232   }
233
234   ValueIterator::ValueIterator (const ValueIterator &other)
235     : ValueIteratorBase (other)
236   {
237   }
238
239   ValueIterator &
240   ValueIterator::operator = (const SelfType &other)
241   {
242     copy (other);
243     return *this;
244   }
245 }
246
247 namespace json
248 {
249   inline bool 
250   in (char c, char c1, char c2, char c3, char c4)
251   {
252     return c == c1 || c == c2 || c == c3 || c == c4;
253   }
254
255   inline bool 
256   in (char c, char c1, char c2, char c3, char c4, char c5)
257   {
258     return c == c1 || c == c2 || c == c3 || c == c4 || c == c5;
259   }
260
261
262   Reader::Reader ()
263   {
264   }
265
266   bool
267   Reader::parse (const std::string &document, 
268                  Value &root)
269   {
270     document_ = document;
271     char const *begin = document_.c_str ();
272     char const *end = begin + document_.length ();
273     return parse (begin, end, root);
274   }
275
276   bool
277   Reader::parse (std::istream& sin,
278                  Value &root)
279   {
280     std::string doc;
281     std::getline (sin, doc, (char)EOF);
282     return parse (doc, root);
283   }
284
285   bool 
286   Reader::parse (char const *beginDoc, char const *endDOc, 
287                  Value &root)
288   {
289     begin_ = beginDoc;
290     end_ = endDOc;
291     current_ = begin_;
292     lastValueEnd_ = 0;
293     lastValue_ = 0;
294     errors_.clear ();
295     while (!nodes_.empty ())
296       nodes_.pop ();
297     nodes_.push (&root);
298    
299     bool successful = readValue ();
300     return successful;
301   }
302
303
304   bool
305   Reader::readValue ()
306   {
307     Token token;
308     do
309       readToken (token);
310     while (token.type_ == tokenComment);
311     bool successful = true;
312
313     switch (token.type_)
314       {
315       case tokenObjectBegin:
316         successful = readObject ();
317         break;
318       case tokenArrayBegin:
319         successful = readArray ();
320         break;
321       case tokenNumber:
322         successful = decodeNumber (token);
323         break;
324       case tokenString:
325         successful = decodeString (token);
326         break;
327       case tokenTrue:
328         currentValue () = true;
329         break;
330       case tokenFalse:
331         currentValue () = false;
332         break;
333       case tokenNull:
334         currentValue () = Value ();
335         break;
336       default:
337         return addError ("Syntax error: value, object or array expected.", token);
338       }
339
340     return successful;
341   }
342
343
344   bool 
345   Reader::expectToken (TokenType type, Token &token, char const *message)
346   {
347     readToken (token);
348     if (token.type_ != type)
349       return addError (message, token);
350     return true;
351   }
352
353
354   bool 
355   Reader::readToken (Token &token)
356   {
357     skipSpaces ();
358     token.start_ = current_;
359     char c = getNextChar ();
360     bool ok = true;
361     switch (c)
362       {
363       case '{':
364         token.type_ = tokenObjectBegin;
365         break;
366       case '}':
367         token.type_ = tokenObjectEnd;
368         break;
369       case '[':
370         token.type_ = tokenArrayBegin;
371         break;
372       case ']':
373         token.type_ = tokenArrayEnd;
374         break;
375       case '"':
376         token.type_ = tokenString;
377         ok = readString ();
378         break;
379 #if 0
380 #ifdef __GNUC__
381       case '0'...'9':
382 #endif
383 #else
384       case '0': case '1': case '2': case '3':
385       case '4': case '5': case '6': case '7':
386       case '8': case '9':
387 #endif
388       case '-':
389         token.type_ = tokenNumber;
390         readNumber ();
391         break;
392       case 't':
393         token.type_ = tokenTrue;
394         ok = match ("rue", 3);
395         break;
396       case 'f':
397         token.type_ = tokenFalse;
398         ok = match ("alse", 4);
399         break;
400       case 'n':
401         token.type_ = tokenNull;
402         ok = match ("ull", 3);
403         break;
404       case ',':
405         token.type_ = tokenArraySeparator;
406         break;
407       case ':':
408         token.type_ = tokenMemberSeparator;
409         break;
410       case 0:
411         token.type_ = tokenEndOfStream;
412         break;
413       default:
414         ok = false;
415         break;
416       }
417     if (!ok)
418       token.type_ = tokenError;
419     token.end_ = current_;
420     return true;
421   }
422
423
424   void 
425   Reader::skipSpaces ()
426   {
427     while (current_ != end_)
428       {
429         char c = *current_;
430         if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
431           ++current_;
432         else
433           break;
434       }
435   }
436
437
438   bool 
439   Reader::match (Location pattern, int patternLength)
440   {
441     if (end_ - current_ < patternLength)
442       return false;
443     int index = patternLength;
444     while (index--)
445       if (current_[index] != pattern[index])
446         return false;
447     current_ += patternLength;
448     return true;
449   }
450
451
452   void 
453   Reader::readNumber ()
454   {
455     while (current_ != end_)
456       {
457         if (!(*current_ >= '0' && *current_ <= '9')  &&
458              !in (*current_, '.', 'e', 'E', '+', '-'))
459           break;
460         ++current_;
461       }
462   }
463
464   bool
465   Reader::readString ()
466   {
467     char c = 0;
468     while (current_ != end_)
469       {
470         c = getNextChar ();
471         if (c == '\\')
472           getNextChar ();
473         else if (c == '"')
474           break;
475       }
476     return c == '"';
477   }
478
479
480   bool 
481   Reader::readObject ()
482   {
483     Token tokenName;
484     std::string name;
485     currentValue () = Value (objectValue);
486     while (readToken (tokenName))
487       {
488         if (tokenName.type_ == tokenObjectEnd && name.empty ())  // empty object
489           return true;
490         if (tokenName.type_ != tokenString)
491           break;
492       
493         name = "";
494         if (!decodeString (tokenName, name))
495           return recoverFromError (tokenObjectEnd);
496
497         Token colon;
498         if (!readToken (colon) ||  colon.type_ != tokenMemberSeparator)
499           {
500             return addErrorAndRecover ("Missing ':' after object member name", 
501                                        colon, 
502                                        tokenObjectEnd);
503           }
504         Value &value = currentValue ()[ name ];
505         nodes_.push (&value);
506         bool ok = readValue ();
507         nodes_.pop ();
508         if (!ok) // error already set
509           return recoverFromError (tokenObjectEnd);
510
511         Token comma;
512         if (!readToken (comma)
513              ||   (comma.type_ != tokenObjectEnd && 
514                    comma.type_ != tokenArraySeparator))
515           {
516             return addErrorAndRecover ("Missing ',' or '}' in object declaration", 
517                                        comma, 
518                                        tokenObjectEnd);
519           }
520         if (comma.type_ == tokenObjectEnd)
521           return true;
522       }
523     return addErrorAndRecover ("Missing '}' or object member name", 
524                                tokenName, 
525                                tokenObjectEnd);
526   }
527
528
529   bool 
530   Reader::readArray ()
531   {
532     currentValue () = Value (arrayValue);
533     skipSpaces ();
534     if (*current_ == ']') // empty array
535       {
536         Token endArray;
537         readToken (endArray);
538         return true;
539       }
540     int index = 0;
541     while (true)
542       {
543         Value &value = currentValue ()[ index++ ];
544         nodes_.push (&value);
545         bool ok = readValue ();
546         nodes_.pop ();
547         if (!ok) // error already set
548           return recoverFromError (tokenArrayEnd);
549
550         Token token;
551         if (!readToken (token) 
552              ||   (token.type_ != tokenArraySeparator && 
553                    token.type_ != tokenArrayEnd))
554           {
555             return addErrorAndRecover ("Missing ',' or ']' in array declaration", 
556                                        token, 
557                                        tokenArrayEnd);
558           }
559         if (token.type_ == tokenArrayEnd)
560           break;
561       }
562     return true;
563   }
564
565
566   bool 
567   Reader::decodeNumber (Token &token)
568   {
569     bool isDouble = false;
570     for (Location inspect = token.start_; inspect != token.end_; ++inspect)
571       {
572         isDouble = isDouble  
573           ||  in (*inspect, '.', 'e', 'E', '+')  
574           ||   (*inspect == '-' && inspect != token.start_);
575       }
576     if (isDouble)
577       return decodeDouble (token);
578     Location current = token.start_;
579     bool isNegative = *current == '-';
580     if (isNegative)
581       ++current;
582     unsigned threshold = (isNegative ? unsigned (-Value::minInt) 
583                              : Value::maxUInt) / 10;
584     unsigned value = 0;
585     while (current < token.end_)
586       {
587         char c = *current++;
588         if (c < '0'  ||  c > '9')
589           return addError ("'" + std::string (token.start_, token.end_) + "' is not a number.", token);
590         if (value >= threshold)
591           return decodeDouble (token);
592         value = value * 10 + unsigned (c - '0');
593       }
594     if (isNegative)
595       currentValue () = -int (value);
596     else if (value <= unsigned (Value::maxInt))
597       currentValue () = int (value);
598     else
599       currentValue () = value;
600     return true;
601   }
602
603
604   bool 
605   Reader::decodeDouble (Token &token)
606   {
607     double value = 0;
608     const int bufferSize = 32;
609     int count;
610     int length = int (token.end_ - token.start_);
611     if (length <= bufferSize)
612       {
613         char buffer[bufferSize];
614         memcpy (buffer, token.start_, length);
615         buffer[length] = 0;
616         count = sscanf (buffer, "%lf", &value);
617       }
618     else
619       {
620         std::string buffer (token.start_, token.end_);
621         count = sscanf (buffer.c_str (), "%lf", &value);
622       }
623
624     if (count != 1)
625       return addError ("'" + std::string (token.start_, token.end_) + "' is not a number.", token);
626     currentValue () = value;
627     return true;
628   }
629
630
631   bool 
632   Reader::decodeString (Token &token)
633   {
634     std::string decoded;
635     if (!decodeString (token, decoded))
636       return false;
637     currentValue () = decoded;
638     return true;
639   }
640
641
642   bool 
643   Reader::decodeString (Token &token, std::string &decoded)
644   {
645     Location current = token.start_ + 1; // skip '"'
646     Location end = token.end_ - 1;       // do not include '"'
647     decoded.reserve (long (end - current));
648
649     while (current != end)
650       {
651         char c = *current++;
652         if (expect_false (c == '"'))
653           break;
654         else if (expect_false (c == '\\'))
655           {
656             if (expect_false (current == end))
657               return addError ("Empty escape sequence in string", token, current);
658             char escape = *current++;
659             switch (escape)
660               {
661               case '"':
662               case '/':
663               case '\\': decoded += escape; break;
664
665               case 'b': decoded += '\010'; break;
666               case 't': decoded += '\011'; break;
667               case 'n': decoded += '\012'; break;
668               case 'f': decoded += '\014'; break;
669               case 'r': decoded += '\015'; break;
670               case 'u':
671                 {
672                   unsigned unicode;
673                   if (!decodeUnicodeEscapeSequence (token, current, end, unicode))
674                     return false;
675                   // @todo encode unicode as utf8.
676                   // @todo remember to alter the writer too.
677                 }
678                 break;
679               default:
680                 return addError ("Bad escape sequence in string", token, current);
681               }
682           }
683         else
684           {
685             decoded += c;
686           }
687       }
688
689     return true;
690   }
691
692
693   bool 
694   Reader::decodeUnicodeEscapeSequence (Token &token, 
695                                        Location &current, 
696                                        Location end, 
697                                        unsigned &unicode)
698   {
699     if (end - current < 4)
700       return addError ("Bad unicode escape sequence in string: four digits expected.", token, current);
701     unicode = 0;
702     for (int index = 0; index < 4; ++index)
703       {
704         char c = *current++;
705         unicode *= 16;
706         if (c >= '0' && c <= '9')
707           unicode += c - '0';
708         else if (c >= 'a' && c <= 'f')
709           unicode += c - 'a' + 10;
710         else if (c >= 'A' && c <= 'F')
711           unicode += c - 'A' + 10;
712         else
713           return addError ("Bad unicode escape sequence in string: hexadecimal digit expected.", token, current);
714       }
715     return true;
716   }
717
718
719   bool 
720   Reader::addError (const std::string &message, 
721                     Token &token,
722                     Location extra)
723   {
724     ErrorInfo info;
725     info.token_ = token;
726     info.message_ = message;
727     info.extra_ = extra;
728     errors_.push_back (info);
729     return false;
730   }
731
732
733   bool 
734   Reader::recoverFromError (TokenType skipUntilToken)
735   {
736     int errorCount = int (errors_.size ());
737     Token skip;
738     while (true)
739       {
740         if (!readToken (skip))
741           errors_.resize (errorCount); // discard errors caused by recovery
742         if (skip.type_ == skipUntilToken  ||  skip.type_ == tokenEndOfStream)
743           break;
744       }
745     errors_.resize (errorCount);
746     return false;
747   }
748
749
750   bool 
751   Reader::addErrorAndRecover (const std::string &message, 
752                               Token &token,
753                               TokenType skipUntilToken)
754   {
755     addError (message, token);
756     return recoverFromError (skipUntilToken);
757   }
758
759
760   Value &
761   Reader::currentValue ()
762   {
763     return *(nodes_.top ());
764   }
765
766
767   char 
768   Reader::getNextChar ()
769   {
770     if (current_ == end_)
771       return 0;
772     return *current_++;
773   }
774
775
776   void 
777   Reader::getLocationLineAndColumn (Location location,
778                                     int &line,
779                                     int &column) const
780   {
781     Location current = begin_;
782     Location lastLineStart = current;
783     line = 0;
784     while (current < location && current != end_)
785       {
786         char c = *current++;
787         if (c == '\r')
788           {
789             if (*current == '\n')
790               ++current;
791             lastLineStart = current;
792             ++line;
793           }
794         else if (c == '\n')
795           {
796             lastLineStart = current;
797             ++line;
798           }
799       }
800     // column & line start at 1
801     column = int (location - lastLineStart) + 1;
802     ++line;
803   }
804
805
806   std::string
807   Reader::getLocationLineAndColumn (Location location) const
808   {
809     int line, column;
810     getLocationLineAndColumn (location, line, column);
811     char buffer[18+16+16+1];
812     sprintf (buffer, "Line %d, Column %d", line, column);
813     return buffer;
814   }
815
816
817   std::string 
818   Reader::error_msgs () const
819   {
820     std::string formattedMessage;
821     for (Errors::const_iterator itError = errors_.begin ();
822           itError != errors_.end ();
823           ++itError)
824       {
825         const ErrorInfo &error = *itError;
826         formattedMessage += "* " + getLocationLineAndColumn (error.token_.start_) + "\n";
827         formattedMessage += "  " + error.message_ + "\n";
828         if (error.extra_)
829           formattedMessage += "See " + getLocationLineAndColumn (error.extra_) + " for detail.\n";
830       }
831     return formattedMessage;
832   }
833 } // namespace json
834
835 namespace json
836 {
837   void
838   unreachable_internal (char const *file, int line, char const *function)
839   {
840     char buf[1024];
841     snprintf (buf, 1024, "%s (%d) [%s] critical: Unreachable line reached.",
842               file, line, function);
843   
844     throw std::runtime_error (buf);
845   }
846   
847   void
848   throw_unless_internal (char const *file, int line, char const *function, char const *condition)
849   {
850     char buf[1024];
851     snprintf (buf, 1024, "%s (%d) [%s] critical: Assertion `%s' failed.",
852               file, line, function, condition);
853   
854     throw std::runtime_error (buf);
855   }
856   
857   void
858   throw_msg_unless_internal (char const *file, int line, char const *function, char const *message)
859   {
860     char buf[1024];
861     snprintf (buf, 1024, "%s (%d) [%s] critical: %s.",
862               file, line, function, message);
863   
864     throw std::runtime_error (buf);
865   }
866   
867   const Value Value::null;
868   const int Value::minInt = int (~ (unsigned (-1)/2));
869   const int Value::maxInt = int (unsigned (-1)/2);
870   const unsigned Value::maxUInt = unsigned (-1);
871
872   ValueAllocator::~ValueAllocator ()
873   {
874   }
875
876   class DefaultValueAllocator : public ValueAllocator
877   {
878   public:
879     virtual ~DefaultValueAllocator ()
880     {
881     }
882
883     virtual char *makeMemberName (char const *memberName)
884     {
885       return duplicateStringValue (memberName);
886     }
887
888     virtual void releaseMemberName (char *memberName)
889     {
890       releaseStringValue (memberName);
891     }
892
893     virtual char *duplicateStringValue (char const *value, unsigned length = unknown)
894     {
895       //@todo invesgate this old optimization
896 #if 0
897       if (!value || value[0] == 0)
898         return 0;
899 #endif
900
901       if (length == unknown)
902         length = (unsigned)strlen (value);
903       char *newString = static_cast<char *> (malloc (length + 1));
904       memcpy (newString, value, length);
905       newString[length] = 0;
906       return newString;
907     }
908
909     virtual void releaseStringValue (char *value)
910     {
911       if (value)
912         free (value);
913     }
914   };
915
916   static ValueAllocator *&valueAllocator ()
917   {
918     static DefaultValueAllocator defaultAllocator;
919     static ValueAllocator *valueAllocator = &defaultAllocator;
920     return valueAllocator;
921   }
922
923   static struct DummyValueAllocatorInitializer {
924     DummyValueAllocatorInitializer () 
925     {
926       valueAllocator ();      // ensure valueAllocator () statics are initialized before main ().
927     }
928   } dummyValueAllocatorInitializer;
929
930
931
932   Value::CZString::CZString (int index)
933     : cstr_ (0)
934     , index_ (index)
935   {
936   }
937
938   Value::CZString::CZString (char const *cstr, DuplicationPolicy allocate)
939     : cstr_ (allocate == duplicate ? valueAllocator()->makeMemberName (cstr) 
940              : cstr)
941     , index_ (allocate)
942   {
943   }
944
945   Value::CZString::CZString (const CZString &other)
946     : cstr_ (other.index_ != noDuplication &&  other.cstr_ != 0
947              ?  valueAllocator()->makeMemberName (other.cstr_)
948              : other.cstr_)
949     , index_ (other.cstr_ ? (other.index_ == noDuplication ? noDuplication : duplicate)
950               : other.index_)
951   {
952   }
953
954   Value::CZString::~CZString ()
955   {
956     if (cstr_ && index_ == duplicate)
957       valueAllocator()->releaseMemberName (const_cast<char *> (cstr_));
958   }
959
960   void 
961   Value::CZString::swap (CZString &other)
962   {
963     std::swap (cstr_, other.cstr_);
964     std::swap (index_, other.index_);
965   }
966
967   Value::CZString &
968   Value::CZString::operator = (const CZString &other)
969   {
970     CZString temp (other);
971     swap (temp);
972     return *this;
973   }
974
975   bool 
976   Value::CZString::operator < (const CZString &other) const 
977   {
978     if (cstr_)
979       return strcmp (cstr_, other.cstr_) < 0;
980     return index_ < other.index_;
981   }
982
983   bool 
984   Value::CZString::operator == (const CZString &other) const 
985   {
986     if (cstr_)
987       return strcmp (cstr_, other.cstr_) == 0;
988     return index_ == other.index_;
989   }
990
991
992   int 
993   Value::CZString::index () const
994   {
995     return index_;
996   }
997
998
999   char const *
1000   Value::CZString::c_str () const
1001   {
1002     return cstr_;
1003   }
1004
1005   bool 
1006   Value::CZString::isStaticString () const
1007   {
1008     return index_ == noDuplication;
1009   }
1010
1011
1012   // class Value::Value
1013
1014   Value::Value (ValueType type)
1015     : type_ (type)
1016     , allocated_ (0)
1017   {
1018     switch (type)
1019       {
1020       case nullValue:
1021         break;
1022       case intValue:
1023       case uintValue:
1024         value_.int_ = 0;
1025         break;
1026       case realValue:
1027         value_.real_ = 0.0;
1028         break;
1029       case stringValue:
1030         value_.string_ = 0;
1031         break;
1032       case arrayValue:
1033       case objectValue:
1034         value_.map_ = new ObjectValues ();
1035         break;
1036       case booleanValue:
1037         value_.bool_ = false;
1038         break;
1039       default:
1040         throw_unreachable;
1041       }
1042   }
1043
1044
1045   Value::Value (int value)
1046     : type_ (intValue)
1047   {
1048     value_.int_ = value;
1049   }
1050
1051
1052   Value::Value (unsigned value)
1053     : type_ (uintValue)
1054   {
1055     value_.uint_ = value;
1056   }
1057
1058   Value::Value (double value)
1059     : type_ (realValue)
1060   {
1061     value_.real_ = value;
1062   }
1063
1064   Value::Value (char const *value)
1065     : type_ (stringValue)
1066     , allocated_ (true)
1067   {
1068     value_.string_ = valueAllocator()->duplicateStringValue (value);
1069   }
1070
1071   Value::Value (const std::string &value)
1072     : type_ (stringValue)
1073     , allocated_ (true)
1074   {
1075     value_.string_ = valueAllocator()->duplicateStringValue (value.c_str (), (unsigned)value.length ());
1076   }
1077
1078   Value::Value (const StaticString &value)
1079     : type_ (stringValue)
1080     , allocated_ (false)
1081   {
1082     value_.string_ = const_cast<char *> (value.c_str ());
1083   }
1084
1085
1086   Value::Value (bool value)
1087     : type_ (booleanValue)
1088   {
1089     value_.bool_ = value;
1090   }
1091
1092
1093   Value::Value (const Value &other)
1094     : type_ (other.type_)
1095   {
1096     switch (type_)
1097       {
1098       case nullValue:
1099       case intValue:
1100       case uintValue:
1101       case realValue:
1102       case booleanValue:
1103         value_ = other.value_;
1104         break;
1105       case stringValue:
1106         if (other.value_.string_)
1107           {
1108             value_.string_ = valueAllocator()->duplicateStringValue (other.value_.string_);
1109             allocated_ = true;
1110           }
1111         else
1112           value_.string_ = 0;
1113         break;
1114       case arrayValue:
1115       case objectValue:
1116         value_.map_ = new ObjectValues (*other.value_.map_);
1117         break;
1118       default:
1119         throw_unreachable;
1120       }
1121   }
1122
1123
1124   Value::~Value ()
1125   {
1126     switch (type_)
1127       {
1128       case nullValue:
1129       case intValue:
1130       case uintValue:
1131       case realValue:
1132       case booleanValue:
1133         break;
1134       case stringValue:
1135         if (allocated_)
1136           valueAllocator()->releaseStringValue (value_.string_);
1137         break;
1138       case arrayValue:
1139       case objectValue:
1140         delete value_.map_;
1141         break;
1142       default:
1143         throw_unreachable;
1144       }
1145   }
1146
1147   Value &
1148   Value::operator = (const Value &other)
1149   {
1150     Value temp (other);
1151     swap (temp);
1152     return *this;
1153   }
1154
1155   void 
1156   Value::swap (Value &other)
1157   {
1158     ValueType temp = type_;
1159     type_ = other.type_;
1160     other.type_ = temp;
1161     std::swap (value_, other.value_);
1162     int temp2 = allocated_;
1163     allocated_ = other.allocated_;
1164     other.allocated_ = temp2;
1165   }
1166
1167   ValueType 
1168   Value::type () const
1169   {
1170     return type_;
1171   }
1172
1173   bool 
1174   Value::operator < (const Value &other) const
1175   {
1176     int typeDelta = type_ - other.type_;
1177     if (typeDelta)
1178       return typeDelta < 0 ? true : false;
1179     switch (type_)
1180       {
1181       case nullValue:
1182         return false;
1183       case intValue:
1184         return value_.int_ < other.value_.int_;
1185       case uintValue:
1186         return value_.uint_ < other.value_.uint_;
1187       case realValue:
1188         return value_.real_ < other.value_.real_;
1189       case booleanValue:
1190         return value_.bool_ < other.value_.bool_;
1191       case stringValue:
1192         return (value_.string_ == 0 && other.value_.string_)
1193           || (other.value_.string_  
1194               && value_.string_  
1195               && strcmp (value_.string_, other.value_.string_) < 0);
1196       case arrayValue:
1197       case objectValue:
1198         {
1199           int delta = int (value_.map_->size () - other.value_.map_->size ());
1200           if (delta)
1201             return delta < 0;
1202           return (*value_.map_) < (*other.value_.map_);
1203         }
1204       default:
1205         throw_unreachable;
1206       }
1207     return 0;  // unreachable
1208   }
1209
1210   bool 
1211   Value::operator <= (const Value &other) const
1212   {
1213     return !(other > *this);
1214   }
1215
1216   bool 
1217   Value::operator >= (const Value &other) const
1218   {
1219     return !(*this < other);
1220   }
1221
1222   bool 
1223   Value::operator > (const Value &other) const
1224   {
1225     return other < *this;
1226   }
1227
1228   bool 
1229   Value::operator == (const Value &other) const
1230   {
1231     if (type_ != other.type_)
1232       return false;
1233
1234     switch (type_)
1235       {
1236       case nullValue:
1237         return true;
1238       case intValue:
1239         return value_.int_ == other.value_.int_;
1240       case uintValue:
1241         return value_.uint_ == other.value_.uint_;
1242       case realValue:
1243         return value_.real_ == other.value_.real_;
1244       case booleanValue:
1245         return value_.bool_ == other.value_.bool_;
1246       case stringValue:
1247         return (value_.string_ == other.value_.string_)
1248           || (other.value_.string_  
1249               && value_.string_  
1250               && strcmp (value_.string_, other.value_.string_) == 0);
1251       case arrayValue:
1252       case objectValue:
1253         return value_.map_->size () == other.value_.map_->size ()
1254           && (*value_.map_) == (*other.value_.map_);
1255       default:
1256         throw_unreachable;
1257       }
1258     return 0;  // unreachable
1259   }
1260
1261   bool 
1262   Value::operator != (const Value &other) const
1263   {
1264     return !(*this == other);
1265   }
1266
1267   Value::operator char const * () const
1268   {
1269     throw_unless (type_ == stringValue);
1270     return value_.string_;
1271   }
1272
1273
1274   Value::operator std::string () const
1275   {
1276     switch (type_)
1277       {
1278       case nullValue:
1279         return "";
1280       case stringValue:
1281         return value_.string_ ? value_.string_ : "";
1282       case booleanValue:
1283         return value_.bool_ ? "true" : "false";
1284       case intValue:
1285       case uintValue:
1286       case realValue:
1287       case arrayValue:
1288       case objectValue:
1289         throw_msg_unless (false, "Type is not convertible to string");
1290       default:
1291         throw_unreachable;
1292       }
1293     return ""; // unreachable
1294   }
1295
1296   Value::operator int () const
1297   {
1298     switch (type_)
1299       {
1300       case nullValue:
1301         return 0;
1302       case intValue:
1303         return value_.int_;
1304       case uintValue:
1305         throw_msg_unless (value_.uint_ < (unsigned)maxInt, "integer out of signed integer range");
1306         return value_.uint_;
1307       case realValue:
1308         throw_msg_unless (value_.real_ >= minInt && value_.real_ <= maxInt, "Real out of signed integer range");
1309         return int (value_.real_);
1310       case booleanValue:
1311         return value_.bool_ ? 1 : 0;
1312       case stringValue:
1313       case arrayValue:
1314       case objectValue:
1315         throw_msg_unless (false, "Type is not convertible to int");
1316       default:
1317         throw_unreachable;
1318       }
1319     return 0; // unreachable;
1320   }
1321
1322   Value::operator unsigned () const
1323   {
1324     switch (type_)
1325       {
1326       case nullValue:
1327         return 0;
1328       case intValue:
1329         throw_msg_unless (value_.int_ >= 0, "Negative integer can not be converted to unsigned integer");
1330         return value_.int_;
1331       case uintValue:
1332         return value_.uint_;
1333       case realValue:
1334         throw_msg_unless (value_.real_ >= 0 && value_.real_ <= maxUInt,  "Real out of unsigned integer range");
1335         return unsigned (value_.real_);
1336       case booleanValue:
1337         return value_.bool_ ? 1 : 0;
1338       case stringValue:
1339       case arrayValue:
1340       case objectValue:
1341         throw_msg_unless (false, "Type is not convertible to uint");
1342       default:
1343         throw_unreachable;
1344       }
1345     return 0; // unreachable;
1346   }
1347
1348   Value::operator double () const
1349   {
1350     switch (type_)
1351       {
1352       case nullValue:
1353         return 0.0;
1354       case intValue:
1355         return value_.int_;
1356       case uintValue:
1357         return value_.uint_;
1358       case realValue:
1359         return value_.real_;
1360       case booleanValue:
1361         return value_.bool_ ? 1.0 : 0.0;
1362       case stringValue:
1363       case arrayValue:
1364       case objectValue:
1365         throw_msg_unless (false, "Type is not convertible to double");
1366       default:
1367         throw_unreachable;
1368       }
1369     return 0; // unreachable;
1370   }
1371
1372   Value::operator bool () const
1373   {
1374     switch (type_)
1375       {
1376       case nullValue:
1377         return false;
1378       case intValue:
1379       case uintValue:
1380         return value_.int_ != 0;
1381       case realValue:
1382         return value_.real_ != 0.0;
1383       case booleanValue:
1384         return value_.bool_;
1385       case stringValue:
1386         return value_.string_ && value_.string_[0] != 0;
1387       case arrayValue:
1388       case objectValue:
1389         return value_.map_->size () != 0;
1390       default:
1391         throw_unreachable;
1392       }
1393     return false; // unreachable;
1394   }
1395
1396
1397   bool 
1398   Value::isConvertibleTo (ValueType other) const
1399   {
1400     switch (type_)
1401       {
1402       case nullValue:
1403         return true;
1404       case intValue:
1405         return (other == nullValue && value_.int_ == 0)
1406           || other == intValue
1407           || (other == uintValue  && value_.int_ >= 0)
1408           || other == realValue
1409           || other == stringValue
1410           || other == booleanValue;
1411       case uintValue:
1412         return (other == nullValue && value_.uint_ == 0)
1413           || (other == intValue  && value_.uint_ <= (unsigned)maxInt)
1414           || other == uintValue
1415           || other == realValue
1416           || other == stringValue
1417           || other == booleanValue;
1418       case realValue:
1419         return (other == nullValue && value_.real_ == 0.0)
1420           || (other == intValue && value_.real_ >= minInt && value_.real_ <= maxInt)
1421           || (other == uintValue && value_.real_ >= 0 && value_.real_ <= maxUInt)
1422           || other == realValue
1423           || other == stringValue
1424           || other == booleanValue;
1425       case booleanValue:
1426         return (other == nullValue && value_.bool_ == false)
1427           || other == intValue
1428           || other == uintValue
1429           || other == realValue
1430           || other == stringValue
1431           || other == booleanValue;
1432       case stringValue:
1433         return other == stringValue
1434           || (other == nullValue && (!value_.string_ || value_.string_[0] == 0));
1435       case arrayValue:
1436         return other == arrayValue
1437           || (other == nullValue && value_.map_->size () == 0);
1438       case objectValue:
1439         return other == objectValue
1440           || (other == nullValue && value_.map_->size () == 0);
1441       default:
1442         throw_unreachable;
1443       }
1444     return false; // unreachable;
1445   }
1446
1447
1448   /// Number of values in array or object
1449   unsigned 
1450   Value::size () const
1451   {
1452     switch (type_)
1453       {
1454       case nullValue:
1455       case intValue:
1456       case uintValue:
1457       case realValue:
1458       case booleanValue:
1459       case stringValue:
1460         return 0;
1461       case arrayValue:  // size of the array is highest index + 1
1462         if (!value_.map_->empty ())
1463           {
1464             ObjectValues::const_iterator itLast = value_.map_->end ();
1465             --itLast;
1466             return itLast->first.index ()+1;
1467           }
1468         return 0;
1469       case objectValue:
1470         return int (value_.map_->size ());
1471       default:
1472         throw_unreachable;
1473       }
1474     return 0; // unreachable;
1475   }
1476
1477
1478   bool 
1479   Value::empty () const
1480   {
1481     if (isNull () || isArray () || isObject ())
1482       return size () == 0u;
1483     else
1484       return false;
1485   }
1486
1487
1488   bool
1489   Value::operator ! () const
1490   {
1491     return isNull ();
1492   }
1493
1494
1495   void 
1496   Value::clear ()
1497   {
1498     throw_unless (type_ == nullValue || type_ == arrayValue  || type_ == objectValue);
1499
1500     switch (type_)
1501       {
1502       case arrayValue:
1503       case objectValue:
1504         value_.map_->clear ();
1505         break;
1506       default:
1507         break;
1508       }
1509   }
1510
1511   void 
1512   Value::resize (unsigned newSize)
1513   {
1514     throw_unless (type_ == nullValue || type_ == arrayValue);
1515     if (type_ == nullValue)
1516       *this = Value (arrayValue);
1517     unsigned oldSize = size ();
1518     if (newSize == 0)
1519       clear ();
1520     else if (newSize > oldSize)
1521       (*this)[ newSize - 1 ];
1522     else
1523       {
1524         for (unsigned index = newSize; index < oldSize; ++index)
1525           value_.map_->erase (index);
1526         throw_unless (size () == newSize);
1527       }
1528   }
1529
1530
1531   Value &
1532   Value::operator [] (int index)
1533   {
1534     return operator [] (static_cast<unsigned> (index));
1535   }
1536
1537
1538   Value &
1539   Value::operator [] (unsigned index)
1540   {
1541     throw_unless (type_ == nullValue || type_ == arrayValue);
1542     if (type_ == nullValue)
1543       *this = Value (arrayValue);
1544     CZString key (index);
1545     ObjectValues::iterator it = value_.map_->lower_bound (key);
1546     if (it != value_.map_->end () && it->first == key)
1547       return it->second;
1548
1549     ObjectValues::value_type defaultValue (key, null);
1550     it = value_.map_->insert (it, defaultValue);
1551     return it->second;
1552   }
1553
1554
1555   const Value &
1556   Value::operator [] (int index) const
1557   {
1558     return operator [] (static_cast<unsigned> (index));
1559   }
1560
1561
1562   const Value &
1563   Value::operator [] (unsigned index) const
1564   {
1565     throw_unless (type_ == nullValue || type_ == arrayValue);
1566     if (type_ == nullValue)
1567       return null;
1568     CZString key (index);
1569     ObjectValues::const_iterator it = value_.map_->find (key);
1570     if (it == value_.map_->end ())
1571       return null;
1572     return it->second;
1573   }
1574
1575
1576   Value &
1577   Value::operator [] (char const *key)
1578   {
1579     return resolveReference (key, false);
1580   }
1581
1582
1583   Value &
1584   Value::resolveReference (char const *key, bool isStatic)
1585   {
1586     throw_unless (type_ == nullValue || type_ == objectValue);
1587     if (type_ == nullValue)
1588       *this = Value (objectValue);
1589     CZString actualKey (key, isStatic ? CZString::noDuplication 
1590                         : CZString::duplicateOnCopy);
1591     ObjectValues::iterator it = value_.map_->lower_bound (actualKey);
1592     if (it != value_.map_->end () && it->first == actualKey)
1593       return it->second;
1594
1595     ObjectValues::value_type defaultValue (actualKey, null);
1596     it = value_.map_->insert (it, defaultValue);
1597     Value &value = it->second;
1598     return value;
1599   }
1600
1601
1602   Value 
1603   Value::get (int index, const Value &defaultValue) const
1604   {
1605     return get (static_cast<unsigned> (index), defaultValue);
1606   }
1607
1608
1609   Value 
1610   Value::get (unsigned index, const Value &defaultValue) const
1611   {
1612     const Value *value = &((*this)[index]);
1613     return value == &null ? defaultValue : *value;
1614   }
1615
1616
1617   bool 
1618   Value::isValidIndex (int index) const
1619   {
1620     return isValidIndex (static_cast<unsigned> (index));
1621   }
1622
1623
1624   bool 
1625   Value::isValidIndex (unsigned index) const
1626   {
1627     return index < size ();
1628   }
1629
1630
1631
1632   const Value &
1633   Value::operator [] (char const *key) const
1634   {
1635     throw_unless (type_ == nullValue || type_ == objectValue);
1636     if (type_ == nullValue)
1637       return null;
1638     CZString actualKey (key, CZString::noDuplication);
1639     ObjectValues::const_iterator it = value_.map_->find (actualKey);
1640     if (it == value_.map_->end ())
1641       return null;
1642     return it->second;
1643   }
1644
1645
1646   Value &
1647   Value::operator [] (const std::string &key)
1648   {
1649     return (*this)[ key.c_str () ];
1650   }
1651
1652
1653   const Value &
1654   Value::operator [] (const std::string &key) const
1655   {
1656     return (*this)[ key.c_str () ];
1657   }
1658
1659   Value &
1660   Value::operator [] (const StaticString &key)
1661   {
1662     return resolveReference (key, true);
1663   }
1664
1665
1666   Value &
1667   Value::append (const Value &value)
1668   {
1669     return (*this)[size ()] = value;
1670   }
1671
1672
1673   Value 
1674   Value::get (char const *key, const Value &defaultValue) const
1675   {
1676     const Value *value = &((*this)[key]);
1677     return value == &null ? defaultValue : *value;
1678   }
1679
1680
1681   Value 
1682   Value::get (const std::string &key, const Value &defaultValue) const
1683   {
1684     return get (key.c_str (), defaultValue);
1685   }
1686
1687   Value
1688   Value::removeMember (char const *key)
1689   {
1690     throw_unless (type_ == nullValue || type_ == objectValue);
1691     if (type_ == nullValue)
1692       return null;
1693     CZString actualKey (key, CZString::noDuplication);
1694     ObjectValues::iterator it = value_.map_->find (actualKey);
1695     if (it == value_.map_->end ())
1696       return null;
1697     Value old (it->second);
1698     value_.map_->erase (it);
1699     return old;
1700   }
1701
1702   Value
1703   Value::removeMember (const std::string &key)
1704   {
1705     return removeMember (key.c_str ());
1706   }
1707
1708   bool 
1709   Value::isMember (char const *key) const
1710   {
1711     const Value *value = &((*this)[key]);
1712     return value != &null;
1713   }
1714
1715
1716   bool 
1717   Value::isMember (const std::string &key) const
1718   {
1719     return isMember (key.c_str ());
1720   }
1721
1722
1723   Value::Members 
1724   Value::getMemberNames () const
1725   {
1726     throw_unless (type_ == nullValue || type_ == objectValue);
1727     if (type_ == nullValue)
1728       return Value::Members ();
1729     Members members;
1730     members.reserve (value_.map_->size ());
1731     ObjectValues::const_iterator it;
1732     ObjectValues::const_iterator itEnd = value_.map_->end ();
1733     for (it = value_.map_->begin (); it != itEnd; ++it)
1734       members.push_back (std::string (it->first.c_str()));
1735     return members;
1736   }
1737
1738   bool
1739   Value::isNull () const
1740   {
1741     return type_ == nullValue;
1742   }
1743
1744
1745   bool 
1746   Value::isBool () const
1747   {
1748     return type_ == booleanValue;
1749   }
1750
1751
1752   bool 
1753   Value::isInt () const
1754   {
1755     return type_ == intValue;
1756   }
1757
1758
1759   bool 
1760   Value::isUInt () const
1761   {
1762     return type_ == uintValue;
1763   }
1764
1765
1766   bool 
1767   Value::isIntegral () const
1768   {
1769     return type_ == intValue  
1770       || type_ == uintValue  
1771       || type_ == booleanValue;
1772   }
1773
1774
1775   bool 
1776   Value::isDouble () const
1777   {
1778     return type_ == realValue;
1779   }
1780
1781
1782   bool 
1783   Value::isNumeric () const
1784   {
1785     return isIntegral () || isDouble ();
1786   }
1787
1788
1789   bool 
1790   Value::isString () const
1791   {
1792     return type_ == stringValue;
1793   }
1794
1795
1796   bool 
1797   Value::isArray () const
1798   {
1799     return type_ == nullValue || type_ == arrayValue;
1800   }
1801
1802
1803   bool 
1804   Value::isObject () const
1805   {
1806     return type_ == nullValue || type_ == objectValue;
1807   }
1808
1809
1810   Value::const_iterator 
1811   Value::begin () const
1812   {
1813     switch (type_)
1814       {
1815       case arrayValue:
1816       case objectValue:
1817         if (value_.map_)
1818           return const_iterator (value_.map_->begin ());
1819         break;
1820       default:
1821         break;
1822       }
1823     return const_iterator ();
1824   }
1825
1826   Value::const_iterator 
1827   Value::end () const
1828   {
1829     switch (type_)
1830       {
1831       case arrayValue:
1832       case objectValue:
1833         if (value_.map_)
1834           return const_iterator (value_.map_->end ());
1835         break;
1836       default:
1837         break;
1838       }
1839     return const_iterator ();
1840   }
1841
1842
1843   Value::iterator 
1844   Value::begin ()
1845   {
1846     switch (type_)
1847       {
1848       case arrayValue:
1849       case objectValue:
1850         if (value_.map_)
1851           return iterator (value_.map_->begin ());
1852         break;
1853       default:
1854         break;
1855       }
1856     return iterator ();
1857   }
1858
1859   Value::iterator 
1860   Value::end ()
1861   {
1862     switch (type_)
1863       {
1864       case arrayValue:
1865       case objectValue:
1866         if (value_.map_)
1867           return iterator (value_.map_->end ());
1868         break;
1869       default:
1870         break;
1871       }
1872     return iterator ();
1873   }
1874 } // namespace json
1875
1876 namespace json
1877 {
1878   static void uintToString (unsigned value, 
1879                             char *&current)
1880   {
1881     *--current = 0;
1882     do
1883       {
1884         *--current = (value % 10) + '0';
1885         value /= 10;
1886       }
1887     while (value != 0);
1888   }
1889
1890   std::string valueToString (int value)
1891   {
1892     char buffer[32];
1893     char *current = buffer + sizeof (buffer);
1894     bool isNegative = value < 0;
1895     if (isNegative)
1896       value = -value;
1897     uintToString (unsigned (value), current);
1898     if (isNegative)
1899       *--current = '-';
1900     throw_unless (current >= buffer);
1901     return current;
1902   }
1903
1904
1905   std::string valueToString (unsigned value)
1906   {
1907     char buffer[32];
1908     char *current = buffer + sizeof (buffer);
1909     uintToString (value, current);
1910     throw_unless (current >= buffer);
1911     return current;
1912   }
1913
1914   std::string valueToString (double value)
1915   {
1916     char buffer[32];
1917     sprintf (buffer, "%.16g", value); 
1918     return buffer;
1919   }
1920
1921
1922   std::string valueToString (bool value)
1923   {
1924     return value ? "true" : "false";
1925   }
1926
1927   std::string valueToQuotedString (char const *value)
1928   {
1929     // Not sure how to handle unicode...
1930     if (std::strpbrk (value, "\"\\\b\f\n\r\t") == NULL)
1931       return std::string ("\"") + value + "\"";
1932     // We have to walk value and escape any special characters.
1933     // Appending to std::string is not efficient, but this should be rare.
1934     // (Note: forward slashes are *not* rare, but I am not escaping them.)
1935     unsigned maxsize = strlen (value) * 2 + 3; // allescaped+quotes+NULL
1936     std::string result;
1937     result.reserve (maxsize); // to avoid lots of mallocs
1938     result += "\"";
1939     for (char const* c=value; *c != 0; ++c){
1940       switch (*c){
1941       case '\"':
1942         result += "\\\"";
1943         break;
1944       case '\\':
1945         result += "\\\\";
1946         break;
1947       case '\b':
1948         result += "\\b";
1949         break;
1950       case '\f':
1951         result += "\\f";
1952         break;
1953       case '\n':
1954         result += "\\n";
1955         break;
1956       case '\r':
1957         result += "\\r";
1958         break;
1959       case '\t':
1960         result += "\\t";
1961         break;
1962       case '/':
1963         // Even though \/ is considered a legal escape in JSON, a bare
1964         // slash is also legal, so I see no reason to escape it.
1965         // (I hope I am not misunderstanding something.)
1966       default:
1967         result += *c;
1968       }
1969     }
1970     result += "\"";
1971     return result;
1972   }
1973
1974   // Class Writer
1975   std::string 
1976   Writer::write (const Value &root)
1977   {
1978     document_ = "";
1979     writeValue (root);
1980     document_ += "\n";
1981     return document_;
1982   }
1983
1984
1985   void 
1986   Writer::writeValue (const Value &value)
1987   {
1988     switch (value.type ())
1989       {
1990       case nullValue:
1991         document_ += "null";
1992         break;
1993       case intValue:
1994         document_ += valueToString (static_cast<int> (value));
1995         break;
1996       case uintValue:
1997         document_ += valueToString (static_cast<unsigned> (value));
1998         break;
1999       case realValue:
2000         document_ += valueToString (static_cast<double> (value));
2001         break;
2002       case stringValue:
2003         document_ += valueToQuotedString (static_cast<char const *> (value));
2004         break;
2005       case booleanValue:
2006         document_ += valueToString (static_cast<bool> (value));
2007         break;
2008       case arrayValue:
2009         {
2010           document_ += "[";
2011           int size = value.size ();
2012           for (int index = 0; index < size; ++index)
2013             {
2014               if (index > 0)
2015                 document_ += ",";
2016               writeValue (value[index]);
2017             }
2018           document_ += "]";
2019         }
2020         break;
2021       case objectValue:
2022         {
2023           Value::Members members (value.getMemberNames ());
2024           document_ += "{";
2025           for (Value::Members::iterator it = members.begin (); 
2026                 it != members.end (); 
2027                 ++it)
2028             {
2029               const std::string &name = *it;
2030               if (it != members.begin ())
2031                 document_ += ",";
2032               document_ += valueToQuotedString (name.c_str ());
2033               document_ += ":";
2034               writeValue (value[name]);
2035             }
2036           document_ += "}";
2037         }
2038         break;
2039       }
2040   }
2041 } // namespace json
2042
2043 /**
2044  * RPC
2045  */
2046
2047 namespace json
2048 {
2049   namespace rpc
2050   {
2051     typedef std::map<std::string, void (*) (HTTPRequest *, Value &, Value &)> method_map;
2052   
2053     method_map methods;
2054   
2055     void
2056     add_method (char *name, method mth)
2057     {
2058       methods[name] = mth;
2059     }
2060   
2061     void
2062     system_list_methods (HTTPRequest *http, Value &request, Value &response)
2063     {
2064       unsigned i = 0;
2065       Value method_list (arrayValue);
2066     
2067       method_map::iterator it;
2068       for (it = methods.begin(); it != methods.end(); ++it)
2069         {
2070           method_list[i] = Value (it->first);
2071           i++;
2072         }
2073     
2074       response["result"] = method_list;
2075     }
2076
2077     void
2078     init ()
2079     {
2080       add_method ("system.listMethods", &system_list_methods);
2081     }
2082   
2083     void
2084     service (HTTPRequest *http, Value &request, Value &response)
2085     {
2086       char const *methodName = static_cast<char const *> (request["method"]);
2087       
2088       method_map::iterator mth = methods.find (methodName);
2089       if (mth != methods.end ())
2090         (*mth->second) (http, request, response);
2091     }
2092     
2093     void
2094     process (HTTPRequest *http, std::string &response_text, char const *request_text)
2095     {
2096       std::string text;
2097       bool parse_success;
2098       Value request (objectValue);
2099       Value response (objectValue);
2100       Reader r;
2101       Writer w;
2102   
2103       parse_success = r.parse (request_text, request_text + strlen (request_text), request);
2104   
2105       service (http, request, response);
2106   
2107       text = w.write (response);
2108   
2109       response_text = text.c_str ();
2110   
2111       return;
2112     }
2113   } // namespace rpc
2114 } // namespace json
2115
2116 MODULE_INIT(ModuleRpcJson)