From cc800a746e7f4323ea79221a4ecede6e06aba4f6 Mon Sep 17 00:00:00 2001 From: Robert Felty Date: Thu, 11 Jul 2024 15:32:55 +0200 Subject: [PATCH 1/5] Fixed parsing of json maps --- spyc.yaml | 2 ++ src/Spyc.php | 10 ++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/spyc.yaml b/spyc.yaml index 489f28c..ccfb0c8 100644 --- a/spyc.yaml +++ b/spyc.yaml @@ -149,6 +149,8 @@ morelesskey: "" array_of_zero: [0] sophisticated_array_of_zero: {rx: {tx: [0]} } +key_value_str_json_map: {"rx": {"tx": ["fx", "sx"]}, "ab": {"cd":"de"} } +key_value_str_json_list: [{"rx": {"tx": ["fx", "sx"]}}, {"ab": {"cd":"de"} }] switches: - { row: 0, col: 0, func: {tx: [0, 1]} } diff --git a/src/Spyc.php b/src/Spyc.php index fd2c38c..bfdddfe 100644 --- a/src/Spyc.php +++ b/src/Spyc.php @@ -1063,10 +1063,16 @@ private function returnKeyValuePair ($line) { $key = trim(array_shift($explode)); $value = trim(implode(': ', $explode)); $this->checkKeysInValue($value); + // Other methods currently don't parse valid json maps or arrays of maps correctly + if ( str_starts_with( $value, '[{' ) || str_starts_with( $value, '{"' ) ) { + $value = json_decode( preg_replace( '/^.*?:\s*/', '', $line ), true ); + } } // Set the type of the value. Int, string, etc - $value = $this->_toType($value); - if ($key === '0') $key = '__!YAMLZero'; + if ( ! is_array( $value ) ) { + $value = $this->_toType($value); + if ($key === '0') $key = '__!YAMLZero'; + } $array[$key] = $value; } else { $array = array ($line); From 70c74668e2937b98e3824b0d282d12a83cb685c7 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Sun, 2 Nov 2025 16:38:37 +0100 Subject: [PATCH 2/5] Fix indentation Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/Spyc.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Spyc.php b/src/Spyc.php index bfdddfe..0b96aa4 100644 --- a/src/Spyc.php +++ b/src/Spyc.php @@ -1063,7 +1063,7 @@ private function returnKeyValuePair ($line) { $key = trim(array_shift($explode)); $value = trim(implode(': ', $explode)); $this->checkKeysInValue($value); - // Other methods currently don't parse valid json maps or arrays of maps correctly + // Other methods currently don't parse valid json maps or arrays of maps correctly if ( str_starts_with( $value, '[{' ) || str_starts_with( $value, '{"' ) ) { $value = json_decode( preg_replace( '/^.*?:\s*/', '', $line ), true ); } From 6ce3c8d2a02ada4bdf340582b79a9d6b9731c482 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Sun, 2 Nov 2025 16:38:45 +0100 Subject: [PATCH 3/5] Fix indentation Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/Spyc.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Spyc.php b/src/Spyc.php index 0b96aa4..7676f85 100644 --- a/src/Spyc.php +++ b/src/Spyc.php @@ -1070,8 +1070,8 @@ private function returnKeyValuePair ($line) { } // Set the type of the value. Int, string, etc if ( ! is_array( $value ) ) { - $value = $this->_toType($value); - if ($key === '0') $key = '__!YAMLZero'; + $value = $this->_toType($value); + if ($key === '0') $key = '__!YAMLZero'; } $array[$key] = $value; } else { From d79b4552d57dd32867a978700c4ac00bd3ac3e29 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Sun, 2 Nov 2025 16:39:01 +0100 Subject: [PATCH 4/5] Remove space Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- spyc.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spyc.yaml b/spyc.yaml index ccfb0c8..252bf18 100644 --- a/spyc.yaml +++ b/spyc.yaml @@ -150,7 +150,7 @@ morelesskey: "" array_of_zero: [0] sophisticated_array_of_zero: {rx: {tx: [0]} } key_value_str_json_map: {"rx": {"tx": ["fx", "sx"]}, "ab": {"cd":"de"} } -key_value_str_json_list: [{"rx": {"tx": ["fx", "sx"]}}, {"ab": {"cd":"de"} }] +key_value_str_json_list: [{"rx": {"tx": ["fx", "sx"]}}, {"ab": {"cd":"de"}}] switches: - { row: 0, col: 0, func: {tx: [0, 1]} } From 47501728bc43049fb883f0ffcc24fe9fb16ab687 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Sun, 2 Nov 2025 16:39:23 +0100 Subject: [PATCH 5/5] Verify json decoding worked Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/Spyc.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Spyc.php b/src/Spyc.php index 7676f85..bc8a889 100644 --- a/src/Spyc.php +++ b/src/Spyc.php @@ -1065,7 +1065,12 @@ private function returnKeyValuePair ($line) { $this->checkKeysInValue($value); // Other methods currently don't parse valid json maps or arrays of maps correctly if ( str_starts_with( $value, '[{' ) || str_starts_with( $value, '{"' ) ) { - $value = json_decode( preg_replace( '/^.*?:\s*/', '', $line ), true ); + $decoded = json_decode( preg_replace( '/^.*?:\s*/', '', $line ), true ); + if ($decoded !== null || (json_last_error() === JSON_ERROR_NONE && $decoded === null)) { + // Accept null as valid JSON value, but only if no error occurred + $value = $decoded; + } + // Otherwise, leave $value as-is (fallback to original string) } } // Set the type of the value. Int, string, etc