Class: Mixlib::JSON
- Inherits:
-
Object
- Object
- Mixlib::JSON
- Defined in:
- lib/mixlib/json.rb,
lib/mixlib/json/gem.rb,
lib/mixlib/json/yajl.rb,
lib/mixlib/json/activesupport.rb
Defined Under Namespace
Classes: ActiveSupport, Gem, YAJL
Class Attribute Summary collapse
-
.json_obj ⇒ Object
readonly
Returns the value of attribute json_obj.
Class Method Summary collapse
- .create_objects ⇒ Object
-
.create_objects=(boolean) ⇒ Object
If you want to force JSON gem style automatic object creation, set this to true.
-
.detect_loaded_library ⇒ Boolean
Checks to see if a JSON library is already loaded - if it is, we’ll use it for serialization.
-
.generate(obj) ⇒ JSON
Generates JSON.
- .json_load_order ⇒ Object
-
.json_load_order=(load_order) ⇒ Object
Set the order in which you prefer your serializers to be used, if none is already loaded.
-
.parse(data) ⇒ Object
Creates a Ruby object from a JSON string.
-
.pretty(obj) ⇒ JSON
Pretty generates JSON.
-
.select_json_library ⇒ Mixlib::JSON::*
First detects if a JSON library is already loaded, and if it is, uses it.
Class Attribute Details
.json_obj ⇒ Object (readonly)
Returns the value of attribute json_obj.
26 27 28 |
# File 'lib/mixlib/json.rb', line 26 def json_obj @json_obj end |
Class Method Details
.create_objects ⇒ Object
40 41 42 |
# File 'lib/mixlib/json.rb', line 40 def create_objects @create_objects ||= false end |
.create_objects=(boolean) ⇒ Object
If you want to force JSON gem style automatic object creation, set this to true.
This means if your JSON has a json_class key with a valid class, and that class responds to from_json, it will have that method called with the JSON object as it’s argument.
36 37 38 |
# File 'lib/mixlib/json.rb', line 36 def create_objects=(boolean) @create_objects = boolean end |
.detect_loaded_library ⇒ Boolean
Checks to see if a JSON library is already loaded
-
if it is, we’ll use it for serialization.
67 68 69 70 71 72 73 74 75 |
# File 'lib/mixlib/json.rb', line 67 def detect_loaded_library json_library_class = json_load_order.detect { |lib| lib.is_loaded? } if json_library_class @json_obj = json_library_class.new true else false end end |
.generate(obj) ⇒ JSON
Generates JSON.
99 100 101 102 |
# File 'lib/mixlib/json.rb', line 99 def generate(obj) select_json_library unless @json_obj @json_obj.generate(obj) end |
.json_load_order ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/mixlib/json.rb', line 55 def json_load_order @json_load_order ||= [ Mixlib::JSON::YAJL, Mixlib::JSON::Gem, Mixlib::JSON::ActiveSupport ] end |
.json_load_order=(load_order) ⇒ Object
Set the order in which you prefer your serializers to be used, if none is already loaded. Should be class names that are children of Mixlib::JSON.
Defaults to YAJL, JSON Gem, and ActiveSupport.
51 52 53 |
# File 'lib/mixlib/json.rb', line 51 def json_load_order=(load_order) @json_load_order = load_order end |
.parse(data) ⇒ Object
Creates a Ruby object from a JSON string. If create_objects is true, might return a fully-inflated object of the class specified in json_class.
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/mixlib/json.rb', line 120 def parse(data) select_json_library unless @json_obj o = @json_obj.parse(data) if @create_objects && o.kind_of?(Hash) if o.has_key?("json_class") class_name = o["json_class"].split('::').inject(Kernel) do |scope, const_name| scope.const_get(const_name) end if class_name.respond_to?(:from_json) return class_name.from_json(o) else return o end end else return o end end |
.pretty(obj) ⇒ JSON
Pretty generates JSON.
108 109 110 111 |
# File 'lib/mixlib/json.rb', line 108 def pretty(obj) select_json_library unless @json_obj @json_obj.pretty(obj) end |
.select_json_library ⇒ Mixlib::JSON::*
First detects if a JSON library is already loaded, and if it is, uses it.
If it isn’t, uses the json_load_order to select the best available library.
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/mixlib/json.rb', line 84 def select_json_library unless detect_loaded_library json_load_order.each do |lib| @json_obj = lib.load break if @json_obj end end raise ArgumentError, "Cannot load a json library!" unless @json_obj @json_obj end |