Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alias for Inner Objects #44

Closed
fenixpsicologia opened this issue Dec 2, 2023 · 1 comment
Closed

Alias for Inner Objects #44

fenixpsicologia opened this issue Dec 2, 2023 · 1 comment
Labels
support Support requests that aren't bugs, mostly "how do I...?"

Comments

@fenixpsicologia
Copy link

fenixpsicologia commented Dec 2, 2023

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
Copy link
Owner

Crell commented Dec 3, 2023

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)].

@Crell Crell added the support Support requests that aren't bugs, mostly "how do I...?" label Dec 3, 2023
@Crell Crell closed this as completed Dec 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support Support requests that aren't bugs, mostly "how do I...?"
Projects
None yet
Development

No branches or pull requests

2 participants