source: Dev/branches/rest-dojo-ui/jQueryUI/server/rdfapi/syntax/JsonSerializer.php @ 312

Last change on this file since 312 was 312, checked in by jkraaijeveld, 13 years ago
File size: 4.1 KB
Line 
1<?php
2/**
3 * This class provides capabilities to serialize MemModels to json strings.
4 *
5 * @package syntax
6 * @author Philipp Frischmuth <philipp@frischmuth24.de>
7 * @version $Id: JsonSerializer.php 555 2007-12-11 20:27:34Z p_frischmuth $
8 */
9class JsonSerializer extends Object {
10       
11        /**
12         * This method takes a MemModel object ad parameter and serializes all contained triples to rdf/json format.
13         *
14         * @see http://n2.talis.com/wiki/RDF_JSON_Specification#rdf.2Fjson
15         * @param MemModel $model
16         * @return string Returns a string containing the serialized model.
17         */
18        public function serialize(MemModel $model) {
19               
20                // create the root json object (root object)
21                $jsonString = '{';
22                $subjects = array();
23               
24                // sort triples by subject
25                foreach ($model->triples as $triple) {
26                        $subjects[$triple->toStringSubject()][] = $triple;
27                }
28               
29                // sort alphabetically
30                ksort($subjects);
31                       
32                // triples are sorted by subject now, each key is a subject uri, containing all triples with this subject uri
33                $i = 0;
34                foreach ($subjects as $predicatesArray) {
35                        $predicates = array();
36                       
37                        if ($i > 0) {
38                                $jsonString .= ',';
39                        }
40                        $i++;
41                       
42                        $subj = $predicatesArray[0]->getSubject();
43                       
44                        // add special _: sequence for blank node only
45                        if ($subj instanceof BlankNode) {
46                                $jsonString .= '"_:' . $this->_escapeValue($subj->getLabel()) . '":';
47                        } else {
48                                $jsonString .= '"' . $this->_escapeValue($subj->getLabel()) . '":';
49                        }
50                       
51                       
52                       
53                        // create a json object for each subject (subject object)
54                        $jsonString .= '{';
55                       
56                        // sort triples with current subject by predicate
57                        foreach ($predicatesArray as $triple) {
58                                $predicates[$triple->toStringPredicate()][] = $triple;
59                        }
60                       
61                        // sort alphabetically
62                        ksort($predicates);
63                       
64                        $j = 0;
65                        foreach ($predicates as $valueArray) {
66                               
67                                if ($j > 0) {
68                                        $jsonString .= ',';
69                                }
70                                $j++;
71                               
72                                $jsonString .= '"' . $this->_escapeValue($valueArray[0]->getLabelPredicate()) . '":';
73                               
74                                // create a json array (value array)
75                                $jsonString .= '[';
76                               
77                                $k = 0;
78                                foreach ($valueArray as $triple) {
79                                        if ($k > 0) {
80                                                $jsonString .= ',';
81                                        }
82                                        $k++;
83                                       
84                                        // create json value object (value object)
85                                        $jsonString .= '{';
86                                       
87                                        $obj = $triple->getObject();
88                                       
89                                        // add special _: sequence for blank nodes only
90                                        if ($obj instanceof BlankNode) {
91                                                $jsonString .= '"value":"_:' . $this->_escapeValue($obj->getLabel()) . '",';
92                                        } else if ($obj instanceof Literal) {
93                                                $jsonString .= '"value":"' . $this->_escapeValue($obj->getLabel()) . '",';
94                                        } else {
95                                                $jsonString .= '"value":"' . $this->_escapeValue($obj->getLabel()) . '",';
96                                        }
97                                       
98                                        // add type of object
99                                        if ($obj instanceof Literal) {
100                                                $jsonString .= '"type":"literal"';
101                                        } else if ($obj instanceof BlankNode) {
102                                                $jsonString .= '"type":"bnode"';
103                                        } else {
104                                                $jsonString .= '"type":"uri"';
105                                        }
106                                       
107                                        if ($obj instanceof Literal) {
108                                                if ($obj->getLanguage() != '') {
109                                                        $jsonString .= ',"lang":"' . $this->_escapeValue($obj->getLanguage()) . '"';
110                                                }
111                                                if ($obj->getDatatype() != '') {
112                                                        $jsonString .= ',"datatype":"' . $this->_escapeValue($obj->getDatatype()) . '"';
113                                                }
114                                               
115                                        }
116                                       
117                                        // close value object
118                                        $jsonString .= '}';
119                                }
120                               
121                                // close the value array
122                                $jsonString .= ']';
123                        }
124                       
125                        // close the json object (for the subject) (subject object)
126                        $jsonString .= '}';
127                }
128               
129                // close root json object (root object)
130                $jsonString .= '}';
131               
132                return $jsonString;
133        }
134       
135        /*
136         * Escapes the following chars as specified at json.org:
137         *
138         * " -> \"
139         * \ -> \\
140         * / -> \/
141         * \b -> \\b
142         * \f -> \\f
143         * \n -> \\n
144         * \r -> \\r
145         * \t -> \\t
146         * \uXXXX -> \\uXXXX
147         */
148        protected function _escapeValue($value) {
149               
150               
151                $value = str_replace("\\", '\\\\', $value);
152                #$value = str_replace("/", '\/', $value);
153                $value = str_replace("\n", '\\n', $value);
154                $value = str_replace("\t", '\\t', $value);
155                $value = str_replace("\r", '\\r', $value);
156                $value = str_replace("\b", '\\b', $value);
157                $value = str_replace("\f", '\\f', $value);
158                $value = str_replace('"', '\"', $value);
159               
160                return $value;
161        }
162}
163?>
Note: See TracBrowser for help on using the repository browser.