Alias for Inner Objects
fenixpsicologia opened this issue · 1 comments
fenixpsicologia commented
Hey, what a cool and useful package! I'd like to ask if Serde would be capable of handling a very specific deserialization case. I work on a legacy application with a somewhat eccentric database, let's put it that way. In PDO queries, I usually bring all results as flattened arrays, and hydrating these data is a bit tedious because it's almost all done manually. My use case would be:
<?php
$data = [
[
"USER_ID" => 1,
"USER_NAME" => "John",
"ADDRESS_LINE" => "...",
"ADDRESS_POST_CODE" => "...",
]
];
class User
{
#[Serde\Field(serializedName: 'USER_ID')]
public int $id;
#[Serde\Field(serializedName: 'USER_NAME')]
public string $name;
#[Field(alias: 'ADDRESS_*')]
public Address $address;
}
class Address {
#[Serde\Field(serializedName: 'ADDRESS_LINE')]
public string $line;
#[Serde\Field(serializedName: 'ADDRESS_POST_CODE')]
public string $postcode;
}
Would the library be able to handle a case like this?
Crell commented
Have a look at the section on "flattening" in the README. You should be able to do almost exactly what you want just by changing #[Field(alias: ...)]
to #[Field(flatten: true)]
.