mirror of
https://github.com/cliffe/SecGen.git
synced 2026-02-20 13:50:45 +00:00
Fix: the utf-8 encoding broke arrays of hashes (e.g. organisation['employees'=>[{...},{...},{...}]]) - now working again.
This commit is contained in:
37
lib/objects/local_encoding_functions.rb
Normal file
37
lib/objects/local_encoding_functions.rb
Normal file
@@ -0,0 +1,37 @@
|
||||
class EncodingFunctions
|
||||
|
||||
# Wrapper around force_encoding for readability
|
||||
def self.string_to_utf8(value)
|
||||
value.force_encoding('UTF-8')
|
||||
end
|
||||
|
||||
# Recursively convert all hash values to UTF-8 encoding
|
||||
def self.hash_to_utf8(value)
|
||||
Hash[
|
||||
value.collect do |k, v|
|
||||
if v.respond_to?(:to_utf8)
|
||||
[k, v.to_utf8]
|
||||
elsif v.respond_to?(:force_encoding)
|
||||
[k, v.dup.force_encoding('UTF-8')]
|
||||
else
|
||||
[k, v]
|
||||
end
|
||||
end
|
||||
]
|
||||
end
|
||||
|
||||
# Recursively convert all array values to UTF-8 encoding
|
||||
def self.array_to_utf8(value)
|
||||
utf8 = []
|
||||
value.map {|element|
|
||||
if element.is_a? String
|
||||
utf8 << element.force_encoding('UTF-8')
|
||||
elsif element.is_a? Hash
|
||||
utf8 << EncodingFunctions::hash_to_utf8(value)
|
||||
elsif element.is_a? Array
|
||||
array_to_utf8(value)
|
||||
end
|
||||
}
|
||||
utf8
|
||||
end
|
||||
end
|
||||
@@ -1,5 +1,6 @@
|
||||
require 'getoptlong'
|
||||
require_relative '../helpers/constants'
|
||||
require_relative './local_encoding_functions.rb'
|
||||
require 'json'
|
||||
require 'base64'
|
||||
|
||||
@@ -140,15 +141,11 @@ class StringEncoder
|
||||
self.instance_variables.each do |iv|
|
||||
iv_value = self.instance_variable_get(iv)
|
||||
if iv_value.is_a? Array
|
||||
utf8 = []
|
||||
iv_value.map {|element|
|
||||
if element.is_a? String
|
||||
utf8 << element.force_encoding('UTF-8')
|
||||
end
|
||||
}
|
||||
self.instance_variable_set(iv, utf8)
|
||||
self.instance_variable_set(iv, EncodingFunctions::array_to_utf8(iv_value))
|
||||
elsif iv_value.is_a? Hash
|
||||
self.instance_variable_set(iv, EncodingFunctions::hash_to_utf8(iv_value))
|
||||
elsif iv_value.is_a? String
|
||||
self.instance_variable_set(iv, iv_value.force_encoding('UTF-8'))
|
||||
self.instance_variable_set(iv, EncodingFunctions::string_to_utf8(iv_value))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user