Class: Rackful::Serializer::JSON

Inherits:
Rackful::Serializer show all
Defined in:
lib/rackful/serializer.rb

Constant Summary

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

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

This class inherits a constructor from Rackful::Serializer

Instance Attribute Details

- (String) content_type (readonly) Originally defined in class Rackful::Serializer

Returns The content type to be served by this Serializer. This will always be one of the content types listed in constant CONTENT_TYPES.

Returns:

  • (String)

    The content type to be served by this Serializer. This will always be one of the content types listed in constant CONTENT_TYPES.

- (Request) request (readonly) Originally defined in class Rackful::Serializer

Returns:

- (Resource) resource (readonly) Originally defined in class Rackful::Serializer

Returns:

Instance Method Details

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

Yields:

  • (json)

Yield Parameters:

  • json (String)


282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/rackful/serializer.rb', line 282

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