php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #79485 Minor version update changed how json_encode serialzies SimpleXMLElement
Submitted: 2020-04-17 11:55 UTC Modified: 2020-04-17 12:21 UTC
From: p dot szymkowski at lottestyle dot de Assigned:
Status: Closed Package: JSON related
PHP Version: 7.3.17 OS: Linux
Private report: No CVE-ID: None
 [2020-04-17 11:55 UTC] p dot szymkowski at lottestyle dot de
Description:
------------
Minor update 7.3.16 -> 7.3.17 changed json_encode() behaviour and it's not documented or mentioned in change log

Test script:
---------------
<?php

$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Element someAttribute="foo">Bar</Element>
</Response>
XML;

echo json_encode(simplexml_load_string($xml), true);

Expected result:
----------------
{"Element":"Bar"}

Actual result:
--------------
{"Element":{"@attributes":{"someAttribute":"foo"},"0":"Bar"}}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-04-17 12:08 UTC] cmb@php.net
That change is not specific to json_encode, but rather caused by
the more general fix for bug #61597.  We usually do not document
bug fixes.

Anyhow, I wonder why you would expect json_encode() to strip the
attribute of the element?
 [2020-04-17 12:21 UTC] p dot szymkowski at lottestyle dot de
-Status: Open +Status: Closed
 [2020-04-17 12:21 UTC] p dot szymkowski at lottestyle dot de
I see. I'm closing this bug report, I should not serialize SimpleXMLElements in first place
 [2020-04-23 18:37 UTC] andrew dot bent at insuremytrip dot com
This fix has broken a lot of our code that relies on the serialization operating the way that it previously did.  Anyone who is serializing simplexml now probably has broken code.

> Anyhow, I wonder why you would expect json_encode() to strip the
attribute of the element?

Because it's behaved like this for nearly a decade and there are now many implementations depend on it being "broken".  Can you reconsider re-opening this bug please?
 [2020-04-30 21:53 UTC] ttijhof at wikimedia dot org
This changed has caused observable changes in user-land code in a bug release, which seems like something that should not happen.

An isolated test case exists at https://3v4l.org/HtN9R.

This is not debugging output, these are run-time return values. And it doesn't even rely on reflection or something hacky like that.

This kind of breaking API change is something that should be reserved for major releases. PHP 8.0 territory, not PHP 7.3.17.
 [2020-05-01 09:33 UTC] p dot szymkowski at lottestyle dot de
Guys I think you have to create new bug report
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 10:01:28 2024 UTC