Class: Rackful::Serializer::JSON
- Inherits:
-
Rackful::Serializer
- Object
- Rackful::Serializer
- Rackful::Serializer::JSON
- Defined in:
- lib/rackful/serializer.rb
Constant Summary
- CONTENT_TYPES =
[ 'application/json', 'application/x-json' ]
Instance Attribute Summary (collapse)
-
- (String) content_type
inherited
from Rackful::Serializer
readonly
The content type to be served by this Serializer.
- - (Request) request inherited from Rackful::Serializer readonly
- - (Resource) resource inherited from Rackful::Serializer readonly
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
.
- (Request) request (readonly) Originally defined in class Rackful::Serializer
- (Resource) resource (readonly) Originally defined in class Rackful::Serializer
Instance Method Details
- (void) each(thing = self.resource.to_rackful) {|json| ... }
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 |