5 # Part of the Collection Workflow Integration System (CWIS)
6 # Copyright 2002-2013 Edward Almasy and Internet Scout Research Group
7 # http://scout.wisc.edu/cwis/
22 $this->Data = array();
23 $this->Warnings = array();
33 $this->Data[$Key] = $Value;
45 $this->Warnings[] = strval($Message);
57 $this->SendResult($this->GenerateResult(
"ERROR", $Message));
67 $this->SendResult($this->GenerateResult(
"OK", $Message));
78 private function SendResult(array $Result)
81 header(
"Content-Type: application/json; charset="
82 .$SysConfig->DefaultCharacterSet(), TRUE);
83 $this->PrintArrayToJson($Result);
94 private function GenerateResult($State, $Message)
97 "data" => $this->Data,
99 "state" => strval($State),
100 "message" => strval($Message),
101 "numWarnings" => count($this->Warnings),
102 "warnings" => $this->Warnings));
109 private function PrintArrayToJson(array $Array)
111 # variables needed for printing commas if necessary
113 $Count = count($Array);
115 # determine whether or not we have a true array or a hash map
117 for ($i = 0, reset($Array); $i < count($Array); $i++, next($Array))
119 if (key($Array) !== $i)
127 print ($TrueArray) ?
"[" :
"{";
130 foreach ($Array as $key => $value)
132 # replacements so we can escape strings and replace smart quotes
133 static $Replace = array(
134 array(
"\\",
"/",
"\n",
"\t",
"\r",
"\b",
"\f",
'"',
"",
"",
"",
"",
""),
135 array(
'\\\\',
'\\/',
'\\n',
'\\t',
'\\r',
'\\b',
'\\f',
'\"',
"'",
"'",
'\"',
'\"',
'-'));
137 # print key if a hash map
140 # escape, remove smart quotes, and print the key
141 print
'"'.str_replace($Replace[0], $Replace[1], $key).
'":';
144 # scalar values (int, float, string, or boolean)
145 if (is_scalar($value))
147 # numeric (i.e., float, int, or float/int string)
148 if (is_numeric($value))
154 else if (is_string($value))
156 # escape, remove smart quotes, and print the value
157 print
'"'.str_replace($Replace[0], $Replace[1], $value).
'"';
161 else if ($value === TRUE)
167 else if ($value === FALSE)
173 # recur if the value is an array
174 else if (is_array($value))
176 $this->PrintArrayToJson($value);
180 else if (is_null($value))
185 # object, just print the name and don't possibly expose secret details
186 else if (is_object($value))
188 print
'"object('.get_class($value).
')"';
191 # resource, just print the name and don't possibly expose secret details
194 print
'"resource('.get_resource_type($value).
')"';
197 # print comma if necessary
198 if (++$Offset < $Count) { print
","; }
202 print ($TrueArray) ?
"]" :
"}";
AddWarning($Message)
Add a warning message to export in the JSON response.
Success($Message="")
Signal that the callback was successful and optionally set a message.
AddDatum($Key, $Value)
Add a datum identified by a key to export in the JSON response.
Convenience class for standardizing JSON responses, making it easier to export primitive data types t...
__construct()
Object constructor.
Error($Message)
Add an error message to export in the JSON response and then send the response.