diff --git a/spyc.yaml b/spyc.yaml index 489f28c..252bf18 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..bc8a889 100644 --- a/src/Spyc.php +++ b/src/Spyc.php @@ -1063,10 +1063,21 @@ 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, '{"' ) ) { + $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 - $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);