msgraph-sdk-php
msgraph-sdk-php copied to clipboard
JSON Encode models
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
Closed, as I'm using the SerializationWriter to get the result. However it feels a bit overcomplicated to serialize the models.
Thanks for the feedback. Re-opening this to track possible improvements.
@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());