Open mabar opened 1 year ago
jms/serializer has Xml* attributes for that - see https://jmsyst.com/libs/serializer/master/reference/annotations
use JMS\Serializer\Annotation\XmlAttribute;
use JMS\Serializer\Annotation\XmlValue;
use JMS\Serializer\Annotation\XmlRoot;
/** @XmlRoot("price") */
class Price
{
/** @XmlAttribute */
private $currency = 'EUR';
/** @XmlValue */
private $amount = 1.23;
}
XmlValue should probably be default
symfony/serializer does something similar too, but the exact format should be mainly declared by the object and not in global config https://symfony.com/doc/current/components/serializer.html#the-xmlencoder
For now I removed support for stdClass from processor. Proper support for various formats will be implemented later https://github.com/orisai/object-mapper/commit/5edbc1adbf93cd1794cea90c1f2b2a61ae929ab0
TIL that any stdClass
in $data
will cause validation to fail. But it will not tell you where it failed.
Most like this to blame: https://github.com/orisai/object-mapper/blob/e974721d8c8d6008ae811c107bd8e7db942fe2fa/src/Processing/DefaultProcessor.php#L220
I might add a test case for this and we might also want to add error to type when this happens. https://github.com/orisai/object-mapper/blob/e974721d8c8d6008ae811c107bd8e7db942fe2fa/src/Processing/DefaultProcessor.php#L224
TIL that any stdClass in $data will cause validation to fail. But it will not tell you where it failed.
@mrceperka It will be probably handled again with proper data types support, but in the meanwhile I think we could add some internal debug info. Also I am planning to add invalid value sampling to the printers, but object support will be quite limited, to prevent exposing implementation details via APIs (probably will just say it is an object, no matter of which type)
As mentioned in https://github.com/orisai/object-mapper/issues/73 data types support may include rules for mapping specific data types (JsonRule, XmlRule) to allow decoding inner jsons and perhaps even mapping them.
For both data type rules and manual decoding in callbacks there should be a way to mark the data type in Type interface so printer could properly disclose that the property is encoded as json and not a part of the outer structure.
Description
Currently if processor is given an output from
json_decode($value)
, anstdClass
is casted toarray
in case it's in place of aMappedObjectValue
. ButstdClass
makes sense to handle also in case of anArrayOf
and aListOf
.Handling should be also available in
Value
in case printer decides to print an invalid value.We should also consider that fix on user side is as simple as
json_decode($value, true)
and that it may work only for some formats:stdClass
and adds an extra properties.SimpleXMLElement
and we have no way of casting it to an array, handling attributes or printing errors while maintaining the original xml structureAddition information
No response
Related issues
No response