cbor-diag 0.5.20 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8dbeccb9b1a15bac6beb52ab1388adddac2c19cf5fce92d2184de8120326fb5e
4
- data.tar.gz: ccd67a87d8e7c04518e0fc7852e4eac5076182cdbe58a97b7e2daea13acbfc53
3
+ metadata.gz: 9df17c4bf389a5b7b62a40e0dbe1fb86f8c10df05d0110cf0c6af8ddf83350ce
4
+ data.tar.gz: 8f853c4e98726ef055378775e21ae90f7192cc9568f77ec782f8d9744324169c
5
5
  SHA512:
6
- metadata.gz: 1f1cadddbfc82820493a77b7c50eb1bbb365be5a61016ee155f5ab88895f667d4c789d03b6d8e78983b5a765d34c316053dfbe8b730716f3d2979061759624b4
7
- data.tar.gz: e3eff5464c68065fb23c2158fba3209880878edb8ecb04bc225bd757b572343f23513856da112377517f2318c5d8c0f2872579e2faa197088eda952ac4e01e48
6
+ metadata.gz: bbec8c671dc5c080c7abe04f7f2ccd41a611d22f806190a7b41486906bcbe27bcb034fe92271533f4d17ca66732977c8a39ad7f7d5261139bb11f88a577b8d0f
7
+ data.tar.gz: 83229ae2d83a31294d5ec93fd760e1d8d6056ad1226e5febb1090eab6a7afc16d6bd9511b04943c3ea776640dcdd61a27b86e6682970ba3182af66fb3a4387ee
data/bin/cbor2json.rb CHANGED
@@ -6,7 +6,7 @@ require 'cbor-deterministic'
6
6
  require 'cbor-canonical'
7
7
 
8
8
  options = ''
9
- while /\A-([cdpq]+)\z/ === ARGV[0]
9
+ while /\A-([cdpqj]+)\z/ === ARGV[0]
10
10
  options << $1
11
11
  ARGV.shift
12
12
  end
@@ -18,4 +18,8 @@ o = o.to_packed_cbor if /p/ === options
18
18
  o = o.to_unpacked_cbor if /q/ === options
19
19
  o = o.cbor_pre_canonicalize if /c/ === options
20
20
  o = o.cbor_prepare_deterministic if /d/ === options
21
+ if /j/ === options
22
+ require 'cbor-transform-j'
23
+ o = CBOR::Transform_j.new.transform(o)
24
+ end
21
25
  puts JSON.pretty_generate(o)
data/bin/json2cbor.rb CHANGED
@@ -6,7 +6,7 @@ require 'cbor-deterministic'
6
6
  require 'cbor-canonical'
7
7
 
8
8
  options = ''
9
- while /\A-([cdpqv]+)\z/ === ARGV[0]
9
+ while /\A-([cdpqvj]+)\z/ === ARGV[0]
10
10
  options << $1
11
11
  ARGV.shift
12
12
  end
@@ -14,6 +14,10 @@ end
14
14
  $stdout.binmode
15
15
  i = ARGF.read
16
16
  o = JSON.load(i)
17
+ if /j/ === options
18
+ require 'cbor-transform-j'
19
+ o = CBOR::Transform_jr.new.transform(o)
20
+ end
17
21
  o = o.to_packed_cbor if /p/ === options
18
22
  o = o.to_unpacked_cbor if /q/ === options
19
23
  o = o.cbor_pre_canonicalize if /c/ === options
data/cbor-diag.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "cbor-diag"
3
- s.version = "0.5.20"
3
+ s.version = "0.6.1"
4
4
  s.summary = "CBOR (Concise Binary Object Representation) diagnostic notation"
5
5
  s.description = %q{cbor-diag implements diagnostic notation for CBOR, RFC 8949 and RFC 8742}
6
6
  s.author = "Carsten Bormann"
@@ -0,0 +1,68 @@
1
+ require 'cbor-transform'
2
+ unless defined?(JSON)
3
+ require 'json'
4
+ end
5
+ require 'base64'
6
+
7
+ class CBOR::Transform_j < CBOR::Transform
8
+
9
+ # CBOR::Transform_j.new(1).transform [1, "a", "b".b, CBOR::Simple.new(2), CBOR::Tagged.new(1, 2), {"a"=> 1, 2=> 3}]
10
+
11
+ def transform_simple(obj)
12
+ {'@@!s': obj.value}
13
+ end
14
+
15
+ def transform_bytes(obj)
16
+ {'@@!b': ::Base64.urlsafe_encode64(obj, padding: false)}
17
+ end
18
+
19
+ def transform_hash(obj)
20
+ Hash[obj.map {|k, v| [ (
21
+ kt = transform(k)
22
+ if (String === kt && kt.encoding != Encoding::BINARY)
23
+ kt
24
+ else
25
+ '@@!:' << JSON.generate(kt)
26
+ end
27
+ ),
28
+ transform(v)]}]
29
+ end
30
+
31
+ def transform_tag(obj)
32
+ {"@@!t#{obj.tag}": transform(obj.value)}
33
+ end
34
+
35
+ end
36
+
37
+ class CBOR::Transform_jr < CBOR::Transform
38
+
39
+ # >> CBOR::Transform_jr.new(1).transform(JSON.parse(JSON.generate(a = CBOR::Transform_j.new(1).transform([1, "a", "b".b, CBOR::Simple.new(2), CBOR::Tagged.new(1, 2), {"a"=> 1, 2=> 3, {3=>4}=>5}]))))
40
+ # => [1, "a", "b", #<struct CBOR::Simple value=2>, #<struct CBOR::Tagged tag=1, value=2>, {"a"=>1, 2=>3, {3=>4}=>5}]
41
+ # >> a
42
+ # => [1, "a", {:"@@!b"=>"Yg"}, {:"@@!s"=>2}, {:"@@!t1"=>2}, {"a"=>1, "@@!:2"=>3, "@@!:{\"@@!:3\":4}"=>5}]
43
+
44
+ def transform_hash(obj)
45
+ if obj.size == 1 && obj.keys.first =~ /\A@@!([a-z].*)/
46
+ cookie = $1
47
+ value = obj.values.first
48
+ case cookie
49
+ when "s"
50
+ CBOR::Simple.new(value)
51
+ when "b"
52
+ ::Base64.urlsafe_decode64(value)
53
+ when /\At(\d+)\z/
54
+ CBOR::Tagged.new($1.to_i, value)
55
+ else
56
+ fail ArgumentError.new("Unknown CBOR-JSON encoding @@!#{cookie}")
57
+ end
58
+ else
59
+ Hash[obj.map {|k, v| [ transform(
60
+ if k =~ /\A@@!:(.*)\z/
61
+ JSON.parse($1)
62
+ else
63
+ k
64
+ end), transform(v)]}]
65
+ end
66
+ end
67
+
68
+ end
@@ -0,0 +1,72 @@
1
+ class CBOR::Transform
2
+
3
+ def transform(obj)
4
+ case obj
5
+ when NilClass
6
+ transform_nil(obj)
7
+ when FalseClass, TrueClass
8
+ transform_bool(obj)
9
+ when CBOR::Simple
10
+ transform_simple(obj)
11
+ when Float
12
+ transform_float(obj)
13
+ when Integer
14
+ transform_integer(obj)
15
+ # XXX should probably handle Symbol
16
+ when String
17
+ case obj.encoding
18
+ when Encoding::BINARY
19
+ transform_bytes(obj)
20
+ else
21
+ transform_text(obj)
22
+ end
23
+ when Array
24
+ transform_array(obj)
25
+ when Hash
26
+ transform_hash(obj)
27
+ when CBOR::Tagged
28
+ transform_tag(obj)
29
+ end
30
+ end
31
+
32
+ def transform_nil(obj)
33
+ obj
34
+ end
35
+
36
+ def transform_bool(obj)
37
+ obj
38
+ end
39
+
40
+ def transform_simple(obj)
41
+ obj
42
+ end
43
+
44
+ def transform_float(obj)
45
+ obj
46
+ end
47
+
48
+ def transform_integer(obj)
49
+ obj
50
+ end
51
+
52
+ def transform_bytes(obj)
53
+ obj
54
+ end
55
+
56
+ def transform_text(obj)
57
+ obj
58
+ end
59
+
60
+ def transform_array(obj)
61
+ obj.map {|x| transform(x)}
62
+ end
63
+
64
+ def transform_hash(obj)
65
+ Hash[obj.map {|k, v| [transform(k), transform(v)]}]
66
+ end
67
+
68
+ def transform_tag(obj)
69
+ CBOR::Tagged.new(transform(obj.tag), transform(obj.value))
70
+ end
71
+
72
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cbor-diag
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.20
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carsten Bormann
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-04 00:00:00.000000000 Z
11
+ date: 2021-10-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -165,6 +165,8 @@ files:
165
165
  - lib/cbor-diagnostic.rb
166
166
  - lib/cbor-pretty.rb
167
167
  - lib/cbor-pure.rb
168
+ - lib/cbor-transform-j.rb
169
+ - lib/cbor-transform.rb
168
170
  - lib/half.rb
169
171
  homepage: http://cbor.io/
170
172
  licenses:
@@ -185,7 +187,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
185
187
  - !ruby/object:Gem::Version
186
188
  version: '0'
187
189
  requirements: []
188
- rubygems_version: 3.2.15
190
+ rubygems_version: 3.2.22
189
191
  signing_key:
190
192
  specification_version: 4
191
193
  summary: CBOR (Concise Binary Object Representation) diagnostic notation