Class: JSONRPC::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonrpc/parser.rb

Overview

JSON-RPC 2.0 Parser for converting raw JSON into JSONRPC objects

The Parser handles converting raw JSON strings into appropriate JSONRPC objects based on the JSON-RPC 2.0 protocol specification.

Examples:

Parse a request

parser = JSONRPC::Parser.new
request = parser.parse('{"jsonrpc":"2.0","method":"subtract","params":[42,23],"id":1}')

JSONRPC::Parse a batch request

parser = JSONRPC::Parser.new
batch = parser.parse('[{"jsonrpc":"2.0","method":"sum","params":[1,2],"id":"1"},
                      {"jsonrpc":"2.0","method":"notify_hello","params":[7]}]')

Instance Method Summary collapse

Instance Method Details

#parse(json) ⇒ Request, ...

Parse a JSON-RPC 2.0 message

Examples:

Parse a single request

parser.parse('{"jsonrpc":"2.0","method":"subtract","params":[42,23],"id":1}')

Parse a batch request

parser.parse('[{"jsonrpc":"2.0","method":"sum","params":[1,2],"id":"1"}]')

Parameters:

  • json (String)

    the JSON-RPC 2.0 message

Returns:

Raises:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/jsonrpc/parser.rb', line 37

def parse(json)
  data = MultiJson.load(json)

  if data.is_a?(Array)
    parse_batch(data)
  else
    parse_single(data)
  end
rescue MultiJson::ParseError => e
  raise ParseError.new(
    data: {
      details: e.message,
      adapter: MultiJson.adapter.name,
      input_preview: json[0..100]
    }
  )
end