Closed rabol closed 7 months ago
an even more simple example:
$jd = '{"id": 716429}';
Recipe::fromJsonData($jd);
class Recipe
{
use JsonSerialize;
#[Json('id')]
public int $internalId;
}
same error
crap.... fromJsonData() requires something different... my bad
Sorry too fast.
yes, I should use fromJsonString(), that solved the first issue, but then I added a DishType class to be used with:
"dishTypes": [
"lunch",
"main course",
"main dish",
"dinner"
],
class looks like this:
<?php
namespace App\Classes\Spoonacular;
use Square\Pjson\Json;
use Square\Pjson\JsonSerialize;
class DishType
{
use JsonSerialize;
#[Json]
public string $name;
}
main class looks like this:
<?php
namespace App\Classes\Spoonacular;
use Square\Pjson\Json;
use Square\Pjson\JsonSerialize;
use App\Classes\Spoonacular\DishType;
class Recipe
{
use JsonSerialize;
#[Json('id')]
public int $internalId;
#[Json('title')]
public string $title; //String
#[Json('image')]
public string $image; //String
#[Json('imageType')]
public string $imageType; //String
#[Json('servings')]
public int $servings; //int
#[Json('readyInMinutes')]
public int $readyInMinutes; //int
#[Json('license')]
public string $license; //String
#[Json('sourceName')]
public string $sourceName; //String
#[Json('sourceUrl')]
public string $sourceUrl; //String
#[Json('spoonacularSourceUrl')]
public string $spoonacularSourceUrl; //String
#[Json('healthScore')]
public string $healthScore; //int
#[Json('spoonacularScore')]
public string $spoonacularScore; //int
#[Json('pricePerServing')]
public float $pricePerServing; //double
#[Json('analyzedInstructions')]
public ?array $analyzedInstructions; //array( undefined )
#[Json('cheap')]
public bool $cheap; //boolean
#[Json('creditsText')]
public string $creditsText; //String
#[Json('cuisines')]
public ?array $cuisines; //array( undefined )
#[Json('dairyFree')]
public bool $dairyFree; //boolean
#[Json('diets')]
public ?array $diets; //array( undefined )
#[Json('gaps')]
public string $gaps; //String
#[Json('glutenFree')]
public bool $glutenFree; //boolean
#[Json('instructions')]
public string $instructions; //String
#[Json('ketogenic')]
public bool $ketogenic; //boolean
#[Json('lowFodmap')]
public bool $lowFodmap; //boolean
#[Json('occasions')]
public ?array $occasions; //array( undefined )
#[Json('sustainable')]
public bool $sustainable; //boolean
#[Json('vegan')]
public bool $vegan; //boolean
#[Json('vegetarian')]
public bool $vegetarian; //boolean
#[Json('veryHealthy')]
public bool $veryHealthy; //boolean
#[Json('veryPopular')]
public bool $veryPopular; //boolean
#[Json('whole30')]
public bool $whole30; //boolean
#[Json('weightWatcherSmartPoints')]
public int $weightWatcherSmartPoints; //int
#[Json(type: DishType::class)]
public array $dishTypes; //array( String )
#[Json('extendedIngredients')]
public array $extendedIngredients; //array( ExtendedIngredients )
#[Json('summary')]
public string $summary; //String
#[Json('winePairing')]
public $winePairing; //WinePairing
}
if I don't use the the DishType class, I get an array, which is kind of correct, but using the class I get the error.
If I understand correctly, in json
you have a string but would like it to map to a class instance when in PHP and not the scalar string
type.
In that case you need to provide the methods for how to read and write this class to its json type. Take a look at the BigInt class for an example. Also look at the Serialization and DeSerialization tests using that class.
Hi
I'm trying to deserialize the json from a api call but get this error:
the json that I try to use:
and my class
I'm sure that i'm missing something in my class, but based on the error messages is difficult to see what
and hint would be highly appriciated