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_ASSEMBLIES =
"Megam::Assemblies".freeze
- MEGAM_ASSEMBLIESCOLLECTION =
"Megam::AssembliesCollection".freeze
- MEGAM_ASSEMBLY =
"Megam::Assembly".freeze
- MEGAM_ASSEMBLYCOLLECTION =
"Megam::AssemblyCollection".freeze
- MEGAM_AUTH =
"Megam::Auth".freeze
- MEGAM_AVAILABLEUNITS =
"Megam::Availableunits".freeze
- MEGAM_AVAILABLEUNITSCOLLECTION =
"Megam::AvailableunitsCollection".freeze
- MEGAM_BALANCES =
"Megam::Balances".freeze
- MEGAM_BALANCESCOLLECTION =
"Megam::BalancesCollection".freeze
- MEGAM_BILLEDHISTORIES =
"Megam::Billedhistories".freeze
- MEGAM_BILLEDHISTORIESCOLLECTION =
"Megam::BilledhistoriesCollection".freeze
- MEGAM_INVOICESCOLLECTION =
"Megam::InvoicesCollection".freeze
- MEGAM_INVOICES =
"Megam::Invoices".freeze
- MEGAM_BILLINGSCOLLECTION =
"Megam::BillingsCollection".freeze
- MEGAM_CLOUDTOOLSETTING =
"Megam::CloudToolSetting".freeze
- MEGAM_CLOUDTOOLSETTINGCOLLECTION =
"Megam::CloudToolSettingCollection".freeze
- MEGAM_COMPONENTS =
"Megam::Components".freeze
- MEGAM_COMPONENTSCOLLECTION =
"Megam::ComponentsCollection".freeze
- MEGAM_CREDITHISTORIES =
"Megam::Credithistories".freeze
- MEGAM_CREDITHISTORIESCOLLECTION =
"Megam::CredithistoriesCollection".freeze
- MEGAM_CSAR =
"Megam::CSAR".freeze
- MEGAM_CSARCOLLECTION =
"Megam::CSARCollection".freeze
- MEGAM_DOMAIN =
"Megam::Domains".freeze
- MEGAM_DISCOUNTS =
"Megam::Discounts".freeze
- MEGAM_DISCOUNTSCOLLECTION =
"Megam::DiscountsCollection".freeze
- MEGAM_ERROR =
"Megam::Error".freeze
- MEGAM_EVENT =
"Megam::Event".freeze
- MEGAM_MARKETPLACE =
"Megam::MarketPlace".freeze
- MEGAM_MARKETPLACECOLLECTION =
"Megam::MarketPlaceCollection".freeze
- MEGAM_MARKETPLACEADDON =
"Megam::MarketPlaceAddons".freeze
- MEGAM_MARKETPLACEADDONCOLLECTION =
"Megam::MarketPlaceAddonsCollection".freeze
- MEGAM_ORGANIZATION =
"Megam::Organizations".freeze
- MEGAM_ORGANIZATIONSCOLLECTION =
"Megam::OrganizationsCollection".freeze
- MEGAM_REQUEST =
"Megam::Request".freeze
- MEGAM_REQUESTCOLLECTION =
"Megam::RequestCollection".freeze
- MEGAM_SSHKEY =
"Megam::SshKey".freeze
- MEGAM_SSHKEYCOLLECTION =
"Megam::SshKeyCollection".freeze
- MEGAM_SUBSCRIPTIONS =
"Megam::Subscriptions".freeze
- MEGAM_SUBSCRIPTIONSCOLLECTION =
"Megam::SubscriptionsCollection".freeze
- MEGAM_PROMOS =
"Megam::Promos".freeze
Class Method Summary collapse
-
.class_for_json_class(json_class) ⇒ Object
Map
JSON_CLAZ
to 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 Megam classes if desired.
-
.opts_add_max_nesting(opts) ⇒ Object
Increase the max nesting for JSON, which defaults to 19, and isn’t enough for some (for example, a Node within a Node) structures.
- .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.
.from_json(source, opts = {}) ⇒ Object
Just call the JSON gem’s parse method with a modified :max_nesting field
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/megam/core/json_compat.rb', line 81 def from_json(source, opts = {}) obj = ::Yajl::Parser.parse(source) # 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) or obj.kind_of?(Array) raise JSON::ParserError, "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
123 124 125 126 127 128 |
# File 'lib/megam/core/json_compat.rb', line 123 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 Megam classes if desired.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/megam/core/json_compat.rb', line 104 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 |
.opts_add_max_nesting(opts) ⇒ Object
Increase the max nesting for JSON, which defaults to 19, and isn’t enough for some (for example, a Node within a Node) structures.
72 73 74 75 76 77 78 |
# File 'lib/megam/core/json_compat.rb', line 72 def opts_add_max_nesting(opts) if opts.nil? || !opts.has_key?(:max_nesting) opts = opts.nil? ? Hash.new : opts.clone opts[:max_nesting] = JSON_MAX_NESTING end opts end |
.to_json(obj, opts = nil) ⇒ Object
130 131 132 |
# File 'lib/megam/core/json_compat.rb', line 130 def to_json(obj, opts = nil) obj.to_json(opts_add_max_nesting(opts)) end |
.to_json_pretty(obj, opts = nil) ⇒ Object
134 135 136 |
# File 'lib/megam/core/json_compat.rb', line 134 def to_json_pretty(obj, opts = nil) ::JSON.pretty_generate(obj, opts_add_max_nesting(opts)) end |