### Summary

With Ansible v2.9 (on Python v3.6.9 provided by Ubuntu 18.04 LTS) and Jinja2 v2.11.3, the playbook (see below) works to cast a number into a string in the YAML output:

```shell
$ ansible-playbook convert.yml 

PLAY [localhost] ******************************************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************************************
ok: [localhost]

TASK [set_fact] *******************************************************************************************************************************************************************************
ok: [localhost]

TASK [debug] **********************************************************************************************************************************************************************************
ok: [localhost] => {
    "bar": {
        "x": "my_string",
        "y": 5,
        "z": "300"
    }
}

TASK [debug] **********************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "x: my_string\ny: 5\nz: '300'\n"
}

PLAY RECAP ************************************************************************************************************************************************************************************
localhost                  : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
```

Unfortunately, with an updated stack (ansible-core v2.12.3, Jinja2 v3.0.3, Python v3.8.0) I get a weird error `cannot represent an object`:

```shell
$ ansible-playbook convert.yml 
[WARNING]: Unable to parse /etc/dallmeier/environment as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [localhost] ******************************************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************************************
ok: [localhost]

TASK [set_fact] *******************************************************************************************************************************************************************************
ok: [localhost]

TASK [debug] **********************************************************************************************************************************************************************************
ok: [localhost] => {
    "bar": {
        "x": "my_string",
        "y": 5,
        "z": "300"
    }
}

TASK [debug] **********************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "to_nice_yaml - ('cannot represent an object', '300'). ('cannot represent an object', '300')"}

PLAY RECAP ************************************************************************************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
```

Jinja2 native is enabled in /etc/ansible/ansible.cfg:

```ini
[defaults]
jinja2_native = True
```

### Issue Type

Bug Report

### Component Name

to_nice_yaml

### Ansible Version

```console
$ ansible --version
ansible [core 2.12.3]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/au/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.8/dist-packages/ansible
  ansible collection location = /home/au/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible
  python version = 3.8.0 (default, Dec  9 2021, 17:53:27) [GCC 8.4.0]
  jinja version = 3.0.3
  libyaml = True
```


### Configuration

```console
$ ansible-config dump --only-changed
DEFAULT_ASK_VAULT_PASS(/etc/ansible/ansible.cfg) = False
DEFAULT_HOST_LIST(/etc/ansible/ansible.cfg) = ['/etc/foobar/environment']
DEFAULT_JINJA2_NATIVE(/etc/ansible/ansible.cfg) = True
```


### OS / Environment

Ubuntu Server 18.04.6 LTS

### Steps to Reproduce

<!--- Paste example playbooks or commands between quotes below -->
```yaml (paste below)
- hosts: localhost
  vars:
    foo: 300
  tasks:
    - set_fact:
        bar:
          x: "my_string"
          y: 5
          z: '{{ foo |string }}'
    - debug: var=bar
    - debug: msg='{{ bar |to_nice_yaml }}'
```


### Expected Results

`to_nice_yaml` displays the number as string

### Actual Results

```console
TASK [set_fact] *******************************************************************************************************************************************************************************
task path: /home/au/src/asa-branches/v0.3/ansible/convert.yml:5
ok: [localhost] => {
    "ansible_facts": {
        "bar": {
            "x": "my_string",
            "y": 5,
            "z": "300"
        }
    },
    "changed": false
}

TASK [debug] **********************************************************************************************************************************************************************************
task path: /home/au/src/asa-branches/v0.3/ansible/convert.yml:10
ok: [localhost] => {
    "bar": {
        "x": "my_string",
        "y": 5,
        "z": "300"
    }
}

TASK [debug] **********************************************************************************************************************************************************************************
task path: /home/au/src/asa-branches/v0.3/ansible/convert.yml:11
fatal: [localhost]: FAILED! => {
    "msg": "to_nice_yaml - ('cannot represent an object', '300'). ('cannot represent an object', '300')"
}
```


### Code of Conduct

- [X] I agree to follow the Ansible Code of Conduct
)
AnsibleDumper.add_representer(
    HostVarsVars,
    represent_hostvars,
)
AnsibleDumper.add_representer(
    VarsWithSources,
    represent_hostvars,
)
AnsibleDumper.add_representer(
    AnsibleSequence,
    yaml.representer.SafeRepresenter.represent_list,
)
AnsibleDumper.add_representer(
    AnsibleMapping,
    yaml.representer.SafeRepresenter.represent_dict,
)
AnsibleDumper.add_representer(
    AnsibleVaultEncryptedUnicode,
    represent_vault_encrypted_unicode,
)
AnsibleDumper.add_representer(
    AnsibleUndefined,
    represent_undefined,
)
AnsibleDumper.add_representer(
    NativeJinjaUnsafeText,
    represent_unicode,
)