Skip to content

Commit

Permalink
Add JsonContent (#355)
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski authored Dec 15, 2023
1 parent f1d39e7 commit caf8148
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/BufferedContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@ public static function fromString(string $content, ?string $contentType = null):
return new self($content, $contentType);
}

/**
* Creates an instance using the given JSON serializable data with the content-type application/json.
*
* @param mixed $json Data which may be JSON serialized with {@see json_encode()}.
*/
public static function fromJson(mixed $json): self
{
try {
return self::fromString(\json_encode($json, flags: \JSON_THROW_ON_ERROR), 'application/json');
} catch (\JsonException $exception) {
throw new HttpException(
'Exception thrown encoding JSON content: ' . $exception->getMessage(),
previous: $exception,
);
}
}

public static function fromFile(
string $path,
?string $contentType = null,
Expand Down

0 comments on commit caf8148

Please sign in to comment.