Tengo una matriz que almacena algunos datos como este y esta es la entrada exacta que estoy usando
{ "accepted" => { "number_of_order" => { "condition" => "between_number", "value" => "0&5" } }, "removed" => { } }Ahora estoy tratando de convertir esta matriz en un acceso de hash rubí como este y este es mi resultado esperado, no es lo que obtengo ahora
--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess accepted: !ruby/hash:ActiveSupport::HashWithIndifferentAccess number_of_order: !ruby/hash:ActiveSupport::HashWithIndifferentAccess condition: between_number value: 0&5 removed: !ruby/hash:ActiveSupport::HashWithIndifferentAccess {} Pero no puedo convertir la matriz en acceso hash ruby. También intento volcar YAML Yaml::dump($array); para convertir a YAML, luego ruby hash access pero nada funciona.
Básicamente, estoy tratando de hacer lo contrario de esta pregunta
¿Cómo obtener datos del acceso ruby hash a laravel 8?
Puede consultar el enlace anterior. Convierto este ruby hash en una matriz laravel, pero ahora también quería convertir esta matriz en ruby hash.
Mi código actual para convertir una matriz en un hash ruby
$jsonEncoded = '{"accepted":{"number_of_order":{"consition":"between_number","value":"0&5"}}}'; $array = json_decode($jsonEncoded, true); dump(Yaml::dump($array));Mi código actual para convertir ruby hash en matriz
$filter_data = str_replace("!ruby/hash:ActiveSupport::HashWithIndifferentAccess", "", $filter_data); $yamlContents = Yaml::parse($filter_data);Para convertir hash a yaml en ruby podemos usar la biblioteca yaml
require 'yaml' data = { "accepted" => { "number_of_order" => { "condition" => "between_number", "value" => "0&5" } }, "removed" => { } } puts data.to_yaml Para convertir una matriz asociada a yaml en php, podemos usar Symfony Yaml
use Symfony\Component\Yaml\Yaml; $jsonEncoded = '{"accepted":{"number_of_order":{"consition":"between_number","value":"0&5"}}}'; $array = json_decode($jsonEncoded, true); print Yaml::dump($array); Para cargar la salida php yaml en ruby, se puede usar la misma biblioteca yaml
require 'yaml' yaml_str = ... # output generate from Symfony Yaml::dump yaml_hash = YAML.load(yaml_str) Para cargar el yaml de salida de ruby en php, podemos usar el mismo Symfony Yaml
$yaml_str = ...; # output generate from ruby rhash.to_yaml $yaml_associated_array = Yaml.parse(yaml_str);Cargar hash ruby a php
$data = " --- !ruby/hash:ActiveSupport::HashWithIndifferentAccess accepted: !ruby/hash:ActiveSupport::HashWithIndifferentAccess number_of_order: !ruby/hash:ActiveSupport::HashWithIndifferentAccess condition: between_number value: 0&5 removed: !ruby/hash:ActiveSupport::HashWithIndifferentAccess {} "; print $data; $data = str_replace("---", "", preg_replace("/!ruby.*Access/", "", $data)); print $data; $result = Yaml::parse($data); var_dump($result);Lo mismo se puede lograr en ruby.
data = %Q( --- !ruby/hash:ActiveSupport::HashWithIndifferentAccess accepted: !ruby/hash:ActiveSupport::HashWithIndifferentAccess number_of_order: !ruby/hash:ActiveSupport::HashWithIndifferentAccess condition: between_number value: 0&5 removed: !ruby/hash:ActiveSupport::HashWithIndifferentAccess {} ) result = str.gsub(/!ruby.*Access/, '').gsub("--- ", "") puts result puts YAML.load(result)Nota: También podemos usar otras bibliotecas
Acabo de encontrar la respuesta a mi pregunta y la comparto con todos ustedes.
Código Laravel:
$jsonEncoded = '{"accepted":{"first_order_number":{"consition":"between_date","value":"2021-11-28 00:00:00&2021-11-22 00:00:00"}},"removed":{}}'; $process = new Process(['ruby', 'convert_hashActiveSupport.rb'],null,null, $jsonEncoded); $process->run(); // executes after the command finishes if (!$process->isSuccessful()) { throw new ProcessFailedException($process); } $data = $process->getOutput();Código rubí:
require 'yaml' require 'json' require "active_support/core_ext/hash/indifferent_access" yaml_str = "#{ ARGF.read }" parsed_output = JSON.parse(yaml_str) yaml_output = parsed_output.with_indifferent_access.to_yaml puts yaml_outputPaquete requerido en laravel:
composer require symfony/processPaquetes requeridos en ruby:
gem install activesupport gem install 'yaml'Y aquí obtengo mi salida esperada:
--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess accepted: !ruby/hash:ActiveSupport::HashWithIndifferentAccess first_order_date: !ruby/hash:ActiveSupport::HashWithIndifferentAccess consition: between_date value: 2021-11-28 00:00:00&2021-11-22 00:00:00 removed: !ruby/hash:ActiveSupport::HashWithIndifferentAccess {}