Class: Rackful::JSON

Inherits:
Serializer show all
Defined in:
gems/rackful-0.1.1/lib/rackful_serializer.rb,
gems/rackful-0.1.1.orig/lib/rackful_serializer.rb

Direct Known Subclasses

EPIC::Handle::JSON

Constant Summary

CONTENT_TYPES =
[
  'application/json',
  'application/x-json'
]

Instance Attribute Summary

Attributes inherited from Serializer

#content_type, #resource

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from Serializer

#initialize

Constructor Details

This class inherits a constructor from Rackful::Serializer

Class Method Details

+ (void) parse(input)



288
289
290
291
292
293
294
# File 'gems/rackful-0.1.1/lib/rackful_serializer.rb', line 288

def self.parse input
  r = ::JSON.parse(
    input.read,
    :symbolize_names => true
  )
  self.recursive_datetime_parser r
end

+ (void) recursive_datetime_parser(p)



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'gems/rackful-0.1.1/lib/rackful_serializer.rb', line 296

def self.recursive_datetime_parser p
  if p.kind_of?(String)
    begin
      return Time.xmlschema(p)
    rescue
    end
  elsif p.kind_of?(Hash)
    p.keys.each do
      |key|
      p[key] = self.recursive_datetime_parser( p[key] )
    end
  elsif p.kind_of?(Array)
    (0 ... p.size).each do
      |i|
      p[i] = self.recursive_datetime_parser( p[i] )
    end
  end
  p
end

Instance Method Details

- (void) each(thing = self.resource.to_rackful) {|json| ... }

Yields:

  • (json)

Yield Parameters:



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'gems/rackful-0.1.1/lib/rackful_serializer.rb', line 257

def each thing = self.resource.to_rackful, &block
  if thing.kind_of?( Resource ) && ! thing.equal?( self.resource )
    thing.serializer( self.content_type ).each &block
  elsif thing.respond_to? :each_pair
    first = true
    thing.each_pair do
      |k, v|
      yield( ( first ? "{\n" : ",\n" ) + k.to_s.to_json + ":" )
      first = false
      self.each v, &block
    end
    yield( first ? "{}" : "\n}" )
  elsif thing.respond_to? :each
    first = true
    thing.each do
      |v|
      yield( first ? "[\n" : ",\n" )
      first = false
      self.each v, &block
    end
    yield( first ? "[]" : "\n]" )
  elsif thing.kind_of?( String ) && thing.encoding == Encoding::BINARY
    yield Base64.encode64(thing).chomp.to_json
  elsif thing.kind_of?( Time )
    yield thing.utc.xmlschema.to_json
  else
    yield thing.to_json
  end
end