json_to_struct 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/json_to_struct.rb +39 -0
- metadata +58 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 13bf711c4c005b66f18b050f9325d696a1aea6071c202669ca4d8d8753cb3e07
|
4
|
+
data.tar.gz: 21df82a938aeed388642510cb50c71e684e4cd8246dbbf7c2208322ab6c1075c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e88cabbaf5d21436b49232b4f8ba62e987e5893ca4645d026ca5034e7cc93eb44496a01c30510578100b54dcfd11c1ae86c16611019b5cd86e6c51c1d1a24464
|
7
|
+
data.tar.gz: f28d8b8f0344e9166c67813072757cbc9846a6be431740913f81d9b1756abf231cec88f72e7618ad850dff5076cce69eb98fefe0077419871e8388d96f67758e
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'oj'
|
2
|
+
|
3
|
+
# JsonToStruct is a module that converts a JSON string to a nested struct
|
4
|
+
#
|
5
|
+
# usage:
|
6
|
+
# > s = JsonToStruct.load(
|
7
|
+
# <<~JSON
|
8
|
+
# {
|
9
|
+
# "employee": {
|
10
|
+
# "name":"Hal",
|
11
|
+
# "salary":56000,
|
12
|
+
# "married":false
|
13
|
+
# }
|
14
|
+
# }
|
15
|
+
# JSON
|
16
|
+
# )
|
17
|
+
# => #<struct employee=
|
18
|
+
# #<struct name="Hal", salary=56000, married=false>>
|
19
|
+
# > s.employee.name
|
20
|
+
# => 'Hal'
|
21
|
+
module JsonToStruct
|
22
|
+
def self.load(json_string)
|
23
|
+
json = Oj.load(json_string, symbol_keys: true)
|
24
|
+
_load_helper(json)
|
25
|
+
end
|
26
|
+
|
27
|
+
def self._load_helper(json)
|
28
|
+
case json
|
29
|
+
when Hash
|
30
|
+
struct = ::Struct.new(*json.keys)
|
31
|
+
struct.new(*json.values.map { |v| _load_helper(v) })
|
32
|
+
when Array
|
33
|
+
json.map { |v| _load_helper(v) }
|
34
|
+
else
|
35
|
+
json
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: json_to_struct
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jeff Lunt
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-01-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: oj
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: converts a JSON String to a nested Struct
|
28
|
+
email: jefflunt@gmail.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- lib/json_to_struct.rb
|
34
|
+
homepage: https://github.com/jefflunt/json_to_struct
|
35
|
+
licenses:
|
36
|
+
- MIT
|
37
|
+
metadata: {}
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubygems_version: 3.4.1
|
54
|
+
signing_key:
|
55
|
+
specification_version: 4
|
56
|
+
summary: converts a JSON String to a tree of nested Struct objects, so you can use
|
57
|
+
dot notation, using the Oj gem
|
58
|
+
test_files: []
|