json_struct_mapper 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1e1a4c44ea87b099135a38beb8be6cf25462334d112b117317ad735335e6f72b
4
- data.tar.gz: f036202b6da95d30cfc4daf23b10719190a9422d3151e7e5bc847b735f98c6b5
3
+ metadata.gz: 3a3eedc414500a84c5c9bb68c9cf9a1cfa2ec77a19ef2c109e70fe1e5f5cc6f8
4
+ data.tar.gz: 5abf03d2e69b2e7489417d03d9abc6fbe1058eeb798fdcd772a0ec6675d7a33f
5
5
  SHA512:
6
- metadata.gz: e366722a7e60c18cf09be666f5eb4017139d2880f242478e3ce76cb1571bd8ed26898d038e82357853923bdfc9317bfbff1007a68ab85e95d31f1a660d816a29
7
- data.tar.gz: 982ba9aa76a859dd21ecf6ffbee6edcf4622d14b042dfbe544ed03edae46e2aeda1f4221b3ac0efe9c8e35e78a442de9b56502cdbe67ebafb9b686d5dbc2bf86
6
+ metadata.gz: 2baa59fbcc6ee1e2aa0a246a068542880852d23648bf0f1454202269a75ca0f7172fe6cb3502c7fa595f5bb642729072f78e8f7edc64afddb7b84f8288d7c912
7
+ data.tar.gz: 6c1ebc6c715a256441ea8a91432c60e628c73e88c5ea3ca805d08341ff52c669a040a7d44edcd180b704459d3ed1dd02bcf65597316436d4ace19ca8111f11a2
data/README.md CHANGED
@@ -30,6 +30,19 @@ converter = JsonStructMapper.from_file('data.json', 'users')
30
30
  converter.object.name
31
31
  converter.object.email
32
32
  ```
33
+ ### From a JSON file and create a template of the json
34
+ ```ruby
35
+ # Load entire JSON file
36
+ converter = JsonStructMapper.from_json_file_to_template('data.json')
37
+
38
+ # Load specific key from JSON file
39
+ converter = JsonStructMapper.from_json_file_to_template('data.json', 'users')
40
+
41
+ # Default struct values
42
+ converter.object.name # => nil
43
+ # Assign data via struct
44
+ converter.object.name = 'Jhon'
45
+ ```
33
46
  ### From a Hash
34
47
  ```ruby
35
48
  hash = { name: 'John', age: 30, address: { city: 'NYC' } }
@@ -38,6 +51,13 @@ converter = JsonStructMapper.from_hash(hash)
38
51
  converter.object.name # => 'John'
39
52
  converter.object.address.city # => 'NYC'
40
53
  ```
54
+ empty hash will return nil value
55
+ ```ruby
56
+ hash = { name: 'John', age: 30, address: {} }
57
+ converter = JsonStructMapper.from_hash(hash)
58
+
59
+ converter.object.address # => nil
60
+ ```
41
61
  ### From a JSON string
42
62
  ```ruby
43
63
  json_string = '{"name": "John", "age": 30}'
@@ -43,6 +43,40 @@ module JsonStructMapper
43
43
  raise InvalidJSONError, "Failed to parse JSON: #{e.message}"
44
44
  end
45
45
 
46
+ # Create a Converter from a JSON file, initialising with nil values
47
+ # @param file_path [String] Path to the JSON file
48
+ # @return [Converter] new instance
49
+ # @raise [FileNotFoundError] if file doesn't exist
50
+ # @raise [InvalidJSONError] if JSON is malformed
51
+ def self.from_json_file_to_template(file_path, key = nil)
52
+ instance = new(file_path, key) # Create an instance of the class
53
+ struct = instance.send(:load_from_file, file_path, key) # Call the instance method
54
+ instance.instance_variable_set(:@object, instance.send(:reset_struct_values, struct))
55
+ instance
56
+ end
57
+
58
+ # Function to remove all values from a struct and initialize with nil
59
+ #
60
+ # @param struct [Struct] to convert
61
+ # @return [Struct] struct with nil values
62
+ def reset_struct_values(struct)
63
+ struct.members.each do |member|
64
+ value = struct[member]
65
+ if value.is_a?(Struct)
66
+ reset_struct_values(value) # Recursively reset nested structs
67
+ elsif value.is_a?(Array)
68
+ if value.any? { |item| item.is_a?(Struct) }
69
+ value.each { |item| reset_struct_values(item) if item.is_a?(Struct) } # Reset structs inside arrays
70
+ else
71
+ struct[member] = nil # Set the array to nil if it doesn't contain structs
72
+ end
73
+ else
74
+ struct[member] = nil
75
+ end
76
+ end
77
+ struct
78
+ end
79
+
46
80
  # Convert the struct object back to a hash
47
81
  #
48
82
  # @param compact [Boolean] whether to remove nil values
@@ -83,6 +117,7 @@ module JsonStructMapper
83
117
  # @return [Struct] converted struct
84
118
  def hash_to_struct(hash)
85
119
  return hash unless hash.is_a?(Hash)
120
+ return nil if hash.is_a?(Hash) && hash.empty? # Return nil for empty hashes
86
121
 
87
122
  struct_class = Struct.new(*hash.keys.map(&:to_sym))
88
123
  struct_class.new(*hash.values.map { |value| convert_value_to_struct(value) })
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsonStructMapper
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
@@ -21,5 +21,10 @@ module JsonStructMapper
21
21
  def from_json(json_string, key = nil)
22
22
  Converter.from_json(json_string, key)
23
23
  end
24
+
25
+ # Convenience method to create a converter from a JSON file and creating template of it
26
+ def from_json_file_to_template(file_path, key = nil)
27
+ Converter.from_json_file_to_template(file_path, key)
28
+ end
24
29
  end
25
30
  end
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_struct_mapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shivani Uppala
8
8
  - Sumit Ghosh
9
+ autorequire:
9
10
  bindir: exe
10
11
  cert_chain: []
11
- date: 2025-11-12 00:00:00.000000000 Z
12
+ date: 2025-11-21 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: json
@@ -90,6 +91,7 @@ licenses:
90
91
  metadata:
91
92
  homepage_uri: https://github.com/UppalaShivani/json_struct_mapper/blob/main/README.md
92
93
  source_code_uri: https://github.com/UppalaShivani/json_struct_mapper
94
+ post_install_message:
93
95
  rdoc_options: []
94
96
  require_paths:
95
97
  - lib
@@ -104,7 +106,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
106
  - !ruby/object:Gem::Version
105
107
  version: '0'
106
108
  requirements: []
107
- rubygems_version: 3.6.5
109
+ rubygems_version: 3.5.3
110
+ signing_key:
108
111
  specification_version: 4
109
112
  summary: Convert JSON data into Ruby Struct objects with bidirectional mapping
110
113
  test_files: []