Class: ActiveRecordGraphExtractor::JSONSerializer
- Inherits:
-
Object
- Object
- ActiveRecordGraphExtractor::JSONSerializer
- Defined in:
- lib/activerecord_graph_extractor/json_serializer.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #deserialize_from_file(file_path) ⇒ Object
- #deserialize_from_string(json_string) ⇒ Object
- #estimate_file_size(data) ⇒ Object
-
#initialize(config = Configuration.new) ⇒ JSONSerializer
constructor
A new instance of JSONSerializer.
- #serialize_to_file(data, file_path) ⇒ Object
- #serialize_to_string(data) ⇒ Object
- #validate_json_structure(data) ⇒ Object
Constructor Details
#initialize(config = Configuration.new) ⇒ JSONSerializer
Returns a new instance of JSONSerializer.
11 12 13 |
# File 'lib/activerecord_graph_extractor/json_serializer.rb', line 11 def initialize(config = Configuration.new) @config = config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
9 10 11 |
# File 'lib/activerecord_graph_extractor/json_serializer.rb', line 9 def config @config end |
Instance Method Details
#deserialize_from_file(file_path) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/activerecord_graph_extractor/json_serializer.rb', line 27 def deserialize_from_file(file_path) raise Errno::ENOENT, "No such file or directory @ rb_sysopen - #{file_path}" unless File.exist?(file_path) if config.stream_json stream_deserialize_from_file(file_path) else Oj.load_file(file_path, mode: :compat) end end |
#deserialize_from_string(json_string) ⇒ Object
37 38 39 |
# File 'lib/activerecord_graph_extractor/json_serializer.rb', line 37 def deserialize_from_string(json_string) Oj.load(json_string, mode: :compat) end |
#estimate_file_size(data) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/activerecord_graph_extractor/json_serializer.rb', line 85 def estimate_file_size(data) # Rough estimation based on JSON serialization sample_size = [data.dig('records')&.values&.first&.size || 0, 100].min if sample_size > 0 sample_data = data.dup sample_data['records'] = data['records'].transform_values do |records| records.first(sample_size) end sample_json = serialize_to_string(sample_data) total_records = data['records'].values.sum(&:size) (sample_json.bytesize.to_f / sample_size * total_records).round else serialize_to_string(data).bytesize end end |
#serialize_to_file(data, file_path) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/activerecord_graph_extractor/json_serializer.rb', line 15 def serialize_to_file(data, file_path) if config.stream_json stream_serialize_to_file(data, file_path) else File.write(file_path, serialize_to_string(data)) end end |
#serialize_to_string(data) ⇒ Object
23 24 25 |
# File 'lib/activerecord_graph_extractor/json_serializer.rb', line 23 def serialize_to_string(data) Oj.dump(data, mode: :compat, indent: 2) end |
#validate_json_structure(data) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/activerecord_graph_extractor/json_serializer.rb', line 41 def validate_json_structure(data) errors = [] # Check required metadata unless data.is_a?(Hash) errors << "Root data must be a hash" return errors end = data['metadata'] unless .is_a?(Hash) errors << "Missing or invalid metadata section" return errors end = %w[root_model root_id extracted_at schema_version] .each do |field| unless .key?(field) errors << "Missing required metadata field: #{field}" end end # Check records structure records = data['records'] unless records.is_a?(Hash) errors << "Missing or invalid records section" return errors end records.each do |model_name, model_records| unless model_records.is_a?(Array) errors << "Records for #{model_name} must be an array" next end model_records.each_with_index do |record, index| record_errors = validate_record_structure(record, model_name, index) errors.concat(record_errors) end end errors end |