### Home Classes Methods

### In Files

- lib/rack/utils/okjson.rb

### Namespace

- CLASS Rack::Utils::OkJson::Error
- CLASS Rack::Utils::OkJson::Utf8Error

### Methods

- #decode
- #encode
- #valenc

### Files

- KNOWN-ISSUES

### Class/Module Index

Quicksearch

- Rack
- Rack::Auth
- Rack::Auth::AbstractHandler
- Rack::Auth::AbstractRequest
- Rack::Auth::Basic
- Rack::Auth::Basic::Request
- Rack::Auth::Digest
- Rack::Auth::Digest::MD5
- Rack::Auth::Digest::Nonce
- Rack::Auth::Digest::Params
- Rack::Auth::Digest::Request
- Rack::BodyProxy
- Rack::Builder
- Rack::Cascade
- Rack::Chunked
- Rack::Chunked::Body
- Rack::CommonLogger
- Rack::ConditionalGet
- Rack::Config
- Rack::ContentLength
- Rack::ContentType
- Rack::Deflater
- Rack::Deflater::DeflateStream
- Rack::Deflater::GzipStream
- Rack::Directory
- Rack::ETag
- Rack::File
- Rack::ForwardRequest
- Rack::Handler
- Rack::Handler::CGI
- Rack::Handler::EventedMongrel
- Rack::Handler::FastCGI
- Rack::Handler::LSWS
- Rack::Handler::Mongrel
- Rack::Handler::SCGI
- Rack::Handler::SwiftipliedMongrel
- Rack::Handler::Thin
- Rack::Handler::WEBrick
- Rack::Head
- Rack::Lint
- Rack::Lobster
- Rack::Lock
- Rack::Logger
- Rack::MethodOverride
- Rack::Mime
- Rack::MockRequest
- Rack::MockRequest::FatalWarner
- Rack::MockRequest::FatalWarning
- Rack::MockResponse
- Rack::Multipart
- Rack::Multipart::Generator
- Rack::Multipart::MultipartPartLimitError
- Rack::Multipart::Parser
- Rack::Multipart::UploadedFile
- Rack::NullLogger
- Rack::Recursive
- Rack::Reloader
- Rack::Reloader::Stat
- Rack::Request
- Rack::Response
- Rack::Response::Helpers
- Rack::RewindableInput
- Rack::RewindableInput::Tempfile
- Rack::Runtime
- Rack::Sendfile
- Rack::Server
- Rack::Server::Options
- Rack::Session
- Rack::Session::Abstract
- Rack::Session::Abstract::ID
- Rack::Session::Abstract::SessionHash
- Rack::Session::Cookie
- Rack::Session::Cookie::Base64
- Rack::Session::Cookie::Base64::JSON
- Rack::Session::Cookie::Base64::Marshal
- Rack::Session::Cookie::Base64::ZipJSON
- Rack::Session::Cookie::Identity
- Rack::Session::Memcache
- Rack::Session::Pool
- Rack::ShowExceptions
- Rack::ShowStatus
- Rack::Static
- Rack::TempfileReaper
- Rack::URLMap
- Rack::Utils
- Rack::Utils::Context
- Rack::Utils::HeaderHash
- Rack::Utils::InvalidParameterError
- Rack::Utils::KeySpaceConstrainedParams
- Rack::Utils::Multipart
- Rack::Utils::OkJson
- Rack::Utils::OkJson::Error
- Rack::Utils::OkJson::Utf8Error
- Rack::Utils::ParameterTypeError
- FCGI
- FCGI::Stream
- WEBrick
- WEBrick::HTTPResponse
- Object

No matching classes.

# Rack::Utils::OkJson

Some parts adapted from golang.org/src/encoding/json/decode.go and golang.org/src/unicode/utf8/utf8.go

### Constants

Spc
Uchar1max
Uchar2max
Uchar3max
Ucharerr
Umask2
Umask3
Umask4
Umaskx
Unesc
Upstream
Ustrerr
Usurr1
Usurr2
Usurr3
Usurrself
Utag2
Utag3
Utag4
Utag5
Utagx

### Public Instance Methods

decode(s) click to toggle source

Decodes a json document in string s and returns the corresponding ruby value. String s must be valid UTF-8. If you have a string in some other encoding, convert it first.

String values in the resulting structure will be UTF-8.

    # File lib/rack/utils/okjson.rb, line 42
    def decode(s)
      ts = lex(s)
      v, ts = textparse(ts)
      if ts.length > 0
        raise Error, 'trailing garbage'
      end
      v
    end

encode(x) click to toggle source

Encodes x into a json text. It may contain only Array, Hash, String, Numeric, true, false, nil. (Note, this list excludes Symbol.) X itself must be an Array or a Hash. No other value can be encoded, and an error will be raised if x contains any other value, such as Nan, Infinity, Symbol, and Proc, or if a Hash key is not a String. Strings contained in x must be valid UTF-8.

    # File lib/rack/utils/okjson.rb, line 61
    def encode(x)
      case x
      when Hash    then objenc(x)
      when Array   then arrenc(x)
      else
        raise Error, 'root value must be an Array or a Hash'
      end
    end

valenc(x) click to toggle source

    # File lib/rack/utils/okjson.rb, line 71
    def valenc(x)
      case x
      when Hash    then objenc(x)
      when Array   then arrenc(x)
      when String  then strenc(x)
      when Numeric then numenc(x)
      when true    then "true"
      when false   then "false"
      when nil     then "null"
      else
        raise Error, "cannot encode #{x.class}: #{x.inspect}"
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.