示例#1
0
 /**
  * Decodes the JSON string.
  *
  * Note that this parses it without casting objects to associative arrays.
  * Objects and arrays are kept as distinguishable types in the PHP values.
  *
  * @return Status
  */
 public function getData()
 {
     if ($this->jsonParse === null) {
         $this->jsonParse = FormatJson::parse($this->getNativeData());
     }
     return $this->jsonParse;
 }
示例#2
0
 public function jsonDecode($s, $flags)
 {
     $this->checkType('mw.text.jsonDecode', 1, $s, 'string');
     $this->checkTypeOptional('mw.text.jsonDecode', 2, $flags, 'number', 0);
     $flags = (int) $flags;
     $opts = FormatJson::FORCE_ASSOC;
     if ($flags & self::JSON_TRY_FIXING) {
         $opts |= FormatJson::TRY_FIXING;
     }
     $status = FormatJson::parse($s, $opts);
     if (!$status->isOk()) {
         throw new Scribunto_LuaError('mw.text.jsonDecode: ' . $status->getMessage()->text());
     }
     $val = $status->getValue();
     if (!($flags & self::JSON_PRESERVE_KEYS) && is_array($val)) {
         $val = self::reindexArrays($val, false);
     }
     return array($val);
 }
 /**
  * @dataProvider provideParseErrors
  * @param mixed $value
  */
 public function testParseErrors($value)
 {
     $st = FormatJson::parse($value);
     $this->assertType('Status', $st);
     $this->assertFalse($st->isOK());
 }
 /**
  * @covers FormatJson::parse
  * @covers FormatJson::stripComments
  * @dataProvider provideParseStripComments
  * @param string $json
  * @param mixed $expect
  */
 public function testParseStripComments($json, $expect)
 {
     $st = FormatJson::parse($json, FormatJson::STRIP_COMMENTS);
     $this->assertType('Status', $st);
     $this->assertTrue($st->isGood());
     $this->assertEquals($expect, $st->getValue());
 }
示例#5
0
 /**
  * Get the report from post body and turn into associative array.
  *
  * @return Array
  */
 private function getReport()
 {
     $postBody = $this->getRequest()->getRawInput();
     if (strlen($postBody) > self::MAX_POST_SIZE) {
         // paranoia, already checked content-length earlier.
         $this->error('toobig', __METHOD__);
     }
     $status = FormatJson::parse($postBody, FormatJson::FORCE_ASSOC);
     if (!$status->isGood()) {
         list($code, ) = $this->getErrorFromStatus($status);
         $this->error($code, __METHOD__);
     }
     $report = $status->getValue();
     if (!isset($report['csp-report'])) {
         $this->error('missingkey', __METHOD__);
     }
     return $report['csp-report'];
 }
 private function toArr($message, $params, $dictByDefault = false)
 {
     if (is_string($params) && $params) {
         $p = $params;
         if ($p[0] !== '[' && $p[0] !== '{') {
             $p = $dictByDefault ? '{' . $params . '}' : "[{$params}]";
         }
         $st = FormatJson::parse($p, FormatJson::FORCE_ASSOC);
         $this->assertTrue($st->isOK(), "{$message}: invalid JSON value {$params}");
         $params = $st->getValue();
     }
     return $params;
 }