~ubuntu-branches/ubuntu/intrepid/ruby1.9/intrepid-updates

« back to all changes in this revision

Viewing changes to lib/json/pure/generator.rb

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-09-04 16:01:17 UTC
  • mfrom: (1.1.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20070904160117-i15zckg2nhxe9fyw
Tags: 1.9.0+20070830-2ubuntu1
* Sync from Debian; remaining changes:
  - Add -g to CFLAGS.
* Fixes build failure on ia64.
* Fixes build failure with gcc-4.2 on lpia.
* Robustify check for target_os, fixing build failure on lpia.
* Set Ubuntu maintainer address.

Show diffs side-by-side

added added

removed removed

Lines of Context:
89
89
        # * *object_nl*: a string that is put at the end of a JSON object (default: ''), 
90
90
        # * *array_nl*: a string that is put at the end of a JSON array (default: ''),
91
91
        # * *check_circular*: true if checking for circular data structures
 
92
        #   should be done (the default), false otherwise.
 
93
        # * *check_circular*: true if checking for circular data structures
92
94
        #   should be done, false (the default) otherwise.
 
95
        # * *allow_nan*: true if NaN, Infinity, and -Infinity should be
 
96
        #   generated, otherwise an exception is thrown, if these values are
 
97
        #   encountered. This options defaults to false.
93
98
        def initialize(opts = {})
94
 
          @indent         = opts[:indent]       || ''
95
 
          @space          = opts[:space]        || ''
96
 
          @space_before   = opts[:space_before] || ''
97
 
          @object_nl      = opts[:object_nl]    || ''
98
 
          @array_nl       = opts[:array_nl]     || ''
99
 
          @check_circular = !!(opts[:check_circular] || false)
100
 
          @seen           = {}
 
99
          @seen = {}
 
100
          @indent         = ''
 
101
          @space          = ''
 
102
          @space_before   = ''
 
103
          @object_nl      = ''
 
104
          @array_nl       = ''
 
105
          @check_circular = true
 
106
          @allow_nan      = false
 
107
          configure opts
101
108
        end
102
109
 
103
110
        # This string is used to indent levels in the JSON text.
117
124
        # This string is put at the end of a line that holds a JSON array.
118
125
        attr_accessor :array_nl
119
126
 
 
127
        # This integer returns the maximum level of data structure nesting in
 
128
        # the generated JSON, max_nesting = 0 if no maximum is checked.
 
129
        attr_accessor :max_nesting
 
130
 
 
131
        def check_max_nesting(depth) # :nodoc:
 
132
          return if @max_nesting.zero?
 
133
          current_nesting = depth + 1
 
134
          current_nesting > @max_nesting and
 
135
            raise NestingError, "nesting of #{current_nesting} is too deep"
 
136
        end
 
137
 
120
138
        # Returns true, if circular data structures should be checked,
121
139
        # otherwise returns false.
122
140
        def check_circular?
123
141
          @check_circular
124
142
        end
125
143
 
 
144
        # Returns true if NaN, Infinity, and -Infinity should be considered as
 
145
        # valid JSON and output.
 
146
        def allow_nan?
 
147
          @allow_nan
 
148
        end
 
149
 
126
150
        # Returns _true_, if _object_ was already seen during this generating
127
151
        # run. 
128
152
        def seen?(object)
139
163
        def forget(object)
140
164
          @seen.delete object.__id__
141
165
        end
 
166
 
 
167
        # Configure this State instance with the Hash _opts_, and return
 
168
        # itself.
 
169
        def configure(opts)
 
170
          @indent         = opts[:indent] if opts.key?(:indent)
 
171
          @space          = opts[:space] if opts.key?(:space)
 
172
          @space_before   = opts[:space_before] if opts.key?(:space_before)
 
173
          @object_nl      = opts[:object_nl] if opts.key?(:object_nl)
 
174
          @array_nl       = opts[:array_nl] if opts.key?(:array_nl)
 
175
          @check_circular = !!opts[:check_circular] if opts.key?(:check_circular)
 
176
          @allow_nan      = !!opts[:allow_nan] if opts.key?(:allow_nan)
 
177
          if !opts.key?(:max_nesting) # defaults to 19
 
178
            @max_nesting = 19
 
179
          elsif opts[:max_nesting]
 
180
            @max_nesting = opts[:max_nesting]
 
181
          else
 
182
            @max_nesting = 0
 
183
          end
 
184
          self
 
185
        end
 
186
 
 
187
        # Returns the configuration instance variables as a hash, that can be
 
188
        # passed to the configure method.
 
189
        def to_h
 
190
          result = {}
 
191
          for iv in %w[indent space space_before object_nl array_nl check_circular allow_nan max_nesting]
 
192
            result[iv.intern] = instance_variable_get("@#{iv}")
 
193
          end
 
194
          result
 
195
        end
142
196
      end
143
197
 
144
198
      module GeneratorMethods
158
212
          def to_json(state = nil, depth = 0, *)
159
213
            if state
160
214
              state = JSON.state.from_state(state)
 
215
              state.check_max_nesting(depth)
161
216
              json_check_circular(state) { json_transform(state, depth) }
162
217
            else
163
218
              json_transform(state, depth)
167
222
          private
168
223
 
169
224
          def json_check_circular(state)
170
 
            if state
 
225
            if state and state.check_circular?
171
226
              state.seen?(self) and raise JSON::CircularDatastructure,
172
227
                  "circular data structures not supported!"
173
228
              state.remember self
211
266
          def to_json(state = nil, depth = 0, *)
212
267
            if state
213
268
              state = JSON.state.from_state(state)
 
269
              state.check_max_nesting(depth)
214
270
              json_check_circular(state) { json_transform(state, depth) }
215
271
            else
216
272
              json_transform(state, depth)
220
276
          private
221
277
 
222
278
          def json_check_circular(state)
223
 
            if state
 
279
            if state and state.check_circular?
224
280
              state.seen?(self) and raise JSON::CircularDatastructure,
225
281
                "circular data structures not supported!"
226
282
              state.remember self
257
313
 
258
314
        module Float
259
315
          # Returns a JSON string representation for this Float number.
260
 
          def to_json(*) to_s end
 
316
          def to_json(state = nil, *)
 
317
            case
 
318
            when infinite?
 
319
              if !state || state.allow_nan?
 
320
                to_s
 
321
              else
 
322
                raise GeneratorError, "#{self} not allowed in JSON"
 
323
              end
 
324
            when nan?
 
325
              if !state || state.allow_nan?
 
326
                to_s
 
327
              else
 
328
                raise GeneratorError, "#{self} not allowed in JSON"
 
329
              end
 
330
            else
 
331
              to_s
 
332
            end
 
333
          end
261
334
        end
262
335
 
263
336
        module String