msgraph-sdk-php icon indicating copy to clipboard operation
msgraph-sdk-php copied to clipboard

JSON Encode models

Open inserve-paul opened this issue 5 months ago • 3 comments

Hi,

I've been implementing the v2.2 version of the msgraph-sdk this week; however I'm facing an issue. How can I encode the models returned from the client to JSON so it contains all values? It seems the actual values are in the BackingStore, but how can I make sure I get the actual data to be passed in a response?

For e.g. this works; but doesn't feel as the right solution:

json_encode($event->getBackingStore()->enumerate());

For reference, this is my code example below;

$requestConfig = new CalendarViewRequestBuilderGetRequestConfiguration(
    headers: [
        'Prefer' => 'outlook.timezone="W. Europe Standard Time"',
    ],
    queryParameters: CalendarViewRequestBuilderGetRequestConfiguration::createQueryParameters(
        endDateTime: $endDate,
        startDateTime: $startDate,
        top: 500
    )
);

$events = $this->graphClient
    ->users()
    ->byUserId($userId)
    ->calendar()
    ->calendarView()
    ->get($requestConfig)
    ->wait()
;

foreach ($events->getValue() as $event) {
        // I want to get the complete object as JSON values to be passed in a response
        json_encode($event->getBackingStore()->enumerate());
}

Thank you in advance

inserve-paul avatar Jan 19 '24 10:01 inserve-paul

Closed, as I'm using the SerializationWriter to get the result. However it feels a bit overcomplicated to serialize the models.

inserve-paul avatar Jan 22 '24 12:01 inserve-paul

Thanks for the feedback. Re-opening this to track possible improvements.

Ndiritu avatar Jan 22 '24 15:01 Ndiritu

@Ndiritu Currently I'm using the following approach to serialize the models, which is not ideal but ok for now.

Microsoft\Kiota\Abstractions\Serialization\Parsable $parsable;
 
$writer = $this
            ->graphClient
            ->getRequestAdapter()
            ->getSerializationWriterFactory()
            ->getSerializationWriter('application/json')
        ;

$parsable->serialize($writer);
$json = sprintf('{%s}', $writer->getSerializedContent()->getContents());

inserve-paul avatar Jan 24 '24 11:01 inserve-paul