Class: Haxe::Format::JsonPrinter

Inherits:
Object
  • Object
show all
Defined in:
lib/lib/haxe/format/json_printer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(replacer, space) ⇒ JsonPrinter

Returns a new instance of JsonPrinter.



8
9
10
11
12
13
14
# File 'lib/lib/haxe/format/json_printer.rb', line 8

def initialize(replacer,space)
  @replacer = replacer
  @indent = space
  @pretty = space != nil
  @nind = 0
  @buf = StringBuf.new
end

Instance Attribute Details

#bufObject

protected - in ruby this doesn’t play well with static/inline methods



18
19
20
# File 'lib/lib/haxe/format/json_printer.rb', line 18

def buf
  @buf
end

#indentObject

Returns the value of attribute indent.



20
21
22
# File 'lib/lib/haxe/format/json_printer.rb', line 20

def indent
  @indent
end

#nindObject

Returns the value of attribute nind.



22
23
24
# File 'lib/lib/haxe/format/json_printer.rb', line 22

def nind
  @nind
end

#prettyObject

Returns the value of attribute pretty.



21
22
23
# File 'lib/lib/haxe/format/json_printer.rb', line 21

def pretty
  @pretty
end

#replacerObject

Returns the value of attribute replacer.



19
20
21
# File 'lib/lib/haxe/format/json_printer.rb', line 19

def replacer
  @replacer
end

Class Method Details

._print(o, replacer = nil, space = nil) ⇒ Object



230
231
232
233
234
# File 'lib/lib/haxe/format/json_printer.rb', line 230

def JsonPrinter._print(o,replacer = nil,space = nil)
  printer = ::Haxe::Format::JsonPrinter.new(replacer,space)
  printer.write("",o)
  printer.buf.b
end

Instance Method Details

#fields_string(v, fields) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/lib/haxe/format/json_printer.rb', line 136

def fields_string(v,fields)
  @buf.b += [123].pack("U")
  len = fields.length
  last = len - 1
  first = true
  begin
    _g = 0
    while(_g < len) 
      i = _g
      _g+=1
      f = fields[i]
      value = Reflect.field(v,f)
      next if Reflect.is_function(value)
      if first 
        @nind+=1
        first = false
      else 
        @buf.b += [44].pack("U")
      end
      @buf.b += [10].pack("U") if @pretty
      if @pretty 
        v1 = nil
        begin
          c = @indent
          l = @nind * @indent.length
          if c.length == 0 || "".length >= l 
            v1 = ""
          else 
            v1 = str_pad("",((l - "".length) / c.length).ceil * c.length + "".length,c,__php__.call("STR_PAD_LEFT"))
          end
        end
        @buf.b += v1.to_s
      end
      self.quote(f)
      @buf.b += [58].pack("U")
      @buf.b += [32].pack("U") if @pretty
      self.write(f,value)
      if i == last 
        @nind-=1
        @buf.b += [10].pack("U") if @pretty
        if @pretty 
          v2 = nil
          begin
            c1 = @indent
            l1 = @nind * @indent.length
            if c1.length == 0 || "".length >= l1 
              v2 = ""
            else 
              v2 = str_pad("",((l1 - "".length) / c1.length).ceil * c1.length + "".length,c1,__php__.call("STR_PAD_LEFT"))
            end
          end
          @buf.b += v2.to_s
        end
      end
    end
  end
  @buf.b += [125].pack("U")
end

#quote(s) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/lib/haxe/format/json_printer.rb', line 195

def quote(s)
  @buf.b += [34].pack("U")
  i = 0
  while(true) 
    c = nil
    begin
      index = i
      i+=1
      c = (s[index] || 0).ord
    end
    break if c == 0
    case(c)
    when 34
      @buf.b += "\\\"".to_s
    when 92
      @buf.b += "\\\\".to_s
    when 10
      @buf.b += "\\n".to_s
    when 13
      @buf.b += "\\r".to_s
    when 9
      @buf.b += "\\t".to_s
    when 8
      @buf.b += "\\b".to_s
    when 12
      @buf.b += "\\f".to_s
    else
      @buf.b += [c].pack("U")
    end
  end
  @buf.b += [34].pack("U")
end

#write(k, v) ⇒ Object



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
# File 'lib/lib/haxe/format/json_printer.rb', line 24

def write(k,v)
  v = (@replacer).call(k,v) if @replacer != nil
  begin
    _g = Type._typeof(v)
    case(_g.index)
    when 8
      @buf.b += "\"???\"".to_s
    when 4
      self.fields_string(v,Reflect.fields(v))
    when 1
      v1 = v
      @buf.b += v1.to_s
    when 2
      v2 = nil
      if lambda{|_this_| f = v
      _r = f.to_f.finite?}.call(self) 
        v2 = v
      else 
        v2 = "null"
      end
      @buf.b += v2.to_s
    when 5
      @buf.b += "\"<fun>\"".to_s
    when 6
      c = _g.params[0]
      if c == String 
        self.quote(v)
      elsif c == Array 
        v3 = v
        @buf.b += [91].pack("U")
        len = v3.length
        last = len - 1
        begin
          _g1 = 0
          while(_g1 < len) 
            i = _g1
            _g1+=1
            if i > 0 
              @buf.b += [44].pack("U")
            else 
              @nind+=1
            end
            @buf.b += [10].pack("U") if @pretty
            if @pretty 
              v4 = nil
              begin
                c1 = @indent
                l = @nind * @indent.length
                if c1.length == 0 || "".length >= l 
                  v4 = ""
                else 
                  v4 = str_pad("",((l - "".length) / c1.length).ceil * c1.length + "".length,c1,__php__.call("STR_PAD_LEFT"))
                end
              end
              @buf.b += v4.to_s
            end
            self.write(i,v3[i])
            if i == last 
              @nind-=1
              @buf.b += [10].pack("U") if @pretty
              if @pretty 
                v5 = nil
                begin
                  c2 = @indent
                  l1 = @nind * @indent.length
                  if c2.length == 0 || "".length >= l1 
                    v5 = ""
                  else 
                    v5 = str_pad("",((l1 - "".length) / c2.length).ceil * c2.length + "".length,c2,__php__.call("STR_PAD_LEFT"))
                  end
                end
                @buf.b += v5.to_s
              end
            end
          end
        end
        @buf.b += [93].pack("U")
      elsif c == ::Haxe::Ds::StringMap 
        v6 = v
        o = { }
        _it2 = ::Rb::RubyIterator.new(v6.keys)
        while(_it2.has_next) do
          k1 = _it2._next
          value = v6[k1]
          o[k1] = value
        end
        self.fields_string(o,Reflect.fields(o))
      elsif c == Date 
        v7 = v
        self.quote(HxOverrides.date_str(v7))
      else 
        self.fields_string(v,Reflect.fields(v))
      end
    when 7
      i1 = nil
      begin
        e = v
        i1 = e.index
      end
      begin
        v8 = i1
        @buf.b += v8.to_s
      end
    when 3
      v9 = v
      @buf.b += v9.to_s
    when 0
      @buf.b += "null".to_s
    end
  end
end