public function export($var) { $var = $this->_formatVarAsJSON($var); $string = json_encode($var); foreach ($this->_jsonParams as $index => $value) { $string = str_replace("\"" . $this->_param($index) . "\"", $value, $string); } return json_unicode_to_utf8(json_format($string)); }
/** * Format JSON to pretty html * * @param string $json JSON to format * @return string */ function json_format_html($json) { $json = json_unicode_to_utf8($json); $tab = " "; $new_json = ""; $indent_level = 0; $in_string = false; $len = strlen($json); for ($c = 0; $c < $len; $c++) { $char = $json[$c]; switch ($char) { case '{': case '[': $char = "<font color=\"green\">" . $char . "</font>"; //iwind if (!$in_string) { $new_json .= $char . "<br/>" . str_repeat($tab, $indent_level + 1); $indent_level++; } else { $new_json .= "["; } break; case '}': case ']': $char = "<font color=\"green\">" . $char . "</font>"; //iwind if (!$in_string) { $indent_level--; $new_json .= "<br/>" . str_repeat($tab, $indent_level) . $char; } else { $new_json .= "]"; } break; case ',': $char = "<font color=\"green\">" . $char . "</font>"; //iwind if (!$in_string) { $new_json .= ",<br/>" . str_repeat($tab, $indent_level); } else { $new_json .= ","; } break; case ':': $char = "<font color=\"green\">" . $char . "</font>"; //iwind if ($in_string) { $new_json .= ":"; } else { $new_json .= $char; } break; case '"': if ($c > 0 && $json[$c - 1] != '\\') { $in_string = !$in_string; if ($in_string) { $new_json .= "<font color=\"#DD0000\" class=\"string_var\">" . $char; } else { $new_json .= $char . "</font>"; } break; } else { if ($c == 0) { $in_string = !$in_string; $new_json .= "<font color=\"red\">" . $char; break; } } default: if (!$in_string && trim($char) !== "") { $char = "<font color=\"blue\">" . $char . "</font>"; } else { if ($char == "&" || $char == "'" || $char == "\"" || $char == "<" || $char == ">") { $char = htmlspecialchars($char); } } $new_json .= $char; break; } } $new_json = preg_replace_callback("{(<font color=\"blue\">([\\da-zA-Z_\\.]+)</font>)+}", create_function('$match', ' $string = str_replace("<font color=\\"blue\\">", "", $match[0]); $string = str_replace("</font>", "", $string); return "<font color=\\"blue\\" class=\\"no_string_var\\">" . $string . "</font>"; '), $new_json); return $new_json; }
function json_format_html($json) { $json = json_unicode_to_utf8($json); $tab = " "; $new_json = ""; $indent_level = 0; $in_string = false; /* commented out by monk.e.boy 22nd May '08 because my web server is PHP4, and json_* are PHP5 functions... $json_obj = json_decode($json); if($json_obj === false) return false; $json = json_encode($json_obj); */ $len = strlen($json); for ($c = 0; $c < $len; $c++) { $char = $json[$c]; switch ($char) { case '{': case '[': $char = "<font color=\"green\">" . $char . "</font>"; //iwind if (!$in_string) { $new_json .= $char . "<br/>" . str_repeat($tab, $indent_level + 1); $indent_level++; } else { $new_json .= $char; } break; case '}': case ']': $char = "<font color=\"green\">" . $char . "</font>"; //iwind if (!$in_string) { $indent_level--; $new_json .= "<br/>" . str_repeat($tab, $indent_level) . $char; } else { $new_json .= $char; } break; case ',': $char = "<font color=\"green\">" . $char . "</font>"; //iwind if (!$in_string) { $new_json .= ",<br/>" . str_repeat($tab, $indent_level); } else { $new_json .= $char; } break; case ':': $char = "<font color=\"green\">" . $char . "</font>"; //iwind if ($in_string) { $new_json .= ": "; } else { $new_json .= $char; } break; case '"': if ($c > 0 && $json[$c - 1] != '\\') { $in_string = !$in_string; if ($in_string) { $new_json .= "<font color=\"#DD0000\">" . $char; } else { $new_json .= $char . "</font>"; } break; } else { if ($c == 0) { $in_string = !$in_string; $new_json .= "<font color=\"red\">" . $char; break; } } default: if (!$in_string && trim($char) !== "") { $char = "<font color=\"blue\">" . $char . "</font>"; } else { $char = htmlspecialchars($char); } $new_json .= $char; break; } } $new_json = preg_replace_callback("{(<font color=\"blue\">([\\da-zA-Z_\\.]+)</font>)+}", create_function('$match', ' $string = str_replace("<font color=\\"blue\\">", "", $match[0]); $string = str_replace("</font>", "", $string); return "<font color=\\"blue\\" class=\\"no_string_var\\">" . $string . "</font>"; '), $new_json); return $new_json; }
/** * load field data * */ function doLoad() { $collection = $this->_mongodb->selectCollection($this->collection); $id = xn("id"); $field = xn("field"); $type = "integer"; $data = null; if ($id) { $one = $collection->findOne(array("_id" => rock_real_id($id))); //not select field, because there is a bug in list, such as "list.0" $data = rock_array_get($one, $field); switch (gettype($data)) { case "boolean": $type = "boolean"; break; case "integer": $type = "integer"; break; case "long": $type = "long"; break; case "float": case "double": $type = "double"; break; case "string": $type = "string"; break; case "array": $type = "mixed"; break; case "object": // int64 is returned as object (Kyryl Bilokurov <*****@*****.**>) if (get_class($data) == "MongoInt64") { $type = "long"; } else { $type = "mixed"; } break; case "resource": $type = "mixed"; break; case "NULL": $type = "null"; break; } } $exporter = new VarExportor($this->_mongodb, $data); $format = rock_cookie("rock_format", "json"); $represent = $exporter->export($format); if ($format == "json") { $represent = json_unicode_to_utf8($represent); } $this->_outputJson(array("code" => 200, "type" => $type, "value" => $type == "long" ? $data->__toString() : $data, "represent" => $represent, "format" => $format)); }
/** modify one row **/ public function doModifyRow() { $this->db = xn("db"); $this->collection = xn("collection"); $id = rock_real_id(xn("id")); //selected format last time $this->last_format = rock_cookie("rock_format", "json"); import("lib.mongo.RQuery"); $query = new RQuery($this->_mongo, $this->db, $this->collection); $this->row = $query->id($id)->findOne(); if (empty($this->row)) { $this->error = "Record is not found."; $this->display(); return; } $this->data = $this->row; unset($this->data["_id"]); import("classes.VarExportor"); $export = new VarExportor($query->db(), $this->data); $this->data = $export->export($this->last_format); if ($this->last_format == "json") { $this->data = json_unicode_to_utf8($this->data); } if ($this->isPost()) { $this->data = xn("data"); $format = x("format"); $this->last_format = $format; $row = null; $eval = new VarEval($this->data, $format, $this->_mongo->selectDb($this->db)); $row = $eval->execute(); if ($row === false || !is_array($row)) { $this->error = "Only valid {$format} is accepted."; $this->display(); return; } $query = new RQuery($this->_mongo, $this->db, $this->collection); $obj = $query->id($this->row["_id"])->find(); $oldAttrs = $obj->attrs(); $obj->setAttrs($row); foreach ($oldAttrs as $oldAttr => $oldValue) { if ($oldAttr == "_id") { continue; } if (!array_key_exists($oldAttr, $row)) { $obj->remove($oldAttr); } } try { $obj->save(); } catch (Exception $e) { $this->error = $e->getMessage(); $this->display(); return; } //remember format choice $this->_rememberFormat($format); $this->message = "Updated successfully."; } $this->display(); }
private function _exportJSON() { if (function_exists('json_encode')) { $service = 'json_encode'; } else { import("classes.Services_JSON"); $json = new Services_JSON(); $service = array(&$json, 'encode'); } $var = $this->_formatVarAsJSON($this->_var, $service); $string = call_user_func($service, $var); $params = array(); foreach ($this->_jsonParams as $index => $value) { $param = $this->_param($index); if (preg_match("/^r_code:(.*)\$/s", $value, $match)) { $param = '"' . $param . '"'; $value = $match[1]; } $params[$param] = $value; } return json_unicode_to_utf8(json_format(strtr($string, $params))); }
private function _exportJSON() { if (function_exists("json_encode")) { $service = "json_encode"; } else { import("classes.Services_JSON"); $json = new Services_JSON(); $service = array(&$json, 'encode'); } $var = $this->_formatVarAsJSON($this->_var, $service); $string = call_user_func($service, $var); //Remove "\/" escape $string = str_replace('\\/', "/", $string); $params = array(); foreach ($this->_jsonParams as $index => $value) { $params['"' . $this->_param($index) . '"'] = $value; } return json_unicode_to_utf8(json_format(strtr($string, $params))); }
private function _exportJSON() { if (function_exists('json_encode')) { $service = 'json_encode'; } else { import("classes.Services_JSON"); $json = new Services_JSON(); $service = array(&$json, 'encode'); } $var = $this->_formatVarAsJSON($this->_var, $service); $string = call_user_func($service, $var); foreach ($this->_jsonParams as $index => $value) { $string = str_replace("\"" . $this->_param($index) . "\"", $value, $string); } return json_unicode_to_utf8(json_format($string)); }