File: JsonSerializer.php

package info (click to toggle)
dojo 1.10.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 97,980 kB
  • ctags: 10,348
  • sloc: php: 10,616; xml: 3,429; java: 3,098; sql: 928; sh: 484; pascal: 205; perl: 182; makefile: 77; python: 45; sed: 37; ruby: 10
file content (141 lines) | stat: -rw-r--r-- 3,237 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php

class JsonSerializer extends Serializer
{
  protected $header = array('{');
  protected $footer = array('}');

  protected function lineStarts($line) {
    if (preg_match('%^\t"([^"]+)": {%', $line, $match)) {
      return $match[1];
    }
  }

  protected function lineEnds($line) {
    if (preg_match('%^\t}%', $line, $match)) {
      return true;
    }
  }

  protected function linesToRaw($lines) {
    $lines[0] = '{';
    $lines[count($lines)-1] = '}';
    return json_decode(implode("\n", $lines));
  }

  public function toObject($raw, $id=null) {
    return $raw;
  }

  public function toString($raw, $id=null) {
    if (!$id) {
      if (!($id = $raw['id'])) {
        throw new Exception('toString must be passed an ID or raw object must contain and ID');
      }
    }

    $tab = "\t";
    $new_json = "\"$id\": ";
    $indent_level = 0;
    $in_string = false;

    $json = json_encode($raw);
    $len = strlen($json);

    for ($c = 0; $c < $len; $c++) {
      $char = $json{$c};
      switch($char) {
      case '{':
      case '[':
        if (!$in_string) {
          $new_json .= $char . "\n" . str_repeat($tab, ++$indent_level);
        }
        else {
          $new_json .= $char;
        }
        break;
      case '}':
      case ']':
        if(!$in_string) {
          $new_json .= "\n" . str_repeat($tab, --$indent_level) . $char;
        }
        else {
          $new_json .= $char;
        }
        break;
      case ',':
        if (!$in_string) {
          $new_json .= ",\n" . str_repeat($tab, $indent_level);
        }
        else {
          $new_json .= $char;
        }
        break;
      case ':':
        if (!$in_string) {
          $new_json .= ": ";
        }
        else {
          $new_json .= $char;
        }
        break;
      case '"':
        if($c > 0 && $json[$c-1] != '\\') {
          $in_string = !$in_string;
        }
      default:
        $new_json .= $char;
        break;
      }
    }

    return $new_json . ",\n";
  }

  protected function descend($node, $object) {
    if (!is_array($object) && !is_object($object)) {
      return $object;
    }

    foreach ($object as $key => $value) {
      switch ($key{0}) {
      case '@':
        $node[substr($key, 1)] = $value;
        break;
      case '#':
        if ($key == '#mixins') {
          foreach ($value as $mixins) {
            $scope = $mixins['@scope'];
            foreach ($mixins['#mixin'] as $mixin) {
              $node['mixins'][$scope][] = $this->descend(array(), $mixin);
            }
          }
        }
        elseif (count($value) == 1 && !in_array($key, array('#property', '#method', '#resource', '#parameter', '#provide', '#return-type'))) {
          $node[substr($key, 1)] = $this->descend(array(), $value[0]);
        }
        else {
          foreach ($value as $child) {
            $node[] = $this->descend(array(), $child);
          }
        }
        break;
      default:
        if ($key === 'content') {
          if (count($object) == 1) {
            return $value;
          }
          $node['content'] = $value;
        }
      }
    }

    return $node;
  }

  public function convertToRaw($object) {
    return $this->descend(array(), $object);
  }
}

?>