### Home Classes Methods

### In Files

- lib/yajl.rb

### Parent

Object

### Methods

- ::encode

### Class/Module Index

Quicksearch

- Yajl
- Yajl::Bzip2
- Yajl::Bzip2::StreamReader
- Yajl::Bzip2::StreamWriter
- Yajl::Deflate
- Yajl::Deflate::StreamReader
- Yajl::Deflate::StreamWriter
- Yajl::Encoder
- Yajl::Gzip
- Yajl::Gzip::StreamReader
- Yajl::Gzip::StreamWriter
- Yajl::HttpStream
- Yajl::HttpStream::HttpError
- Yajl::HttpStream::InvalidContentType
- Yajl::Parser
- JSON
- JSON::GeneratorError
- JSON::JSONError
- JSON::ParserError
- Kernel
- Object

No matching classes.

# Yajl::Encoder

### Public Class Methods

encode(obj, *args, &block) click to toggle source

A helper method for encode-and-forget use-cases

Examples:

    Yajl::Encoder.encode(obj[, io, :pretty => true, :indent => "\t", &block])

    output = Yajl::Encoder.encode(obj[, :pretty => true, :indent => "\t", &block])

`obj` is a ruby object to encode to JSON format

`io` is the optional IO stream to encode the ruby object to. If `io` isn't passed, the resulting JSON string is returned. If `io` is passed, nil is returned.

The `options` hash allows you to set two encoding options - :pretty and :indent

:pretty accepts a boolean and will enable/disable "pretty printing" the resulting output

:indent accepts a string and will be used as the indent character(s) during the pretty print process

If a block is passed, it will be used as (and work the same as) the `on_progress` callback

    # File lib/yajl.rb, line 61
    def self.encode(obj, *args, &block)
      # TODO: this code smells, any ideas?
      args.flatten!
      options = {}
      io = nil
      args.each do |arg|
        if arg.is_a?(Hash)
          options = arg
        elsif arg.respond_to?(:write)
          io = arg
        end
      end if args.any?
      new(options).encode(obj, io, &block)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.