From 498bc87e9fa664396ca6790adb5ff0dab4c5ef17 Mon Sep 17 00:00:00 2001 From: ts Date: Mon, 14 Jan 2019 16:54:17 +0000 Subject: [PATCH] Fix: the utf-8 encoding broke arrays of hashes (e.g. organisation['employees'=>[{...},{...},{...}]]) - now working again. --- lib/objects/local_encoding_functions.rb | 37 +++++++++++++++++++++++++ lib/objects/local_string_encoder.rb | 13 ++++----- 2 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 lib/objects/local_encoding_functions.rb diff --git a/lib/objects/local_encoding_functions.rb b/lib/objects/local_encoding_functions.rb new file mode 100644 index 000000000..bb1cef88c --- /dev/null +++ b/lib/objects/local_encoding_functions.rb @@ -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 \ No newline at end of file diff --git a/lib/objects/local_string_encoder.rb b/lib/objects/local_string_encoder.rb index 14c7c2478..c8b2662c1 100644 --- a/lib/objects/local_string_encoder.rb +++ b/lib/objects/local_string_encoder.rb @@ -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