- README

- Top Level Namespace
- Adler32
- ArgumentError
- Array
- Atomic
- Base64
  - Error
- Benchmark
  - BM
    - Job
    - Tms
  - IPS
    - Entry
    - Job
- BigFloat
- BigInt
- BigRational
- BitArray
- Bool
- Box
- Bytes
- Channel
  - Buffered
  - ClosedError
  - ReceiveAction
  - SelectAction
  - SendAction
  - Unbuffered
- Char
  - Reader
- Class
- Colorize
  - Object
  - ObjectExtensions
- Comparable
- Complex
- Concurrent
  - CanceledError
- CRC32
- Crypto
  - Bcrypt
    - Error
    - Password
  - Blowfish
  - Subtle
- CSV
  - Builder
    - Row
  - Error
  - Lexer
  - MalformedCSVError
  - Parser
  - Row
  - Token
    - Kind
- Debug
  - DWARF
    - Abbrev
      - Attribute
    - AT
    - FORM
    - Info
      - Value
    - LineNumbers
      - Register
      - Row
      - Sequence
    - LNE
    - LNS
    - Strings
    - TAG
  - ELF
    - Endianness
    - Error
    - Ident
    - Klass
    - Machine
    - OSABI
    - SectionHeader
      - Flags
      - Type
    - Type
- Deque
- Digest
  - Base
  - MD5
  - SHA1
- Dir
- DivisionByZero
- DL
- ECR
- Enum
- Enumerable
  - EmptyError
- ENV
- Errno
- Exception
- Fiber
- File
  - Stat
- FileUtils
- Flate
  - Error
  - Reader
  - Strategy
  - Writer
- Float
  - Primitive
- Float32
- Float64
- GC
  - Stats
- Gzip
  - Error
  - Header
  - Reader
  - Writer
- Hash
- HTML
- HTTP
  - Client
    - BodyType
    - Response
  - CompressHandler
  - ComputedContentTypeHeader
  - Cookie
  - Cookies
  - ErrorHandler
  - FormData
    - Builder
    - Error
    - FileMetadata
    - Parser
    - Part
  - Handler
    - Proc
  - Headers
  - LogHandler
  - Multipart
    - Builder
    - Error
    - Parser
  - Params
    - Builder
  - Request
  - Server
    - Context
    - RequestProcessor
    - Response
  - StaticFileHandler
    - DirectoryListing
  - WebSocket
  - WebSocketHandler
- Indexable
- IndexError
- INI
- Int
  - Primitive
  - Signed
  - Unsigned
- Int16
- Int32
- Int64
- Int8
- InvalidByteSequenceError
- IO
  - Buffered
  - ByteFormat
    - BigEndian
    - LittleEndian
    - NetworkEndian
    - SystemEndian
  - Delimited
  - EncodingOptions
  - EOFError
  - Error
  - FileDescriptor
  - Hexdump
  - Memory
  - MultiWriter
  - Seek
  - Sized
  - Timeout
- IPSocket
- Iterable
- Iterator
  - IteratorWrapper
  - Stop
- JSON
  - Any
  - Builder
    - ArrayState
    - DocumentEndState
    - DocumentStartState
    - ObjectState
    - StartState
    - State
  - Error
  - Lexer
  - ParseException
  - Parser
  - PullParser
  - Token
  - Type
- KeyError
- Levenshtein
  - Finder
- LLVM
  - ABI
    - AArch64
    - ArgKind
    - ArgType
    - ARM
    - FunctionType
    - X86
    - X86_64
      - RegClass
  - AtomicOrdering
  - AtomicRMWBinOp
  - Attribute
  - AttributeIndex
  - BasicBlock
  - BasicBlockCollection
  - Builder
  - CallConvention
  - CodeGenFileType
  - CodeGenOptLevel
  - CodeModel
  - Context
  - DIBuilder
  - DIFlags
  - DwarfTag
  - DwarfTypeEncoding
  - Function
  - FunctionCollection
  - FunctionPassManager
    - Runner
  - GenericValue
  - GlobalCollection
  - InstructionCollection
  - IntPredicate
  - JITCompiler
  - Linkage
  - MemoryBuffer
  - Module
  - ModuleFlag
  - ModulePassManager
  - ParameterCollection
  - PassManagerBuilder
  - PassRegistry
  - PhiTable
  - RealPredicate
  - RelocMode
  - Target
  - TargetData
  - TargetMachine
  - Type
    - Kind
  - Value
    - Kind
  - ValueMethods
  - VerifierFailureAction
- Logger
  - Formatter
  - Severity
- Markdown
  - HTMLRenderer
  - Parser
    - PrefixHeader
    - UnorderedList
  - Renderer
- Math
- Mutex
- NamedTuple
- Nil
- Number
  - Primitive
- OAuth
  - AccessToken
  - Consumer
  - Error
  - RequestToken
- OAuth2
  - AccessToken
    - Bearer
    - Mac
  - Client
  - Error
  - Session
- Object
- OpenSSL
  - Digest
    - Error
    - UnsupportedError
  - DigestBase
  - Error
  - HMAC
  - MD5
  - SHA1
  - SSL
    - Context
      - Client
      - Server
    - Error
    - ErrorType
    - Modes
    - Options
    - Socket
      - Client
      - Server
    - VerifyMode
    - X509VerifyFlags
- OptionParser
  - Exception
  - InvalidOption
  - MissingOption
- PartialComparable
- Pointer
  - Appender
- PrettyPrint
- Proc
- Process
  - Env
  - Status
  - Stdio
  - Tms
- Random
  - MT19937
- Range
- Readline
  - CompletionProc
  - KeyBindingProc
- Reference
- Reflect
- Regex
  - MatchData
  - Options
- SecureRandom
- Set
- Signal
- Slice
- Socket
  - Address
  - Addrinfo
  - Error
  - Family
  - IPAddress
  - Protocol
  - Server
  - Type
  - UNIXAddress
- Spec
  - Expectations
  - Methods
  - ObjectExtensions
- StaticArray
- String
  - Builder
  - RawConverter
- StringPool
- StringScanner
- Struct
- Symbol
- System
- TCPServer
- TCPSocket
- Tempfile
- Termios
  - AttributeSelection
  - BaudRate
  - ControlMode
  - InputMode
  - LineControl
  - LocalMode
  - OutputMode
- Time
  - DayOfWeek
  - EpochConverter
  - EpochMillisConverter
  - Format
    - Error
  - Kind
  - MonthSpan
  - Span
- Tuple
- TypeCastError
- UDPSocket
- UInt16
- UInt32
- UInt64
- UInt8
- Unicode
  - CaseOptions
- Union
- UNIXServer
- UNIXSocket
- URI
  - Error
- Value
- WeakRef
- XML
  - Attributes
  - AttributeType
  - Builder
  - Error
  - HTMLParserOptions
  - Namespace
  - Node
  - NodeSet
  - ParserOptions
  - Reader
  - SaveOptions
  - Type
  - XPathContext
- YAML
  - Any
  - Builder
  - Error
  - EventKind
  - ParseException
  - Parser
  - PullParser
  - Type
- Zip
  - CompressionMethod
  - Error
  - File
    - Entry
  - FileInfo
  - Reader
    - Entry
  - Writer
    - Entry
- Zlib
  - Error
  - Reader
  - Writer

# module JSON

## Overview

The JSON module allows parsing and generating JSON documents.

### Parsing and generating with `JSON#mapping`

Use `JSON#mapping` to define how an object is mapped to JSON, making it the recommended easy, type-safe and efficient option for parsing and generating JSON. Refer to that module's documentation to learn about it.

### Parsing with `JSON#parse`

`JSON#parse` will return an `Any`, which is a convenient wrapper around all possible JSON types, making it easy to traverse a complex JSON structure but requires some casts from time to time, mostly via some method invocations.

    require "json"

    value = JSON.parse("[1, 2, 3]") # : JSON::Any

    value[0]              # => 1
    typeof(value[0])      # => JSON::Any
    value[0].as_i         # => 1
    typeof(value[0].as_i) # => Int32

    value[0] + 1       # Error, because value[0] is JSON::Any
    value[0].as_i + 10 # => 11

The above is useful for dealing with a dynamic JSON structure but is slower than using `JSON#mapping`.

### Generating with `JSON.build`

Use `JSON.build`, which uses `JSON::Builder`, to generate JSON by emitting scalars, arrays and objects:

    require "json"

    string = JSON.build do |json|
      json.object do
        json.field "name", "foo"
        json.field "values" do
          json.array do
            json.number 1
            json.number 2
            json.number 3
          end
        end
      end
    end
    string # => %<{"name":"foo","values":[1,2,3]}>

### Generating with `to_json`

`to_json`, `to_json(IO)` and `to_json(JSON::Builder)` methods are provided for primitive types, but you need to define `to_json(JSON::Builder)` for custom objects, either manually or using `JSON#mapping`.

## Defined in:

## Class Method Summary

- .build(io : IO, indent = nil, &block)

  Writes JSON into the given `IO`.

- .build(indent = nil, &block)

  Returns the resulting `String` of writing JSON to the yielded `JSON::Builder`.

- .parse(input : String | IO) : Any

  Parses a JSON document as a `JSON::Any`.

- .parse_raw(input : String | IO) : Type

  Parses a JSON document as a `JSON::Type`.

## Macro Summary

- mapping(properties, strict = false)

  The `JSON.mapping` macro defines how an object is mapped to JSON.

- mapping

  This is a convenience method to allow invoking `JSON.mapping` with named arguments instead of with a hash/named-tuple literal.

## Class Method Detail

def self.build(io : IO, indent = nil, &block) #

Writes JSON into the given `IO`. A `JSON::Builder` is yielded to the block.

def self.build(indent = nil, &block) #

Returns the resulting `String` of writing JSON to the yielded `JSON::Builder`.

    require "json"

    string = JSON.build do |json|
      json.object do
        json.field "name", "foo"
        json.field "values" do
          json.array do
            json.number 1
            json.number 2
            json.number 3
          end
        end
      end
    end
    string # => %<{"name":"foo","values":[1,2,3]}>

def self.parse(input : String | IO) : Any #

Parses a JSON document as a `JSON::Any`.

def self.parse_raw(input : String | IO) : Type #

Parses a JSON document as a `JSON::Type`.

## Macro Detail

macro mapping(properties, strict = false) #

The `JSON.mapping` macro defines how an object is mapped to JSON.

### Example

    require "json"

    class Location
      JSON.mapping(
        lat: Float64,
        lng: Float64,
      )
    end

    class House
      JSON.mapping(
        address: String,
        location: {type: Location, nilable: true},
      )
    end

    house = House.from_json(%({"address": "Crystal Road 1234", "location": {"lat": 12.3, "lng": 34.5}}))
    house.address  # => "Crystal Road 1234"
    house.location # => #<Location:0x10cd93d80 @lat=12.3, @lng=34.5>
    house.to_json  # => %({"address":"Crystal Road 1234","location":{"lat":12.3,"lng":34.5}})

### Usage

`JSON.mapping` must receive a series of named arguments, or a named tuple literal, or a hash literal, whose keys will define Crystal properties.

The value of each key can be a single type (not a union type). Primitive types (numbers, string, boolean and nil) are supported, as well as custom objects which use `JSON.mapping` or define a `new` method that accepts a `JSON::PullParser` and returns an object from it.

The value can also be another hash literal with the following options:

- type: (required) the single type described above (you can use `JSON::Any` too)
- key: the property name in the JSON document (as opposed to the property name in the Crystal code)
- nilable: if `true`, the property can be `Nil`. Passing `T?` as a type has the same effect.
- default: value to use if the property is missing in the JSON document, or if it's `null` and `nilable` was not set to `true`. If the default value creates a new instance of an object (for example `[1, 2, 3]` or `SomeObject.new`), a different instance will be used each time a JSON document is parsed.
- emit_null: if `true`, emits a `null` value for nilable properties (by default nulls are not emitted)
- converter: specify an alternate type for parsing and generation. The converter must define `from_json(JSON::PullParser)` and `to_json(value, JSON::Builder)` as class methods. Examples of converters are `Time::Format` and `Time::EpochConverter` for `Time`.
- root: assume the value is inside a JSON object with a given key (see `Object.from_json(string_or_io, root)`)
- setter: if `true`, will generate a setter for the variable, `true` by default
- getter: if `true`, will generate a getter for the variable, `true` by default

This macro by default defines getters and setters for each variable (this can be overrided with setter and getter). The mapping doesn't define a constructor accepting these variables as arguments, but you can provide an overload.

The macro basically defines a constructor accepting a `JSON::PullParser` that reads from it and initializes this type's instance variables. It also defines a `to_json(JSON::Builder)` method by invoking `to_json(JSON::Builder)` on each of the properties (unless a converter is specified, in which case `to_json(value, JSON::Builder)` is invoked).

This macro also declares instance variables of the types given in the mapping.

If strict is `true`, unknown properties in the JSON document will raise a parse exception. The default is `false`, so unknown properties are silently ignored.

macro mapping #

This is a convenience method to allow invoking `JSON.mapping` with named arguments instead of with a hash/named-tuple literal.