Module: JSON::ModuleMethods
Instance Method Summary collapse
-
#parse(source) ⇒ Object
Parse the JSON string source into a Ruby data structure and return it.
-
#pretty_unparse(obj) ⇒ Object
Unparse the Ruby data structure obj into a JSON string and return it.
-
#support_unicode=(enable) ⇒ Object
Switches on Unicode support, if enable is true.
-
#support_unicode? ⇒ Boolean
Returns true if JSON supports unicode, otherwise false is returned.
-
#unparse(obj, state = nil) ⇒ Object
Unparse the Ruby data structure obj into a single line JSON string and return it.
-
#utf16_to_utf8(string) ⇒ Object
Convert string from UTF16 (big endian) encoding to UTF8 encoding and return it.
- #utf8_to_json ⇒ Object
- #utf8_to_utf16 ⇒ Object
Instance Method Details
#parse(source) ⇒ Object
Parse the JSON string source into a Ruby data structure and return it.
160 161 162 |
# File 'lib/fjson.rb', line 160 def parse(source) Parser.new(source).parse end |
#pretty_unparse(obj) ⇒ Object
Unparse the Ruby data structure obj into a JSON string and return it. The returned string is a prettier form of the string returned by #unparse.
173 174 175 176 177 178 179 180 181 |
# File 'lib/fjson.rb', line 173 def pretty_unparse(obj) state = JSON::State.new( :indent => ' ', :space => ' ', :object_nl => "\n", :array_nl => "\n" ) obj.to_json(state) end |
#support_unicode=(enable) ⇒ Object
Switches on Unicode support, if enable is true. Otherwise switches Unicode support off.
143 144 145 |
# File 'lib/fjson.rb', line 143 def support_unicode=(enable) @support_unicode = enable end |
#support_unicode? ⇒ Boolean
Returns true if JSON supports unicode, otherwise false is returned.
148 149 150 |
# File 'lib/fjson.rb', line 148 def support_unicode? !!@support_unicode end |
#unparse(obj, state = nil) ⇒ Object
Unparse the Ruby data structure obj into a single line JSON string and return it. state is a JSON::State object, that can be used to configure the output further.
167 168 169 |
# File 'lib/fjson.rb', line 167 def unparse(obj, state = nil) obj.to_json(JSON::State.from_state(state)) end |
#utf16_to_utf8(string) ⇒ Object
Convert string from UTF16 (big endian) encoding to UTF8 encoding and return it.
154 155 156 157 |
# File 'lib/fjson.rb', line 154 def utf16_to_utf8(string) bytes = '' << string[0, 2].to_i(16) << string[2, 2].to_i(16) JSON::UTF16toUTF8.iconv(bytes) end |