fork download
  1. <?php
  2.  
  3. class rapidjson {
  4. private $data = '';
  5. public function StartObject() {$this->data .= '{';}
  6. public function EndObject() {$this->data .= '}';}
  7. public function BeginArray() {$this->data .= '[';}
  8. public function EndArray() {$this->data .= ']';}
  9. public function Int($value) {$this->data .= strval(intval($value)) . ', ';}
  10. public function Key($value) {$this->data .= '"' . addslashes($value) . '": ';}
  11. public function String($value){$this->data .= '"' . addslashes($value) . '", ';}
  12. public function Bool($value) {$this->data .= '"' . ($value ? 'true' : 'false') . '", ';}
  13. public function __toString() {return $this->data;}
  14. }
  15.  
  16. $writer = new rapidjson();
  17.  
  18. $writer->StartObject();
  19.  
  20. $writer->Key("key");
  21. $writer->String("value");
  22.  
  23. $writer->Key("array_key");
  24. $writer->BeginArray();
  25.  
  26. $writer->Int(43);
  27. $writer->String("44");
  28. $writer->Bool(true);
  29.  
  30. $writer->EndArray();
  31.  
  32. $writer->EndObject();
  33.  
  34. echo $writer;
  35.  
Success #stdin #stdout 0s 82560KB
stdin
Standard input is empty
stdout
{"key": "value", "array_key": [43, "44", "true", ]}