Search:

# CWIS Developers Documentation

- Main Page
- Classes
- Files

- Search for |

- File List
- File Members

# JsonHelper.php

Go to the documentation of this file.

    00001 <?PHP
    00002
    00003 class JsonHelper
    00004 {
    00005
    00006     private $Data;
    00007     private $Warnings;
    00008
    00009     public function __construct()
    00010     {
    00011         $this->Data = array();
    00012         $this->Warnings = array();
    00013     }
    00014
    00015     public function AddDatum($Key, $Value)
    00016     {
    00017         $this->Data[$Key] = $Value;
    00018     }
    00019
    00020     public function AddWarning($Message)
    00021     {
    00022         $this->Warnings[] = strval($Message);
    00023     }
    00024
    00025     public function Error($Message)
    00026     {
    00027         $this->SendResult($this->GenerateResult("ERROR", $Message));
    00028     }
    00029
    00030     public function Success($Message="")
    00031     {
    00032         $this->SendResult($this->GenerateResult("OK", $Message));
    00033     }
    00034
    00035     private function SendResult(array $Result)
    00036     {
    00037         header("Content-Type: application/json;  charset=utf-8");
    00038         print $this->ArrayToJson($Result);
    00039     }
    00040
    00041     private function GenerateResult($State, $Message)
    00042     {
    00043         return array(
    00044             "data" => $this->Data,
    00045             "status" => array(
    00046                 "state" => strval($State),
    00047                 "message" => strval($Message),
    00048                 "numWarnings" => count($this->Warnings),
    00049                 "warnings" => $this->Warnings));
    00050     }
    00051
    00052     private function ArrayToJson(array $Array)
    00053     {
    00054         # initialize
    00055         $JSON = "";
    00056         $counter = 0;
    00057
    00058         foreach ($Array as $key => $value)
    00059         {
    00060             # add key
    00061             $JSON .= "'".$key."':";
    00062
    00063             # recur if the value is an array
    00064             if (is_array($value))
    00065             {
    00066                 $JSON .= $this->ArrayToJson($value);
    00067             }
    00068
    00069             # add the value
    00070             else
    00071             {
    00072                 # escape
    00073                 $value = $this->ConvertSmartQuotes($value);
    00074                 $value = str_replace("\r", "", $value);
    00075                 $value = htmlentities($value, ENT_QUOTES);
    00076
    00077                 # if the value is a number, parse it and don't put in quotes
    00078                 if (is_numeric($value))
    00079                 {
    00080                     $JSON .= intval($value);
    00081                 }
    00082
    00083                 # for everything else, get the string value and put in quotes
    00084                 else
    00085                 {
    00086                     $JSON .= "'".strval($value)."'";
    00087                 }
    00088             }
    00089
    00090             # add comma if necessary
    00091             $counter++;
    00092             $JSON .= ($counter < count($Array)) ? "," : "" ;
    00093         }
    00094
    00095         # return JSON-ified array
    00096         return "{".$JSON."}";
    00097     }
    00098
    00099     private function ConvertSmartQuotes($String)
    00100     {
    00101         $search = array(chr(145), chr(146), chr(147), chr(148), chr(151));
    00102         $replace = array("'", "'", '"', '"', '-');
    00103         return str_replace($search, $replace, $String);
    00104     }
    00105
    00106 }
    00107
    00108 ?>

Copyright 2009 Internet Scout