Class: RustyJson::Parser

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

Overview

Parser is the base class that actually parses JSON into Rust structs

Example: “‘ruby name = ’Test’ json = ‘“world”’ parser = Parser.new(name, json) parser.parse “‘

Instance Method Summary collapse

Constructor Details

#initialize(name, json) ⇒ Parser

Returns a new instance of Parser.

Parameters:

  • name (String)

    the name of the returned root JSON struct

  • json (String)

    the JSON string to parse into a Rust struct



18
19
20
21
# File 'lib/rusty_json/parser.rb', line 18

def initialize(name, json)
  @name = name
  @json = json
end

Instance Method Details

#parseObject

parse takes the given JSON string and turns it into a string of Rust structs, suitable for use with rustc_serialize.



25
26
27
28
# File 'lib/rusty_json/parser.rb', line 25

def parse
  (type, subtype) = parse_object([@name], JSON.parse(@json))
  (subtype || type).to_s.gsub(/\n\n$/, "\n")
end