class CodeRay::Encoders::JSON

]
{“type”=>“block”, “action”=>“close”, “kind”=>“string”},
{“type”=>“text”, “text”=>“"”, “kind”=>“delimiter”},
{“type”=>“text”, “text”=>“Hello world!”, “kind”=>“content”},
{“type”=>“text”, “text”=>“"”, “kind”=>“delimiter”},
{“type”=>“block”, “action”=>“open”, “kind”=>“string”},
{“type”=>“text”, “text”=>“ ”, “kind”=>“space”},
{“type”=>“text”, “text”=>“puts”, “kind”=>“ident”},
[
yields
CodeRay.scan(‘puts “Hello world!”’, :ruby).json
Example:
A simple JSON Encoder.

def append data

def append data
  if @first
    @first = false
  else
    @out << ','
  end
  
  @out << data.to_json
end

def begin_group kind

def begin_group kind
  append :type => 'block', :action => 'open', :kind => kind
end

def begin_line kind

def begin_line kind
  append :type => 'block', :action => 'begin_line', :kind => kind
end

def end_group kind

def end_group kind
  append :type => 'block', :action => 'close', :kind => kind
end

def end_line kind

def end_line kind
  append :type => 'block', :action => 'end_line', :kind => kind
end

def finish options

def finish options
  @out << ']'
end

def setup options

def setup options
  super
  
  @first = true
  @out << '['
end

def text_token text, kind

def text_token text, kind
  append :type => 'text', :text => text, :kind => kind
end