Class: PDK::Config::JSONWithSchema
- Inherits:
-
JSONSchemaNamespace
- Object
- Namespace
- JSONSchemaNamespace
- PDK::Config::JSONWithSchema
- Defined in:
- lib/pdk/config/json_with_schema.rb
Instance Attribute Summary
Attributes inherited from Namespace
Instance Method Summary collapse
-
#parse_file(filename) ⇒ Object
Parses a JSON document with a schema.
-
#serialize_data(data) ⇒ Object
Serializes object data into a JSON string.
Methods inherited from JSONSchemaNamespace
#empty_schema?, #initialize, #schema, #schema_property_names, #to_h, #validate_document!
Methods inherited from Namespace
#[], #[]=, #child_namespace?, #fetch, #include_in_parent?, #initialize, #mount, #namespace, #read_only!, #resolve, #setting, #to_h
Constructor Details
This class inherits a constructor from PDK::Config::JSONSchemaNamespace
Instance Method Details
#parse_file(filename) ⇒ Object
Parses a JSON document with a schema.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/pdk/config/json_with_schema.rb', line 9 def parse_file(filename) raise unless block_given? data = load_data(filename) data = '{}' if data.nil? || data.empty? require 'json' @raw_data = ::JSON.parse(data) @raw_data = {} if @raw_data.nil? begin # Ensure the parsed document is actually valid validate_document!(@raw_data) rescue ::JSON::Schema::ValidationError => e raise PDK::Config::LoadError, format('The configuration file %{filename} is not valid: %{message}', filename: filename, message: e.) end schema_property_names.each do |key| yield key, PDK::Config::JSONSchemaSetting.new(key, self, @raw_data[key]) end # Remove all of the "known" settings from the schema and # we're left with the settings that we don't manage. self.unmanaged_settings = @raw_data.reject { |k, _| schema_property_names.include?(k) } rescue ::JSON::ParserError => e raise PDK::Config::LoadError, e. end |
#serialize_data(data) ⇒ Object
Serializes object data into a JSON string.
40 41 42 43 44 |
# File 'lib/pdk/config/json_with_schema.rb', line 40 def serialize_data(data) require 'json' ::JSON.pretty_generate(data) end |