Permalink
Browse files

BUGFIX Quoting keys in JSONDataFormatter to ensure valid JSON (#5119)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@99853 467b73ca-7a2a-4603-9d3b-597d59a354a9
  • Loading branch information...
1 parent a4e14d3 commit 0b1fc5b91b78f021c8d09bc094a2fb2f7a445ed5 @chillu chillu committed Feb 25, 2010
Showing with 8 additions and 8 deletions.
  1. +8 −8 api/JSONDataFormatter.php
@@ -36,16 +36,16 @@ public function convertDataObject(DataObjectInterface $obj, $fields = null, $rel
$className = $obj->class;
$id = $obj->ID;
- $json = "{\n className : \"$className\",\n";
+ $json = "{\n \"className\" : \"$className\",\n";
foreach($this->getFieldsForObj($obj) as $fieldName => $fieldType) {
// Field filtering
if($fields && !in_array($fieldName, $fields)) continue;
$fieldValue = $obj->$fieldName;
if(is_object($fieldValue) && is_subclass_of($fieldValue, 'Object') && $fieldValue->hasMethod('toJSON')) {
- $jsonParts[] = "$fieldName : " . $fieldValue->toJSON();
+ $jsonParts[] = "\"$fieldName\" : " . $fieldValue->toJSON();
} else {
- $jsonParts[] = "$fieldName : " . Convert::raw2json($fieldValue);
+ $jsonParts[] = "\"$fieldName\" : " . Convert::raw2json($fieldValue);
}
}
@@ -63,7 +63,7 @@ public function convertDataObject(DataObjectInterface $obj, $fields = null, $rel
} else {
$href = Director::absoluteURL(self::$api_base . "$className/$id/$relName");
}
- $jsonParts[] = "$relName : { className : \"$relClass\", href : \"$href.json\", id : \"{$obj->$fieldName}\" }";
+ $jsonParts[] = "$relName : { \"className\" : \"$relClass\", \"href\" : \"$href.json\", \"id\" : \"{$obj->$fieldName}\" }";
}
foreach($obj->has_many() as $relName => $relClass) {
@@ -78,7 +78,7 @@ public function convertDataObject(DataObjectInterface $obj, $fields = null, $rel
foreach($items as $item) {
//$href = Director::absoluteURL(self::$api_base . "$className/$id/$relName/$item->ID");
$href = Director::absoluteURL(self::$api_base . "$relClass/$item->ID");
- $jsonInnerParts[] = "{ className : \"$relClass\", href : \"$href.json\", id : \"{$obj->$fieldName}\" }";
+ $jsonInnerParts[] = "{ \"className\" : \"$relClass\", \"href\" : \"$href.json\", \"id\" : \"{$obj->$fieldName}\" }";
}
$jsonParts[] = "$relName : [\n " . implode(",\n ", $jsonInnerParts) . " \n ]";
}
@@ -95,7 +95,7 @@ public function convertDataObject(DataObjectInterface $obj, $fields = null, $rel
foreach($items as $item) {
//$href = Director::absoluteURL(self::$api_base . "$className/$id/$relName/$item->ID");
$href = Director::absoluteURL(self::$api_base . "$relClass/$item->ID");
- $jsonInnerParts[] = " { className : \"$relClass\", href : \"$href.json\", id : \"{$obj->$fieldName}\" }";
+ $jsonInnerParts[] = " { \"className\" : \"$relClass\", \"href\" : \"$href.json\", \"id\" : \"{$obj->$fieldName}\" }";
}
$jsonParts[] = "$relName : [\n " . implode(",\n ", $jsonInnerParts) . "\n ]";
}
@@ -115,10 +115,10 @@ public function convertDataObjectSet(DataObjectSet $set, $fields = null) {
if($item->canView()) $jsonParts[] = $this->convertDataObject($item, $fields);
}
$json = "{\n";
- $json .= 'totalSize: ';
+ $json .= '\"totalSize\": ';
$json .= (is_numeric($this->totalSize)) ? $this->totalSize : 'null';
$json .= ",\n";
- $json .= "items: [\n" . implode(",\n", $jsonParts) . "\n]\n";
+ $json .= "\"items\": [\n" . implode(",\n", $jsonParts) . "\n]\n";
$json .= "}\n";
return $json;

0 comments on commit 0b1fc5b

Please sign in to comment.