From 5035e53e803b75d6c2c66a0c65f9bcdb6581cd39 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Tue, 2 Feb 2016 11:22:41 -0500 Subject: [PATCH] Revert "load now does not modify the incomming data" This reverts commit 2126e9fee1ed1d9f9909d34d0c4cbd2ba6fdd1bc. we judge this to be too big a change for stable, keeping in devel we may still add part or all back to stable in the future. --- lib/ansible/parsing/dataloader.py | 41 ++++++++++++++++++------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/lib/ansible/parsing/dataloader.py b/lib/ansible/parsing/dataloader.py index b295560c931..c54ba78f1fe 100644 --- a/lib/ansible/parsing/dataloader.py +++ b/lib/ansible/parsing/dataloader.py @@ -20,6 +20,7 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type import copy +import json import os import stat import subprocess @@ -73,24 +74,30 @@ class DataLoader(): a JSON or YAML string. ''' - # YAML parser will take JSON as it is a subset. - if isinstance(data, AnsibleUnicode): - # The PyYAML's libyaml bindings use PyUnicode_CheckExact so - # they are unable to cope with our subclass. - # Unwrap and re-wrap the unicode so we can keep track of line - # numbers - in_data = text_type(data) - else: - in_data = data try: - new_data = self._safe_load(in_data, file_name=file_name) - except YAMLError as yaml_exc: - self._handle_error(yaml_exc, file_name, show_content) - - if isinstance(data, AnsibleUnicode): - new_data = AnsibleUnicode(new_data) - new_data.ansible_pos = data.ansible_pos - return new_data + # we first try to load this data as JSON + return json.loads(data) + except: + # if loading JSON failed for any reason, we go ahead + # and try to parse it as YAML instead + + if isinstance(data, AnsibleUnicode): + # The PyYAML's libyaml bindings use PyUnicode_CheckExact so + # they are unable to cope with our subclass. + # Unwrap and re-wrap the unicode so we can keep track of line + # numbers + new_data = text_type(data) + else: + new_data = data + try: + new_data = self._safe_load(new_data, file_name=file_name) + except YAMLError as yaml_exc: + self._handle_error(yaml_exc, file_name, show_content) + + if isinstance(data, AnsibleUnicode): + new_data = AnsibleUnicode(new_data) + new_data.ansible_pos = data.ansible_pos + return new_data def load_from_file(self, file_name): ''' Loads data from a file, which can contain either JSON or YAML. '''