wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.JSONFormat.php
Go to the documentation of this file.
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2014 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of any of the following licenses
7  * at your choice:
8  *
9  * - GNU Lesser General Public License (LGPL)
10  * http://www.gnu.org/licenses/lgpl.html
11  * - Eclipse Public License (EPL)
12  * http://www.eclipse.org/org/documents/epl-v10.php
13  *
14  * See the license.txt file distributed with this work for
15  * additional information.
16  *
17  * $Id: class.JSONFormat.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
19 require_once(BASE."wcmf/lib/util/class.JSONUtil.php");
20 require_once(BASE."wcmf/lib/model/class.NodeSerializer.php");
21 require_once(BASE."wcmf/lib/presentation/format/class.HierarchicalFormat.php");
22 
23 /**
24  * JSONFormatter collects the response data from all executed controllers
25  * into one response array and returns it all at once at the end of
26  * script execution. This prevents from having multiple junks of json
27  * from each controller response that can't be decoded by clients.
28  */
29 $GLOBALS['gJSONData'] = array();
30 $GLOBALS['gJSONUsed'] = false;
31 function gPrintJSONResult()
32 {
33  if ($GLOBALS['gJSONUsed'])
34  {
35  $data = $GLOBALS['gJSONData'];
36  if ($data != null)
37  {
38  $data = EncodingUtil::utf8EncodeMix(($data));
39  $encoded = JSONUtil::encode($data);
40  print($encoded);
41  }
42  }
43 }
44 register_shutdown_function('gPrintJSONResult');
45 
46 /**
47  * @class JSONFormat
48  * @ingroup Format
49  * @brief JSONFormat realizes the JSON request/response format. All data will
50  * be de-/serialized using the json_encode/json_encode method if avalaible or
51  * JSON.php as fallback, except for Nodes. Nodes are serialized into an array
52  * before encoding (see JSONFormat::serializeValue). On serialization the data
53  * will be outputted directly using the print command.
54  *
55  * @author ingo herwig <ingo@wemove.com>
56  */
58 {
59  /**
60  * @see HierarchicalFormat::beforeDeserialize()
61  */
62  function beforeDeserialize(&$data)
63  {
64  // decode the json data into an array
65  foreach(array_keys($data) as $key)
66  $data[$key] = &JSONUtil::decode($data[$key], true);
67  }
68 
69  /**
70  * @see HierarchicalFormat::afterSerialize()
71  */
72  function afterSerialize(&$data)
73  {
74  // merge data into global data array
75  // new values override old
76  $GLOBALS['gJSONData'] = array_merge($GLOBALS['gJSONData'], $data);
77  $GLOBALS['gJSONUsed'] = true;
78  }
79 
80  /**
81  * @see HierarchicalFormat::isSerializedNode()
82  */
83  function isSerializedNode($key, &$value)
84  {
85  return ((is_object($value) || is_array($value)) && isset($value['oid']) && isset($value['type']));
86  }
87 
88  /**
89  * @see HierarchicalFormat::serializeNode()
90  */
91  function serializeNode($key, &$value)
92  {
93  // use NodeSerializer to serialize
94  return NodeSerializer::serializeNode($value, false);
95  }
96 
97  /**
98  * @see HierarchicalFormat::deserializeNode()
99  */
100  function &deserializeNode($key, &$value)
101  {
102  if (is_array($value))
103  $type = $value['type'];
104  if (is_object($value))
105  $type = $value->type;
106 
107  // use NodeSerializer to deserialize
108  $node = &NodeSerializer::deserializeNode($type, $value, false);
109  return $node;
110  }
111 }
112 ?>
serializeNode($key, &$value)
afterSerialize(&$data)
& deserializeNode($type, $data, $hasFlattendedValues, $parent=null)
static utf8EncodeMix($input, $encodeKeys=false)
JSONFormat realizes the JSON request/response format. All data will be de-/serialized using the json_...
beforeDeserialize(&$data)
HierarchicalFormat maybe used as base class for formats that are able to represent hierarchical data ...
static decode($value, $assoc=false)
static encode($value)
isSerializedNode($key, &$value)
$GLOBALS['gJSONData']
gPrintJSONResult()
serializeNode(&$obj, $flattenValues)
& deserializeNode($key, &$value)