]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_rpc_json.cpp
44c1726ec4efdb3367fcab935459c8ec15aabe14
[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         void MthModuleVersion (HTTPRequest *http, json::Value &request, json::Value &response)
34         {
35                 Version v = this->GetVersion();
36                 std::string result = ConvToStr(v.Major) + "." + ConvToStr(v.Minor) + "." + ConvToStr(v.Revision) + "." + ConvToStr(v.Build);
37                 response["result"] = result;
38         }
39
40         void system_list_methods (HTTPRequest *http, json::Value &request, json::Value &response)
41         {
42                 unsigned i = 0;
43                 json::Value method_list (json::arrayValue);
44                 
45                 json::rpc::method_map::iterator it;
46                 for (it = json::rpc::methods.begin(); it != json::rpc::methods.end(); ++it)
47                 {
48                         method_list[i] = json::Value (it->first);
49                         i++;
50                 }
51                 
52                 response["result"] = method_list;
53         }
54
55  public:
56         ModuleRpcJson(InspIRCd* Me) : Module(Me)
57         {
58                 ServerInstance->PublishInterface("JSON-RPC", this);
59                 json::rpc::add_method ("system.listMethods", (Module *)this, (void (Module::*)(HTTPRequest*, json::Value&, json::Value&))&ModuleRpcJson::system_list_methods);
60                 json::rpc::add_method ("ircd.moduleVersion", (Module *)this, (void (Module::*)(HTTPRequest*, json::Value&, json::Value&))&ModuleRpcJson::MthModuleVersion);
61         }
62
63         void OnEvent(Event* event)
64         {
65                 std::stringstream data("");
66
67                 if (event->GetEventID() == "httpd_url")
68                 {
69                         HTTPRequest* http = (HTTPRequest*)event->GetData();
70
71                         if (http->GetURI() == "/jsonrpc" && http->GetType() == "POST")
72                         {
73                                 try
74                                 {
75                                         std::string response_text;
76                                         json::rpc::process (http, response_text, http->GetPostData().c_str());
77                                         data << response_text;
78                                 }
79                                 catch (std::runtime_error &)
80                                 {
81                                         data << "{ \"result\": \"JSON Fault\", \"error\": \"Invalid RPC call\", \"id\": 1}";
82                                 }
83
84                                 /* Send the document back to m_httpd */
85                                 HTTPDocument response(http->sock, &data, 200);
86                                 response.headers.SetHeader("X-Powered-By", "m_rpc_json.so");
87                                 response.headers.SetHeader("Content-Type", "application/json; charset=iso-8859-1");
88                                 response.headers.SetHeader("Connection", "Keep-Alive");
89                                 Request req((char*)&response, (Module*)this, event->GetSource());
90                                 req.Send();
91                         }
92                 }
93         }
94
95         void Implements(char* List)
96         {
97                 List[I_OnEvent] = 1;
98         }
99
100         virtual ~ModuleRpcJson()
101         {
102                 ServerInstance->UnpublishInterface("JSON-RPC", this);
103         }
104
105         virtual Version GetVersion()
106         {
107                 return Version(0, 1, 0, 0, VF_VENDOR, API_VERSION);
108         }
109 };
110
111 namespace json
112 {
113   ValueIteratorBase::ValueIteratorBase ()
114   {
115   }
116
117
118   ValueIteratorBase::ValueIteratorBase (const Value::ObjectValues::iterator &current)
119     : current_ (current)
120   {
121   }
122
123   Value &
124   ValueIteratorBase::deref () const
125   {
126     return current_->second;
127   }
128
129
130   void 
131   ValueIteratorBase::increment ()
132   {
133     ++current_;
134   }
135
136
137   void 
138   ValueIteratorBase::decrement ()
139   {
140     --current_;
141   }
142
143
144   ValueIteratorBase::difference_type 
145   ValueIteratorBase::computeDistance (const SelfType &other) const
146   {
147     return difference_type (std::distance (current_, other.current_));
148   }
149
150
151   bool 
152   ValueIteratorBase::isEqual (const SelfType &other) const
153   {
154     return current_ == other.current_;
155   }
156
157
158   void 
159   ValueIteratorBase::copy (const SelfType &other)
160   {
161     current_ = other.current_;
162   }
163
164
165   Value 
166   ValueIteratorBase::key () const
167   {
168     const Value::CZString czstring = (*current_).first;
169     if (czstring.c_str ())
170       {
171         if (czstring.isStaticString ())
172           return Value (StaticString (czstring.c_str ()));
173         return Value (czstring.c_str ());
174       }
175     return Value (czstring.index ());
176   }
177
178
179   unsigned 
180   ValueIteratorBase::index () const
181   {
182     const Value::CZString czstring = (*current_).first;
183     if (!czstring.c_str ())
184       return czstring.index ();
185     return unsigned (-1);
186   }
187
188
189   char const *
190   ValueIteratorBase::memberName () const
191   {
192     char const *name = (*current_).first.c_str ();
193     return name ? name : "";
194   }
195
196
197   ValueConstIterator::ValueConstIterator ()
198   {
199   }
200
201
202   ValueConstIterator::ValueConstIterator (const Value::ObjectValues::iterator &current)
203     : ValueIteratorBase (current)
204   {
205   }
206
207   ValueConstIterator &
208   ValueConstIterator::operator = (const ValueIteratorBase &other)
209   {
210     copy (other);
211     return *this;
212   }
213
214
215   ValueIterator::ValueIterator ()
216   {
217   }
218
219
220   ValueIterator::ValueIterator (const Value::ObjectValues::iterator &current)
221     : ValueIteratorBase (current)
222   {
223   }
224
225   ValueIterator::ValueIterator (const ValueConstIterator &other)
226     : ValueIteratorBase (other)
227   {
228   }
229
230   ValueIterator::ValueIterator (const ValueIterator &other)
231     : ValueIteratorBase (other)
232   {
233   }
234
235   ValueIterator &
236   ValueIterator::operator = (const SelfType &other)
237   {
238     copy (other);
239     return *this;
240   }
241 }
242
243 namespace json
244 {
245   inline bool 
246   in (char c, char c1, char c2, char c3, char c4)
247   {
248     return c == c1 || c == c2 || c == c3 || c == c4;
249   }
250
251   inline bool 
252   in (char c, char c1, char c2, char c3, char c4, char c5)
253   {
254     return c == c1 || c == c2 || c == c3 || c == c4 || c == c5;
255   }
256
257
258   Reader::Reader ()
259   {
260   }
261
262   bool
263   Reader::parse (const std::string &document, 
264                  Value &root)
265   {
266     document_ = document;
267     char const *begin = document_.c_str ();
268     char const *end = begin + document_.length ();
269     return parse (begin, end, root);
270   }
271
272   bool
273   Reader::parse (std::istream& sin,
274                  Value &root)
275   {
276     std::string doc;
277     std::getline (sin, doc, (char)EOF);
278     return parse (doc, root);
279   }
280
281   bool 
282   Reader::parse (char const *beginDoc, char const *endDOc, 
283                  Value &root)
284   {
285     begin_ = beginDoc;
286     end_ = endDOc;
287     current_ = begin_;
288     lastValueEnd_ = 0;
289     lastValue_ = 0;
290     errors_.clear ();
291     while (!nodes_.empty ())
292       nodes_.pop ();
293     nodes_.push (&root);
294    
295     bool successful = readValue ();
296     return successful;
297   }
298
299
300   bool
301   Reader::readValue ()
302   {
303     Token token;
304     do
305       readToken (token);
306     while (token.type_ == tokenComment);
307     bool successful = true;
308
309     switch (token.type_)
310       {
311       case tokenObjectBegin:
312         successful = readObject ();
313         break;
314       case tokenArrayBegin:
315         successful = readArray ();
316         break;
317       case tokenNumber:
318         successful = decodeNumber (token);
319         break;
320       case tokenString:
321         successful = decodeString (token);
322         break;
323       case tokenTrue:
324         currentValue () = true;
325         break;
326       case tokenFalse:
327         currentValue () = false;
328         break;
329       case tokenNull:
330         currentValue () = Value ();
331         break;
332       default:
333         return addError ("Syntax error: value, object or array expected.", token);
334       }
335
336     return successful;
337   }
338
339
340   bool 
341   Reader::expectToken (TokenType type, Token &token, char const *message)
342   {
343     readToken (token);
344     if (token.type_ != type)
345       return addError (message, token);
346     return true;
347   }
348
349
350   bool 
351   Reader::readToken (Token &token)
352   {
353     skipSpaces ();
354     token.start_ = current_;
355     char c = getNextChar ();
356     bool ok = true;
357     switch (c)
358       {
359       case '{':
360         token.type_ = tokenObjectBegin;
361         break;
362       case '}':
363         token.type_ = tokenObjectEnd;
364         break;
365       case '[':
366         token.type_ = tokenArrayBegin;
367         break;
368       case ']':
369         token.type_ = tokenArrayEnd;
370         break;
371       case '"':
372         token.type_ = tokenString;
373         ok = readString ();
374         break;
375 #if 0
376 #ifdef __GNUC__
377       case '0'...'9':
378 #endif
379 #else
380       case '0': case '1': case '2': case '3':
381       case '4': case '5': case '6': case '7':
382       case '8': case '9':
383 #endif
384       case '-':
385         token.type_ = tokenNumber;
386         readNumber ();
387         break;
388       case 't':
389         token.type_ = tokenTrue;
390         ok = match ("rue", 3);
391         break;
392       case 'f':
393         token.type_ = tokenFalse;
394         ok = match ("alse", 4);
395         break;
396       case 'n':
397         token.type_ = tokenNull;
398         ok = match ("ull", 3);
399         break;
400       case ',':
401         token.type_ = tokenArraySeparator;
402         break;
403       case ':':
404         token.type_ = tokenMemberSeparator;
405         break;
406       case 0:
407         token.type_ = tokenEndOfStream;
408         break;
409       default:
410         ok = false;
411         break;
412       }
413     if (!ok)
414       token.type_ = tokenError;
415     token.end_ = current_;
416     return true;
417   }
418
419
420   void 
421   Reader::skipSpaces ()
422   {
423     while (current_ != end_)
424       {
425         char c = *current_;
426         if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
427           ++current_;
428         else
429           break;
430       }
431   }
432
433
434   bool 
435   Reader::match (Location pattern, int patternLength)
436   {
437     if (end_ - current_ < patternLength)
438       return false;
439     int index = patternLength;
440     while (index--)
441       if (current_[index] != pattern[index])
442         return false;
443     current_ += patternLength;
444     return true;
445   }
446
447
448   void 
449   Reader::readNumber ()
450   {
451     while (current_ != end_)
452       {
453         if (!(*current_ >= '0' && *current_ <= '9')  &&
454              !in (*current_, '.', 'e', 'E', '+', '-'))
455           break;
456         ++current_;
457       }
458   }
459
460   bool
461   Reader::readString ()
462   {
463     char c = 0;
464     while (current_ != end_)
465       {
466         c = getNextChar ();
467         if (c == '\\')
468           getNextChar ();
469         else if (c == '"')
470           break;
471       }
472     return c == '"';
473   }
474
475
476   bool 
477   Reader::readObject ()
478   {
479     Token tokenName;
480     std::string name;
481     currentValue () = Value (objectValue);
482     while (readToken (tokenName))
483       {
484         if (tokenName.type_ == tokenObjectEnd && name.empty ())  // empty object
485           return true;
486         if (tokenName.type_ != tokenString)
487           break;
488       
489         name = "";
490         if (!decodeString (tokenName, name))
491           return recoverFromError (tokenObjectEnd);
492
493         Token colon;
494         if (!readToken (colon) ||  colon.type_ != tokenMemberSeparator)
495           {
496             return addErrorAndRecover ("Missing ':' after object member name", 
497                                        colon, 
498                                        tokenObjectEnd);
499           }
500         Value &value = currentValue ()[ name ];
501         nodes_.push (&value);
502         bool ok = readValue ();
503         nodes_.pop ();
504         if (!ok) // error already set
505           return recoverFromError (tokenObjectEnd);
506
507         Token comma;
508         if (!readToken (comma)
509              ||   (comma.type_ != tokenObjectEnd && 
510                    comma.type_ != tokenArraySeparator))
511           {
512             return addErrorAndRecover ("Missing ',' or '}' in object declaration", 
513                                        comma, 
514                                        tokenObjectEnd);
515           }
516         if (comma.type_ == tokenObjectEnd)
517           return true;
518       }
519     return addErrorAndRecover ("Missing '}' or object member name", 
520                                tokenName, 
521                                tokenObjectEnd);
522   }
523
524
525   bool 
526   Reader::readArray ()
527   {
528     currentValue () = Value (arrayValue);
529     skipSpaces ();
530     if (*current_ == ']') // empty array
531       {
532         Token endArray;
533         readToken (endArray);
534         return true;
535       }
536     int index = 0;
537     while (true)
538       {
539         Value &value = currentValue ()[ index++ ];
540         nodes_.push (&value);
541         bool ok = readValue ();
542         nodes_.pop ();
543         if (!ok) // error already set
544           return recoverFromError (tokenArrayEnd);
545
546         Token token;
547         if (!readToken (token) 
548              ||   (token.type_ != tokenArraySeparator && 
549                    token.type_ != tokenArrayEnd))
550           {
551             return addErrorAndRecover ("Missing ',' or ']' in array declaration", 
552                                        token, 
553                                        tokenArrayEnd);
554           }
555         if (token.type_ == tokenArrayEnd)
556           break;
557       }
558     return true;
559   }
560
561
562   bool 
563   Reader::decodeNumber (Token &token)
564   {
565     bool isDouble = false;
566     for (Location inspect = token.start_; inspect != token.end_; ++inspect)
567       {
568         isDouble = isDouble  
569           ||  in (*inspect, '.', 'e', 'E', '+')  
570           ||   (*inspect == '-' && inspect != token.start_);
571       }
572     if (isDouble)
573       return decodeDouble (token);
574     Location current = token.start_;
575     bool isNegative = *current == '-';
576     if (isNegative)
577       ++current;
578     unsigned threshold = (isNegative ? unsigned (-Value::minInt) 
579                              : Value::maxUInt) / 10;
580     unsigned value = 0;
581     while (current < token.end_)
582       {
583         char c = *current++;
584         if (c < '0'  ||  c > '9')
585           return addError ("'" + std::string (token.start_, token.end_) + "' is not a number.", token);
586         if (value >= threshold)
587           return decodeDouble (token);
588         value = value * 10 + unsigned (c - '0');
589       }
590     if (isNegative)
591       currentValue () = -int (value);
592     else if (value <= unsigned (Value::maxInt))
593       currentValue () = int (value);
594     else
595       currentValue () = value;
596     return true;
597   }
598
599
600   bool 
601   Reader::decodeDouble (Token &token)
602   {
603     double value = 0;
604     const int bufferSize = 32;
605     int count;
606     int length = int (token.end_ - token.start_);
607     if (length <= bufferSize)
608       {
609         char buffer[bufferSize];
610         memcpy (buffer, token.start_, length);
611         buffer[length] = 0;
612         count = sscanf (buffer, "%lf", &value);
613       }
614     else
615       {
616         std::string buffer (token.start_, token.end_);
617         count = sscanf (buffer.c_str (), "%lf", &value);
618       }
619
620     if (count != 1)
621       return addError ("'" + std::string (token.start_, token.end_) + "' is not a number.", token);
622     currentValue () = value;
623     return true;
624   }
625
626
627   bool 
628   Reader::decodeString (Token &token)
629   {
630     std::string decoded;
631     if (!decodeString (token, decoded))
632       return false;
633     currentValue () = decoded;
634     return true;
635   }
636
637
638   bool 
639   Reader::decodeString (Token &token, std::string &decoded)
640   {
641     Location current = token.start_ + 1; // skip '"'
642     Location end = token.end_ - 1;       // do not include '"'
643     decoded.reserve (long (end - current));
644
645     while (current != end)
646       {
647         char c = *current++;
648         if (expect_false (c == '"'))
649           break;
650         else if (expect_false (c == '\\'))
651           {
652             if (expect_false (current == end))
653               return addError ("Empty escape sequence in string", token, current);
654             char escape = *current++;
655             switch (escape)
656               {
657               case '"':
658               case '/':
659               case '\\': decoded += escape; break;
660
661               case 'b': decoded += '\010'; break;
662               case 't': decoded += '\011'; break;
663               case 'n': decoded += '\012'; break;
664               case 'f': decoded += '\014'; break;
665               case 'r': decoded += '\015'; break;
666               case 'u':
667                 {
668                   unsigned unicode;
669                   if (!decodeUnicodeEscapeSequence (token, current, end, unicode))
670                     return false;
671                   // @todo encode unicode as utf8.
672                   // @todo remember to alter the writer too.
673                 }
674                 break;
675               default:
676                 return addError ("Bad escape sequence in string", token, current);
677               }
678           }
679         else
680           {
681             decoded += c;
682           }
683       }
684
685     return true;
686   }
687
688
689   bool 
690   Reader::decodeUnicodeEscapeSequence (Token &token, 
691                                        Location &current, 
692                                        Location end, 
693                                        unsigned &unicode)
694   {
695     if (end - current < 4)
696       return addError ("Bad unicode escape sequence in string: four digits expected.", token, current);
697     unicode = 0;
698     for (int index = 0; index < 4; ++index)
699       {
700         char c = *current++;
701         unicode *= 16;
702         if (c >= '0' && c <= '9')
703           unicode += c - '0';
704         else if (c >= 'a' && c <= 'f')
705           unicode += c - 'a' + 10;
706         else if (c >= 'A' && c <= 'F')
707           unicode += c - 'A' + 10;
708         else
709           return addError ("Bad unicode escape sequence in string: hexadecimal digit expected.", token, current);
710       }
711     return true;
712   }
713
714
715   bool 
716   Reader::addError (const std::string &message, 
717                     Token &token,
718                     Location extra)
719   {
720     ErrorInfo info;
721     info.token_ = token;
722     info.message_ = message;
723     info.extra_ = extra;
724     errors_.push_back (info);
725     return false;
726   }
727
728
729   bool 
730   Reader::recoverFromError (TokenType skipUntilToken)
731   {
732     int errorCount = int (errors_.size ());
733     Token skip;
734     while (true)
735       {
736         if (!readToken (skip))
737           errors_.resize (errorCount); // discard errors caused by recovery
738         if (skip.type_ == skipUntilToken  ||  skip.type_ == tokenEndOfStream)
739           break;
740       }
741     errors_.resize (errorCount);
742     return false;
743   }
744
745
746   bool 
747   Reader::addErrorAndRecover (const std::string &message, 
748                               Token &token,
749                               TokenType skipUntilToken)
750   {
751     addError (message, token);
752     return recoverFromError (skipUntilToken);
753   }
754
755
756   Value &
757   Reader::currentValue ()
758   {
759     return *(nodes_.top ());
760   }
761
762
763   char 
764   Reader::getNextChar ()
765   {
766     if (current_ == end_)
767       return 0;
768     return *current_++;
769   }
770
771
772   void 
773   Reader::getLocationLineAndColumn (Location location,
774                                     int &line,
775                                     int &column) const
776   {
777     Location current = begin_;
778     Location lastLineStart = current;
779     line = 0;
780     while (current < location && current != end_)
781       {
782         char c = *current++;
783         if (c == '\r')
784           {
785             if (*current == '\n')
786               ++current;
787             lastLineStart = current;
788             ++line;
789           }
790         else if (c == '\n')
791           {
792             lastLineStart = current;
793             ++line;
794           }
795       }
796     // column & line start at 1
797     column = int (location - lastLineStart) + 1;
798     ++line;
799   }
800
801
802   std::string
803   Reader::getLocationLineAndColumn (Location location) const
804   {
805     int line, column;
806     getLocationLineAndColumn (location, line, column);
807     char buffer[18+16+16+1];
808     sprintf (buffer, "Line %d, Column %d", line, column);
809     return buffer;
810   }
811
812
813   std::string 
814   Reader::error_msgs () const
815   {
816     std::string formattedMessage;
817     for (Errors::const_iterator itError = errors_.begin ();
818           itError != errors_.end ();
819           ++itError)
820       {
821         const ErrorInfo &error = *itError;
822         formattedMessage += "* " + getLocationLineAndColumn (error.token_.start_) + "\n";
823         formattedMessage += "  " + error.message_ + "\n";
824         if (error.extra_)
825           formattedMessage += "See " + getLocationLineAndColumn (error.extra_) + " for detail.\n";
826       }
827     return formattedMessage;
828   }
829 } // namespace json
830
831 namespace json
832 {
833   void
834   unreachable_internal (char const *file, int line, char const *function)
835   {
836     char buf[1024];
837     snprintf (buf, 1024, "%s (%d) [%s] critical: Unreachable line reached.",
838               file, line, function);
839   
840     throw std::runtime_error (buf);
841   }
842   
843   void
844   throw_unless_internal (char const *file, int line, char const *function, char const *condition)
845   {
846     char buf[1024];
847     snprintf (buf, 1024, "%s (%d) [%s] critical: Assertion `%s' failed.",
848               file, line, function, condition);
849   
850     throw std::runtime_error (buf);
851   }
852   
853   void
854   throw_msg_unless_internal (char const *file, int line, char const *function, char const *message)
855   {
856     char buf[1024];
857     snprintf (buf, 1024, "%s (%d) [%s] critical: %s.",
858               file, line, function, message);
859   
860     throw std::runtime_error (buf);
861   }
862
863 #define throw_unreachable                       unreachable_internal (__FILE__, __LINE__, CURFUNC)
864 #define throw_unless(condition)                 if (!expect_false (condition)) throw_unless_internal (__FILE__, __LINE__, CURFUNC, #condition)
865 #define throw_msg_unless(condition, message)    if (!expect_false (condition)) throw_msg_unless_internal (__FILE__, __LINE__, CURFUNC, message)
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     method_map methods;
2052   
2053     void
2054     add_method (char *name, Module const *mod, method mth)
2055     {
2056       mfp m = { mod, mth };
2057       methods[name] = m;
2058     }
2059   
2060     void
2061     service (HTTPRequest *http, Value &request, Value &response)
2062     {
2063       char const *methodName = static_cast<char const *> (request["method"]);
2064       
2065       method_map::iterator mthit = methods.find (methodName);
2066       if (mthit != methods.end ())
2067         {
2068           mfp m = mthit->second;
2069           Module *mod = new Module (*m.mod);
2070           method mth = m.mth;
2071           (mod->*mth) (http, request, response);
2072           delete mod;
2073         }
2074     }
2075     
2076     void
2077     process (HTTPRequest *http, std::string &response_text, char const *request_text)
2078     {
2079       std::string text;
2080       bool parse_success;
2081       Value request (objectValue);
2082       Value response (objectValue);
2083       Reader r;
2084       Writer w;
2085       
2086       response["error"] = Value(nullValue);
2087       response["result"] = Value(nullValue);
2088       
2089       parse_success = r.parse (request_text, request_text + strlen (request_text), request);
2090       
2091       response["id"] = request["id"];
2092       
2093       service (http, request, response);
2094   
2095       text = w.write (response);
2096   
2097       response_text = text.c_str ();
2098   
2099       return;
2100     }
2101   } // namespace rpc
2102 } // namespace json
2103
2104 MODULE_INIT(ModuleRpcJson)