lib/yq.rb in yq-0.1.1 vs lib/yq.rb in yq-0.2.0
- old
+ new
@@ -71,21 +71,30 @@
hash.to_yaml
end
def self.json_to_hash(json)
JSON.parse(json)
- rescue JSON::ParserError => e
+ rescue JSON::ParserError
LOGGER.debug "Non JSON output from jq. Interpreting."
interpret_non_json_output(json)
end
def self.hash_to_json(hash)
hash.to_json
end
def self.interpret_non_json_output(string)
- string.split("\n").map do |line|
- obj = JSON.parse(%Q[{ "value": #{line} }])
- obj["value"]
+ regex = /(^\{.+?^\}|^\[.+?\]|^".+?")/m
+ matches = string.scan(regex)
+
+ without_the_wrapping_array = matches.map(&:first)
+ without_the_wrapping_array.map do |line|
+ begin
+ JSON.parse(line)
+ rescue JSON::ParserError
+ LOGGER.debug "Assuming #{line} is a string."
+ obj = JSON.parse(%Q[{ "value": #{line} }])
+ obj["value"]
+ end
end
end
end