console 1.21.0 → 1.22.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/lib/console/output/encoder.rb +1 -0
- data/lib/console/output/json.rb +87 -5
- data/lib/console/version.rb +1 -1
- data.tar.gz.sig +0 -0
- metadata +2 -2
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9f868615689827a90fa20cb73c9408598bd6cfc9602b21ee939a318213a3aee1
|
4
|
+
data.tar.gz: 44927a1200deac00fab718aa995f0549eec0301b688a440da83ae13fd87edc33
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eabb7c42bcb625726e75a7ebc1e7f330b7dcb0b8936b012c96c79b8a5456f0ac2b3fea5d02cdb1511fde9bd696c7ba300b6394952138810824cb18bdbe8e8e6e
|
7
|
+
data.tar.gz: 1afa8d02eb8e56c81ac39f925e6a5df7edad41198c8aa255eb418847076913b6965b7d5794ba04ed6d5d35b08a1785e8825e12d32a8398f4203f7197584670c1
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data/lib/console/output/json.rb
CHANGED
@@ -4,16 +4,98 @@
|
|
4
4
|
# Copyright, 2021-2022, by Samuel Williams.
|
5
5
|
|
6
6
|
require_relative '../serialized/logger'
|
7
|
-
require_relative 'encoder'
|
8
7
|
|
9
8
|
module Console
|
10
9
|
module Output
|
11
10
|
module JSON
|
11
|
+
# This is a safe JSON serializer that will not raise exceptions.
|
12
|
+
class Safe
|
13
|
+
def initialize(limit: 8, encoding: ::Encoding::UTF_8)
|
14
|
+
@limit = limit
|
15
|
+
@encoding = encoding
|
16
|
+
end
|
17
|
+
|
18
|
+
def dump(object)
|
19
|
+
::JSON.dump(object, @limit)
|
20
|
+
rescue => error
|
21
|
+
::JSON.dump(safe_dump(object, error))
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def default_objects
|
27
|
+
Hash.new.compare_by_identity
|
28
|
+
end
|
29
|
+
|
30
|
+
def safe_dump(object, error)
|
31
|
+
object = safe_dump_recurse(object)
|
32
|
+
|
33
|
+
object[:truncated] = true
|
34
|
+
object[:error] = {
|
35
|
+
class: safe_dump_recurse(error.class.name),
|
36
|
+
message: safe_dump_recurse(error.message),
|
37
|
+
}
|
38
|
+
|
39
|
+
return object
|
40
|
+
end
|
41
|
+
|
42
|
+
def replacement_for(object)
|
43
|
+
case object
|
44
|
+
when Array
|
45
|
+
"[...]"
|
46
|
+
when Hash
|
47
|
+
"{...}"
|
48
|
+
else
|
49
|
+
"..."
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# This will recursively generate a safe version of the object.
|
54
|
+
# Nested hashes and arrays will be transformed recursively.
|
55
|
+
# Strings will be encoded with the given encoding.
|
56
|
+
# Primitive values will be returned as-is.
|
57
|
+
# Other values will be converted using `as_json` if available, otherwise `to_s`.
|
58
|
+
def safe_dump_recurse(object, limit = @limit, objects = default_objects)
|
59
|
+
if limit <= 0 || objects[object]
|
60
|
+
return replacement_for(object)
|
61
|
+
end
|
62
|
+
|
63
|
+
case object
|
64
|
+
when Hash
|
65
|
+
objects[object] = true
|
66
|
+
|
67
|
+
object.to_h do |key, value|
|
68
|
+
[
|
69
|
+
String(key).encode(@encoding, invalid: :replace, undef: :replace),
|
70
|
+
safe_dump_recurse(value, limit - 1, objects)
|
71
|
+
]
|
72
|
+
end
|
73
|
+
when Array
|
74
|
+
objects[object] = true
|
75
|
+
|
76
|
+
object.map do |value|
|
77
|
+
safe_dump_recurse(value, limit - 1, objects)
|
78
|
+
end
|
79
|
+
when String
|
80
|
+
object.encode(@encoding, invalid: :replace, undef: :replace)
|
81
|
+
when Numeric, TrueClass, FalseClass, NilClass
|
82
|
+
object
|
83
|
+
else
|
84
|
+
objects[object] = true
|
85
|
+
|
86
|
+
# We could do something like this but the chance `as_json` will blow up.
|
87
|
+
# We'd need to be extremely careful about it.
|
88
|
+
# if object.respond_to?(:as_json)
|
89
|
+
# safe_dump_recurse(object.as_json, limit - 1, objects)
|
90
|
+
# else
|
91
|
+
|
92
|
+
safe_dump_recurse(object.to_s, limit - 1, objects)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
12
97
|
def self.new(output, **options)
|
13
|
-
|
14
|
-
Output::Encoder.new(
|
15
|
-
Serialized::Logger.new(output, format: ::JSON, **options)
|
16
|
-
)
|
98
|
+
Serialized::Logger.new(output, format: Safe.new, **options)
|
17
99
|
end
|
18
100
|
end
|
19
101
|
end
|
data/lib/console/version.rb
CHANGED
data.tar.gz.sig
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: console
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.22.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Williams
|
@@ -46,7 +46,7 @@ cert_chain:
|
|
46
46
|
Q2K9NVun/S785AP05vKkXZEFYxqG6EW012U4oLcFl5MySFajYXRYbuUpH6AY+HP8
|
47
47
|
voD0MPg1DssDLKwXyt1eKD/+Fq0bFWhwVM/1XiAXL7lyYUyOq24KHgQ2Csg=
|
48
48
|
-----END CERTIFICATE-----
|
49
|
-
date: 2023-08-
|
49
|
+
date: 2023-08-11 00:00:00.000000000 Z
|
50
50
|
dependencies:
|
51
51
|
- !ruby/object:Gem::Dependency
|
52
52
|
name: fiber-annotation
|
metadata.gz.sig
CHANGED
Binary file
|