Class: JSON

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

Defined Under Namespace

Classes: Malformed

Class Method Summary collapse

Class Method Details

.generate(object, pretty = false) ⇒ Object



15
16
17
18
# File 'lib/under_os/core/json.rb', line 15

def self.generate(object, pretty=false)
  options = pretty ? NSJSONWritingPrettyPrinted : 0
  NSJSONSerialization.dataWithJSONObject(object, options:options, error:nil).to_s
end

.parse(string, encoding = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/under_os/core/json.rb', line 4

def self.parse(string, encoding=nil)
  error = Pointer.new(:object)
  data  = NSJSONSerialization.JSONObjectWithData string.to_data(encoding), options:0, error:error

  if error[0]
    raise Malformed
  else
    data
  end
end