Module: RestCore::Json
- Defined in:
- lib/rest-core/util/json.rb
Defined Under Namespace
Modules: Json, MultiJson, YajlRuby
Class Method Summary
collapse
Class Method Details
.empty_to_null(json) ⇒ Object
86
87
88
89
90
91
92
|
# File 'lib/rest-core/util/json.rb', line 86
def self.empty_to_null json
if json.empty?
'null'
else
json
end
end
|
.normalize(json) ⇒ Object
65
66
67
|
# File 'lib/rest-core/util/json.rb', line 65
def self.normalize json
empty_to_null(strip_bom(json))
end
|
.select_json!(mod, picked = false) ⇒ Object
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/rest-core/util/json.rb', line 40
def self.select_json! mod, picked=false
if Object.const_defined?(:MultiJson)
mod.send(:extend, MultiJson)
elsif Object.const_defined?(:Yajl)
mod.send(:extend, YajlRuby)
elsif Object.const_defined?(:JSON)
mod.send(:extend, Json)
elsif picked
raise LoadError.new(
'No JSON library found. Tried: multi_json, yajl-ruby, json.')
else
%w[multi_json yajl json].each{ |json|
begin
require json
break
rescue LoadError
end
}
select_json!(mod, true)
end
end
|
.strip_bom(json) ⇒ Object
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/rest-core/util/json.rb', line 69
def self.strip_bom json
case json.encoding.name
when 'UTF-8'
json.sub(/\A\xEF\xBB\xBF/u, '')
when 'ASCII-8BIT'
json.sub(/\A\xEF\xBB\xBF/n, '')
else
json
end
end
|