Class: Megam::JSONCompat
- Inherits:
-
Object
- Object
- Megam::JSONCompat
- Defined in:
- lib/megam/core/json_compat.rb
Constant Summary collapse
- JSON_MAX_NESTING =
1000- JSON_CLAZ =
'json_claz'.freeze
- MEGAM_ACCOUNT =
'Megam::Account'.freeze
- MEGAM_ACCOUNTCOLLECTION =
'Megam::AccountCollection'.freeze
- MEGAM_ASSEMBLIES =
'Megam::Assemblies'.freeze
- MEGAM_ASSEMBLIESCOLLECTION =
'Megam::AssembliesCollection'.freeze
- MEGAM_ASSEMBLY =
'Megam::Assembly'.freeze
- MEGAM_ASSEMBLYCOLLECTION =
'Megam::AssemblyCollection'.freeze
- MEGAM_BALANCES =
'Megam::Balances'.freeze
- MEGAM_BALANCESCOLLECTION =
'Megam::BalancesCollection'.freeze
- MEGAM_BILLEDHISTORIES =
'Megam::Billedhistories'.freeze
- MEGAM_BILLEDHISTORIESCOLLECTION =
'Megam::BilledhistoriesCollection'.freeze
- MEGAM_BILLINGTRANSACTIONS =
'Megam::Billingtransactions'.freeze
- MEGAM_BILLINGTRANSACTIONSCOLLECTION =
'Megam::BillingtransactionsCollection'.freeze
- MEGAM_COMPONENTS =
'Megam::Components'.freeze
- MEGAM_COMPONENTSCOLLECTION =
'Megam::ComponentsCollection'.freeze
- MEGAM_DOMAIN =
'Megam::Domains'.freeze
- MEGAM_DOMAINCOLLECTION =
'Megam::DomainsCollection'.freeze
- MEGAM_ERROR =
'Megam::Error'.freeze
- MEGAM_MARKETPLACE =
'Megam::MarketPlace'.freeze
- MEGAM_MARKETPLACECOLLECTION =
'Megam::MarketPlaceCollection'.freeze
- MEGAM_ORGANIZATION =
'Megam::Organizations'.freeze
- MEGAM_ORGANIZATIONSCOLLECTION =
'Megam::OrganizationsCollection'.freeze
- MEGAM_REQUEST =
'Megam::Request'.freeze
- MEGAM_REQUESTCOLLECTION =
'Megam::RequestCollection'.freeze
- MEGAM_SENSORS =
'Megam::Sensors'.freeze
- MEGAM_SENSORSCOLLECTION =
'Megam::SensorsCollection'.freeze
- MEGAM_SNAPSHOTS =
'Megam::Snapshots'.freeze
- MEGAM_SNAPSHOTSCOLLECTION =
'Megam::SnapshotsCollection'.freeze
- MEGAM_DISKS =
'Megam::Disks'.freeze
- MEGAM_DISKSCOLLECTION =
'Megam::DisksCollection'.freeze
- MEGAM_SSHKEY =
'Megam::SshKey'.freeze
- MEGAM_SSHKEYCOLLECTION =
'Megam::SshKeyCollection'.freeze
- MEGAM_EVENTSALL =
'Megam::EventsAll'.freeze
- MEGAM_EVENTSALLCOLLECTION =
'Megam::EventsAllCollection'.freeze
- MEGAM_EVENTSVM =
'Megam::EventsVm'.freeze
- MEGAM_EVENTSVMCOLLECTION =
'Megam::EventsVmCollection'.freeze
- MEGAM_EVENTSCONTAINER =
'Megam::EventsContainer'.freeze
- MEGAM_EVENTSCONTAINERCOLLECTION =
'Megam::EventsContainerCollection'.freeze
- MEGAM_EVENTSBILLING =
'Megam::EventsBilling'.freeze
- MEGAM_EVENTSBILLINGCOLLECTION =
'Megam::EventsBillingCollection'.freeze
- MEGAM_EVENTSSTORAGE =
'Megam::EventsStorage'.freeze
- MEGAM_EVENTSSTORAGECOLLECTION =
'Megam::EventsStorageCollection'.freeze
- MEGAM_SUBSCRIPTIONS =
'Megam::Subscriptions'.freeze
- MEGAM_SUBSCRIPTIONSCOLLECTION =
'Megam::SubscriptionsCollection'.freeze
- MEGAM_ADDONS =
'Megam::Addons'.freeze
- MEGAM_ADDONSCOLLECTION =
'Megam::AddonsCollection'.freeze
- MEGAM_PROMOS =
'Megam::Promos'.freeze
Class Method Summary collapse
-
.class_for_json_class(json_class) ⇒ Object
Map
JSON_CLAZto a Class object. -
.from_json(source, opts = {}) ⇒ Object
Just call the JSON gem’s parse method with a modified :max_nesting field.
- .map_hash_to_rb_obj(json_hash) ⇒ Object
-
.map_to_rb_obj(json_obj) ⇒ Object
Look at an object that’s a basic type (from json parse) and convert it to an instance of Chef classes if desired.
-
.parse(source, opts = {}) ⇒ Object
API to use to avoid create_addtions.
- .to_json(obj, opts = nil) ⇒ Object
- .to_json_pretty(obj, opts = nil) ⇒ Object
Class Method Details
.class_for_json_class(json_class) ⇒ Object
Map JSON_CLAZ to a Class object. We use a case instead of a Hash assigned to a constant because otherwise this file could not be loaded until all the constants were defined, which means you’d have to load the world to get json.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/megam/core/json_compat.rb', line 140 def class_for_json_class(json_class) case json_class when MEGAM_ERROR Megam::Error when MEGAM_ACCOUNT Megam::Account when MEGAM_ACCOUNTCOLLECTION Megam::AccountCollection when MEGAM_ASSEMBLIES Megam::Assemblies when MEGAM_ASSEMBLIESCOLLECTION Megam::AssembliesCollection when MEGAM_ASSEMBLY Megam::Assembly when MEGAM_ASSEMBLYCOLLECTION Megam::AssemblyCollection when MEGAM_COMPONENTS Megam::Components when MEGAM_COMPONENTSCOLLECTION Megam::ComponentsCollection when MEGAM_REQUEST Megam::Request when MEGAM_REQUESTCOLLECTION Megam::RequestCollection when MEGAM_SSHKEY Megam::SshKey when MEGAM_SSHKEYCOLLECTION Megam::SshKeyCollection when MEGAM_EVENTSVM Megam::EventsVm when MEGAM_EVENTSVMCOLLECTION Megam::EventsVmCollection when MEGAM_EVENTSALL Megam::EventsAll when MEGAM_EVENTSALLCOLLECTION Megam::EventsAllCollection when MEGAM_EVENTSCONTAINER Megam::EventsContainer when MEGAM_EVENTSCONTAINERCOLLECTION Megam::EventsContainerCollection when MEGAM_EVENTSBILLING Megam::EventsBilling when MEGAM_EVENTSBILLINGCOLLECTION Megam::EventsBillingCollection when MEGAM_EVENTSSTORAGE Megam::EventsStorage when MEGAM_EVENTSSTORAGECOLLECTION Megam::EventsStorageCollection when MEGAM_MARKETPLACE Megam::MarketPlace when MEGAM_MARKETPLACECOLLECTION Megam::MarketPlaceCollection when MEGAM_ORGANIZATION Megam::Organizations when MEGAM_ORGANIZATIONSCOLLECTION Megam::OrganizationsCollection when MEGAM_DOMAIN Megam::Domains when MEGAM_DOMAINCOLLECTION Megam::DomainsCollection when MEGAM_SENSORS Megam::Sensors when MEGAM_SENSORSCOLLECTION Megam::SensorsCollection when MEGAM_SNAPSHOTS Megam::Snapshots when MEGAM_SNAPSHOTSCOLLECTION Megam::SnapshotsCollection when MEGAM_BALANCES Megam::Balances when MEGAM_BALANCESCOLLECTION Megam::BalancesCollection when MEGAM_BILLEDHISTORIES Megam::Billedhistories when MEGAM_BILLEDHISTORIESCOLLECTION Megam::BilledhistoriesCollection when MEGAM_BILLINGTRANSACTIONS Megam::Billingtransactions when MEGAM_BILLINGTRANSACTIONSCOLLECTION Megam::BillingtransactionsCollection when MEGAM_SUBSCRIPTIONS Megam::Subscriptions when MEGAM_SUBSCRIPTIONSCOLLECTION Megam::SubscriptionsCollection when MEGAM_DISKS Megam::Disks when MEGAM_DISKSCOLLECTION Megam::DisksCollection when MEGAM_ADDONS Megam::Addons when MEGAM_ADDONSCOLLECTION Megam::AddonsCollection when MEGAM_PROMOS Megam::Promos else fail JSON::ParserError, "Unsupported `json_class` type '#{json_class}'" end end |
.from_json(source, opts = {}) ⇒ Object
Just call the JSON gem’s parse method with a modified :max_nesting field
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/megam/core/json_compat.rb', line 72 def from_json(source, opts = {}) obj = parse(source, opts) # JSON gem requires top level object to be a Hash or Array (otherwise # you get the "must contain two octets" error). Yajl doesn't impose the # same limitation. For compatibility, we re-impose this condition. unless obj.kind_of?(Hash) || obj.kind_of?(Array) raise JSON::ParseError, "Top level JSON object must be a Hash or Array. (actual: #{obj.class})" end # The old default in the json gem (which we are mimicing because we # sadly rely on this misfeature) is to "create additions" i.e., convert # JSON objects into ruby objects. Explicit :create_additions => false # is required to turn it off. if opts[:create_additions].nil? || opts[:create_additions] map_to_rb_obj(obj) else obj end end |
.map_hash_to_rb_obj(json_hash) ⇒ Object
111 112 113 114 115 116 |
# File 'lib/megam/core/json_compat.rb', line 111 def map_hash_to_rb_obj(json_hash) json_hash.each do |key, value| json_hash[key] = map_to_rb_obj(value) end json_hash end |
.map_to_rb_obj(json_obj) ⇒ Object
Look at an object that’s a basic type (from json parse) and convert it to an instance of Chef classes if desired.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/megam/core/json_compat.rb', line 95 def map_to_rb_obj(json_obj) case json_obj when Hash mapped_hash = map_hash_to_rb_obj(json_obj) if json_obj.has_key?(JSON_CLAZ) && (class_to_inflate = class_for_json_class(json_obj[JSON_CLAZ])) class_to_inflate.json_create(mapped_hash) else mapped_hash end when Array json_obj.map { |e| map_to_rb_obj(e) } else json_obj end end |
.parse(source, opts = {}) ⇒ Object
API to use to avoid create_addtions
63 64 65 66 67 68 69 |
# File 'lib/megam/core/json_compat.rb', line 63 def parse(source, opts = {}) begin FFI_Yajl::Parser.parse(source, opts) rescue FFI_Yajl::ParseError => e raise StandardError, e. end end |
.to_json(obj, opts = nil) ⇒ Object
118 119 120 121 122 123 124 |
# File 'lib/megam/core/json_compat.rb', line 118 def to_json(obj, opts = nil) begin FFI_Yajl::Encoder.encode(obj, opts) rescue FFI_Yajl::EncodeError => e raise JSON::GeneratorError, e. end end |
.to_json_pretty(obj, opts = nil) ⇒ Object
127 128 129 130 131 132 133 |
# File 'lib/megam/core/json_compat.rb', line 127 def to_json_pretty(obj, opts = nil) opts ||= {} = {} [:pretty] = true [:indent] = opts[:indent] if opts.has_key?(:indent) to_json(obj, ).chomp end |