source: Dev/trunk/src/client/util/docscripts/lib/generator/JsonSerializer.php

Last change on this file was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 3.2 KB
Line 
1<?php
2
3class JsonSerializer extends Serializer
4{
5  protected $header = array('{');
6  protected $footer = array('}');
7
8  protected function lineStarts($line) {
9    if (preg_match('%^\t"([^"]+)": {%', $line, $match)) {
10      return $match[1];
11    }
12  }
13
14  protected function lineEnds($line) {
15    if (preg_match('%^\t}%', $line, $match)) {
16      return true;
17    }
18  }
19
20  protected function linesToRaw($lines) {
21    $lines[0] = '{';
22    $lines[count($lines)-1] = '}';
23    return json_decode(implode("\n", $lines));
24  }
25
26  public function toObject($raw, $id=null) {
27    return $raw;
28  }
29
30  public function toString($raw, $id=null) {
31    if (!$id) {
32      if (!($id = $raw['id'])) {
33        throw new Exception('toString must be passed an ID or raw object must contain and ID');
34      }
35    }
36
37    $tab = "\t";
38    $new_json = "\"$id\": ";
39    $indent_level = 0;
40    $in_string = false;
41
42    $json = json_encode($raw);
43    $len = strlen($json);
44
45    for ($c = 0; $c < $len; $c++) {
46      $char = $json{$c};
47      switch($char) {
48      case '{':
49      case '[':
50        if (!$in_string) {
51          $new_json .= $char . "\n" . str_repeat($tab, ++$indent_level);
52        }
53        else {
54          $new_json .= $char;
55        }
56        break;
57      case '}':
58      case ']':
59        if(!$in_string) {
60          $new_json .= "\n" . str_repeat($tab, --$indent_level) . $char;
61        }
62        else {
63          $new_json .= $char;
64        }
65        break;
66      case ',':
67        if (!$in_string) {
68          $new_json .= ",\n" . str_repeat($tab, $indent_level);
69        }
70        else {
71          $new_json .= $char;
72        }
73        break;
74      case ':':
75        if (!$in_string) {
76          $new_json .= ": ";
77        }
78        else {
79          $new_json .= $char;
80        }
81        break;
82      case '"':
83        if($c > 0 && $json[$c-1] != '\\') {
84          $in_string = !$in_string;
85        }
86      default:
87        $new_json .= $char;
88        break;
89      }
90    }
91
92    return $new_json . ",\n";
93  }
94
95  protected function descend($node, $object) {
96    if (!is_array($object) && !is_object($object)) {
97      return $object;
98    }
99
100    foreach ($object as $key => $value) {
101      switch ($key{0}) {
102      case '@':
103        $node[substr($key, 1)] = $value;
104        break;
105      case '#':
106        if ($key == '#mixins') {
107          foreach ($value as $mixins) {
108            $scope = $mixins['@scope'];
109            foreach ($mixins['#mixin'] as $mixin) {
110              $node['mixins'][$scope][] = $this->descend(array(), $mixin);
111            }
112          }
113        }
114        elseif (count($value) == 1 && !in_array($key, array('#property', '#method', '#resource', '#parameter', '#provide', '#return-type'))) {
115          $node[substr($key, 1)] = $this->descend(array(), $value[0]);
116        }
117        else {
118          foreach ($value as $child) {
119            $node[] = $this->descend(array(), $child);
120          }
121        }
122        break;
123      default:
124        if ($key === 'content') {
125          if (count($object) == 1) {
126            return $value;
127          }
128          $node['content'] = $value;
129        }
130      }
131    }
132
133    return $node;
134  }
135
136  public function convertToRaw($object) {
137    return $this->descend(array(), $object);
138  }
139}
140
141?>
Note: See TracBrowser for help on using the repository browser.