Class: String

Inherits:
Object show all
Defined in:
lib/facets/json.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.json_create(o) ⇒ Object

Raw Strings are JSON Objects (the raw bytes are stored in an array for the key “raw”). The Ruby String can be created by this class method.



621
622
623
# File 'lib/facets/json.rb', line 621

def self.json_create(o)
  o['raw'].pack('C*')
end

Instance Method Details

#to_jsonObject

This string should be encoded with UTF-8 (if JSON unicode support is enabled). A call to this method returns a JSON string encoded with UTF16 big endian characters as u????. If JSON.support_unicode? is false only control characters are encoded this way, all 8-bit bytes are just passed through.



615
616
617
# File 'lib/facets/json.rb', line 615

def to_json(*)
  '"' << JSON::utf8_to_json(self) << '"'
end

#to_json_raw(*args) ⇒ Object

This method should be used, if you want to convert raw strings to JSON instead of UTF-8 strings, e. g. binary data (and JSON Unicode support is enabled).



637
638
639
# File 'lib/facets/json.rb', line 637

def to_json_raw(*args)
  to_json_raw_object.to_json(*args)
end

#to_json_raw_objectObject

This method creates a raw object, that can be nested into other data structures and will be unparsed as a raw string.



627
628
629
630
631
632
# File 'lib/facets/json.rb', line 627

def to_json_raw_object
  {
    'json_class'  => self.class.name,
    'raw'         => self.unpack('C*'),
  }
end