string-keys-to-int working.
parent
795acc1d33
commit
e87dcc3178
10
README.md
10
README.md
|
@ -25,3 +25,13 @@ json-to-cbor < file.json > file.cbor
|
||||||
hs < file.cbor
|
hs < file.cbor
|
||||||
```
|
```
|
||||||
|
|
||||||
|
# string-keys-to-int usage
|
||||||
|
|
||||||
|
JSON format has a limitation on the type of hash keys: it has to be a string.
|
||||||
|
CBOR doesn't come with such limitation.
|
||||||
|
`string-keys-to-int` automatically converts string keys into integers, whenever all the keys in a hash can be converted.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
string-keys-to-int < file.cbor
|
||||||
|
```
|
||||||
|
|
||||||
|
|
|
@ -1,30 +1,16 @@
|
||||||
require "cbor"
|
require "cbor"
|
||||||
require "option_parser"
|
require "option_parser"
|
||||||
|
|
||||||
#
|
|
||||||
# JSON format has a limitation on the type of hash keys: it has to be a string.
|
|
||||||
# CBOR doesn't come with such limitation.
|
|
||||||
# string-keys-to-int: automatically converts string keys into integers, whenever all
|
|
||||||
# the keys in a hash can be converted.
|
|
||||||
#
|
|
||||||
|
|
||||||
class Context
|
class Context
|
||||||
class_property do_nothing = false
|
class_property debug = false
|
||||||
class_property print_data = false
|
|
||||||
end
|
end
|
||||||
|
|
||||||
OptionParser.parse do |parser|
|
OptionParser.parse do |parser|
|
||||||
parser.banner = "usage: string-keys-to-int < file.cbor"
|
parser.banner = "usage: string-keys-to-int < file.cbor"
|
||||||
|
|
||||||
parser.on "-n", "--nothing",
|
parser.on "-d", "--debug",
|
||||||
"Do nothing, just print when keys would have been converted." do
|
"Debug: print keys to convert, input and output data." do
|
||||||
Context.do_nothing = true
|
Context.debug = true
|
||||||
end
|
|
||||||
|
|
||||||
parser.on "-p", "--print-data",
|
|
||||||
"Do nothing, print keys to convert, input and output data." do
|
|
||||||
Context.do_nothing = true
|
|
||||||
Context.print_data = true
|
|
||||||
end
|
end
|
||||||
|
|
||||||
parser.on "-h", "--help", "Displays this help and exits." do
|
parser.on "-h", "--help", "Displays this help and exits." do
|
||||||
|
@ -50,7 +36,7 @@ end
|
||||||
def change(data)
|
def change(data)
|
||||||
# 1. change keys
|
# 1. change keys
|
||||||
if are_hash_keys_all_int? data
|
if are_hash_keys_all_int? data
|
||||||
if Context.do_nothing
|
if Context.debug
|
||||||
puts "keys to convert: #{data.as(Hash).keys}"
|
puts "keys to convert: #{data.as(Hash).keys}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -84,19 +70,17 @@ until STDIN.read(buffer) == 0
|
||||||
while data = decoder.read_value
|
while data = decoder.read_value
|
||||||
break if data == 0
|
break if data == 0
|
||||||
|
|
||||||
if Context.print_data
|
if Context.debug
|
||||||
puts "input data:"
|
puts "input data:"
|
||||||
pp data
|
pp data
|
||||||
end
|
end
|
||||||
|
|
||||||
change data
|
change data
|
||||||
|
|
||||||
if Context.print_data
|
if Context.debug
|
||||||
puts "output data:"
|
puts "output data:"
|
||||||
pp data
|
pp data
|
||||||
end
|
else
|
||||||
|
|
||||||
unless Context.do_nothing
|
|
||||||
STDOUT.write data.to_cbor
|
STDOUT.write data.to_cbor
|
||||||
STDOUT.flush
|
STDOUT.flush
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue