pg_json 0.1.11 → 0.1.12
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.
- data/bin/pg_json +1 -1
- data/lib/pg_json/sequel/pg_json.rb +45 -0
- metadata +1 -1
data/bin/pg_json
CHANGED
@@ -89,7 +89,7 @@ simple :time do
|
|
89
89
|
end
|
90
90
|
simple :timestamp do
|
91
91
|
# "extract(epoch from data)::integer::text"
|
92
|
-
"'{\"__type__\":\"timestamp\",\"value\"
|
92
|
+
"'{\"__type__\":\"timestamp\",\"value\":'||extract(epoch from data)::integer::text||'}'"
|
93
93
|
end
|
94
94
|
|
95
95
|
while composite = ARGV.slice!(0)
|
@@ -1,4 +1,38 @@
|
|
1
|
+
JSON_TYPES = {} unless defined? JSON_TYPES
|
2
|
+
JSON_TYPES[:date] = lambda do |v|
|
3
|
+
v.nil? ? nil : v.to_date
|
4
|
+
end
|
5
|
+
JSON_TYPES[:time] = lambda do |v|
|
6
|
+
v.nil? ? nil : v.to_time
|
7
|
+
end
|
8
|
+
JSON_TYPES[:datetime] = lambda do |v|
|
9
|
+
v.nil? ? nil : v.to_datetime
|
10
|
+
end
|
11
|
+
|
1
12
|
class Hash
|
13
|
+
def json_typify!
|
14
|
+
replacements = []
|
15
|
+
self.each do |k, v|
|
16
|
+
if v.is_a? Hash
|
17
|
+
if h[:__type__]
|
18
|
+
if h[:value]
|
19
|
+
replacements << [k, JSON_TYPES[h[:__type__]].call(h[:value])]
|
20
|
+
else
|
21
|
+
replacements << [k, nil]
|
22
|
+
end
|
23
|
+
else
|
24
|
+
v.json_typify!
|
25
|
+
end
|
26
|
+
elsif v.is_a? Array
|
27
|
+
v.json_typify!
|
28
|
+
end
|
29
|
+
end
|
30
|
+
replacements.each do |r|
|
31
|
+
k, v = r
|
32
|
+
self[k] = v
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
2
36
|
def recursively_symbolize_keys!
|
3
37
|
self.symbolize_keys!
|
4
38
|
self.values.each do |v|
|
@@ -13,6 +47,16 @@ class Hash
|
|
13
47
|
end
|
14
48
|
|
15
49
|
class Array
|
50
|
+
def json_typify!
|
51
|
+
self.each do |item|
|
52
|
+
if item.is_a? Hash
|
53
|
+
item.json_typify!
|
54
|
+
elsif item.is_a? Array
|
55
|
+
item.json_typify!
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
16
60
|
def recursively_symbolize_keys!
|
17
61
|
self.each do |item|
|
18
62
|
if item.is_a? Hash
|
@@ -31,6 +75,7 @@ module Sequel
|
|
31
75
|
if self.first
|
32
76
|
JSON.parse(self.first[:to_json]).tap do |h|
|
33
77
|
h.recursively_symbolize_keys!
|
78
|
+
h.json_typify!
|
34
79
|
end
|
35
80
|
else
|
36
81
|
nil
|