Class: Mixlib::JSON

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Class Attribute Details

.json_objObject (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_objectsObject



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.

Parameters:

  • Whether (Boolean)

    to create objects



36
37
38
# File 'lib/mixlib/json.rb', line 36

def create_objects=(boolean)
  @create_objects = boolean
end

.detect_loaded_libraryBoolean

Checks to see if a JSON library is already loaded

  • if it is, we’ll use it for serialization.

Returns:

  • (Boolean)

    true if we loaded a library, false otherwise.



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.

Parameters:

  • Generates (Object)

    JSON for this object.

Returns:

  • (JSON)

    Returns a JSON string.



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_orderObject



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.

Parameters:

  • An (Array)

    array of classes to use.



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.

Parameters:

  • A (String)

    JSON string.

Returns:

  • (Object)

    A ruby object.



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.

Parameters:

  • Generates (Object)

    pretty JSON for this object.

Returns:

  • (JSON)

    Returns a JSON string.



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_libraryMixlib::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.

Returns:

Raises:

  • (ArgumentError)


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