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

TASK: Unify JSON de/encoding behavior #5093

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,12 @@ final public function jsonSerialize(): array
return $this->coordinates;
}

/**
* @throws \JsonException
*/
final public function toJson(): string
{
return json_encode($this, JSON_THROW_ON_ERROR);
try {
return json_encode($this, JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
throw new \RuntimeException(sprintf('Failed to JSON-encode instance of %s: %s', self::class, $e->getMessage()), 1716575128, $e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,12 @@ public function jsonSerialize(): array
return $this->value;
}

/**
* @throws \JsonException
*/
public function toJson(): string
{
return json_encode($this, JSON_THROW_ON_ERROR);
try {
return json_encode($this, JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
throw new \RuntimeException(sprintf('Failed to JSON-encode instance of %s: %s', self::class, $e->getMessage()), 1716575135, $e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ public static function fromArray(array $data): self
*/
public static function fromJsonString(string $jsonString): self
{
return self::instance(json_decode($jsonString, true));
try {
return self::instance(json_decode($jsonString, true, 512, JSON_THROW_ON_ERROR));
} catch (\JsonException $e) {
throw new \InvalidArgumentException(sprintf('Failed to JSON-decode "%s" for %s instance: %s', $jsonString, self::class, $e->getMessage()), 1716574623, $e);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ public static function fromArray(array $array): self

public static function fromJsonString(string $jsonString): self
{
return new self(\json_decode($jsonString, true));
try {
return new self(\json_decode($jsonString, true, 512, JSON_THROW_ON_ERROR));
} catch (\JsonException $e) {
throw new \InvalidArgumentException(sprintf('Failed to JSON-decode "%s" for %s instance: %s', $jsonString, self::class, $e->getMessage()), 1716574647, $e);
}
}

/**
Expand Down Expand Up @@ -156,11 +160,12 @@ public function getIterator(): \Traversable
yield from $this->points;
}

/**
* @throws \JsonException
*/
public function toJson(): string
{
return json_encode($this, JSON_THROW_ON_ERROR);
try {
return json_encode($this, JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
throw new \RuntimeException(sprintf('Failed to JSON-encode instance of %s: %s', self::class, $e->getMessage()), 1716575140, $e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ public static function createWithoutDimensions(): self
*/
public static function fromJsonString(string $jsonString): self
{
return self::instance(json_decode($jsonString, true, 512, JSON_THROW_ON_ERROR));
try {
return self::instance(json_decode($jsonString, true, 512, JSON_THROW_ON_ERROR));
} catch (\JsonException $e) {
throw new \InvalidArgumentException(sprintf('Failed to JSON-decode "%s" for %s instance: %s', $jsonString, self::class, $e->getMessage()), 1716574660, $e);
}
}

public static function fromDimensionSpacePoint(DimensionSpacePoint $dimensionSpacePoint): self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ public static function fromArray(array $array): self

public static function fromJsonString(string $jsonString): self
{
return self::fromArray(json_decode($jsonString, true));
try {
return self::fromArray(json_decode($jsonString, true, 512, JSON_THROW_ON_ERROR));
} catch (\JsonException $e) {
throw new \InvalidArgumentException(sprintf('Failed to JSON-decode "%s" for %s instance: %s', $jsonString, self::class, $e->getMessage()), 1716574670, $e);
}
}

public function toDimensionSpacePointSet(): DimensionSpacePointSet
Expand Down Expand Up @@ -112,7 +116,11 @@ public function contains(OriginDimensionSpacePoint $point): bool

public function toJson(): string
{
return json_encode($this, JSON_THROW_ON_ERROR);
try {
return json_encode($this, JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
throw new \RuntimeException(sprintf('Failed to JSON-encode instance of %s: %s', self::class, $e->getMessage()), 1716575147, $e);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,13 @@ public static function fromArray(array $array): self
return new self($nodeAggregateIds);
}

/**
* @throws \JsonException
*/
public static function fromJsonString(string $jsonString): self
{
return self::fromArray(\json_decode($jsonString, true, 512, JSON_THROW_ON_ERROR));
try {
return self::fromArray(\json_decode($jsonString, true, 512, JSON_THROW_ON_ERROR));
} catch (\JsonException $e) {
throw new \InvalidArgumentException(sprintf('Failed to JSON-decode "%s" for %s instance: %s', $jsonString, self::class, $e->getMessage()), 1716574683, $e);
}
}

public function merge(self $other): self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ public static function fromArray(array $values): self
return new self($values);
}

/**
* @throws \JsonException
*/
public static function fromJsonString(string $jsonString): self
{
return self::fromArray(\json_decode($jsonString, true, 512, JSON_THROW_ON_ERROR));
try {
return self::fromArray(\json_decode($jsonString, true, 512, JSON_THROW_ON_ERROR));
} catch (\JsonException $e) {
throw new \InvalidArgumentException(sprintf('Failed to JSON-decode "%s" for %s instance: %s', $jsonString, self::class, $e->getMessage()), 1716574701, $e);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ public static function defaultFromNodeType(NodeType $nodeType, PropertyConverter

public static function fromJsonString(string $jsonString): self
{
return self::fromArray(\json_decode($jsonString, true));
try {
return self::fromArray(\json_decode($jsonString, true, 512, JSON_THROW_ON_ERROR));
} catch (\JsonException $e) {
throw new \InvalidArgumentException(sprintf('Failed to JSON-decode "%s" for %s instance: %s', $jsonString, self::class, $e->getMessage()), 1716574712, $e);
}
}

public function merge(self $other): self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,13 @@ public static function fromNodeAggregateIds(NodeAggregateIds $nodeAggregateIds):
));
}

/**
* @throws \JsonException
*/
public static function fromJsonString(string $jsonString): self
{
return self::fromArray(\json_decode($jsonString, true, 512, JSON_THROW_ON_ERROR));
try {
return self::fromArray(\json_decode($jsonString, true, 512, JSON_THROW_ON_ERROR));
} catch (\JsonException $e) {
throw new \InvalidArgumentException(sprintf('Failed to JSON-decode "%s" for %s instance: %s', $jsonString, self::class, $e->getMessage()), 1716574722, $e);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ public static function fromNodeAggregateIds(NodeAggregateIds $nodeAggregateIds):

public static function fromJsonString(string $jsonString): self
{
return self::fromArray(\json_decode($jsonString, true));
try {
return self::fromArray(\json_decode($jsonString, true, 512, JSON_THROW_ON_ERROR));
} catch (\JsonException $e) {
throw new \InvalidArgumentException(sprintf('Failed to JSON-decode "%s" for %s instance: %s', $jsonString, self::class, $e->getMessage()), 1716574730, $e);
}
}

public function merge(self $other): self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ public static function fromArray(array $array): self

public static function fromJsonString(string $jsonString): self
{
return self::fromArray(json_decode($jsonString, true));
try {
return self::fromArray(json_decode($jsonString, true, 512, JSON_THROW_ON_ERROR));
} catch (\JsonException $e) {
throw new \InvalidArgumentException(sprintf('Failed to JSON-decode "%s" for %s instance: %s', $jsonString, self::class, $e->getMessage()), 1716574740, $e);
}
}

public function getCoverage(OriginDimensionSpacePoint $originDimensionSpacePoint): ?DimensionSpacePointSet
Expand All @@ -86,6 +90,10 @@ public function jsonSerialize(): array

public function toJson(): string
{
return json_encode($this, JSON_THROW_ON_ERROR);
try {
return json_encode($this, JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
throw new \RuntimeException(sprintf('Failed to JSON-encode instance of %s: %s', self::class, $e->getMessage()), 1716575152, $e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ public static function fromArray(array $array): self

public static function fromJsonString(string $jsonString): self
{
return self::fromArray(json_decode($jsonString, true));
try {
return self::fromArray(json_decode($jsonString, true, 512, JSON_THROW_ON_ERROR));
} catch (\JsonException $e) {
throw new \InvalidArgumentException(sprintf('Failed to JSON-decode "%s" for %s instance: %s', $jsonString, self::class, $e->getMessage()), 1716574748, $e);
}
}

public function getOrigin(DimensionSpacePoint $coveredDimensionSpacePoint): ?OriginDimensionSpacePoint
Expand All @@ -94,6 +98,10 @@ public function jsonSerialize(): array

public function toJson(): string
{
return json_encode($this, JSON_THROW_ON_ERROR);
try {
return json_encode($this, JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
throw new \RuntimeException(sprintf('Failed to JSON-encode instance of %s: %s', self::class, $e->getMessage()), 1716575158, $e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ public static function fromArray(array $array): self

public static function fromJsonString(string $jsonString): self
{
return self::fromArray(\json_decode($jsonString, true, JSON_THROW_ON_ERROR));
try {
return self::fromArray(\json_decode($jsonString, true, JSON_THROW_ON_ERROR, JSON_THROW_ON_ERROR));
} catch (\JsonException $e) {
throw new \InvalidArgumentException(sprintf('Failed to JSON-decode "%s" for %s instance: %s', $jsonString, self::class, $e->getMessage()), 1716574757, $e);
}
}

public function withAggregateId(NodeAggregateId $aggregateId): self
Expand All @@ -95,7 +99,7 @@ public function toJson(): string
try {
return json_encode($this, JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
throw new \RuntimeException(sprintf('Failed to JSON-encode NodeAddress: %s', $e->getMessage()), 1715608338, $e);
throw new \RuntimeException(sprintf('Failed to JSON-encode instance of %s: %s', self::class, $e->getMessage()), 1715608338, $e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ public static function fromArray(array $array): self

public static function fromJsonString(string $jsonString): self
{
return self::fromArray(\json_decode($jsonString, true));
try {
return self::fromArray(\json_decode($jsonString, true, 512, JSON_THROW_ON_ERROR));
} catch (\JsonException $e) {
throw new \InvalidArgumentException(sprintf('Failed to JSON-decode "%s" for %s instance: %s', $jsonString, self::class, $e->getMessage()), 1716574767, $e);
}
}

public static function fromNodes(Nodes $nodes): self
Expand Down Expand Up @@ -98,7 +102,11 @@ public function jsonSerialize(): array

public function toJson(): string
{
return \json_encode($this, JSON_THROW_ON_ERROR);
try {
return \json_encode($this, JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
throw new \RuntimeException(sprintf('Failed to JSON-encode instance of %s: %s', self::class, $e->getMessage()), 1716575179, $e);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ public static function fromAsset(Asset $asset): self
);
}

public static function fromJson(string $json): self
public static function fromJsonString(string $jsonString): self
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah nice i see you adjusted it to be in sync with the other parts thanks :)
I for one am perfectly fine with toJson() and fromJsonString() ... we discussed this briefly already in #4868 (comment)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep. I'm still not very fond of it – not only because it is asynchronous but because JSON is a string by definition. But consistency > correctness in this case I'd say

{
try {
/** @var array{identifier: string, type: string, title: string, copyrightNotice: string, caption: string, assetSourceIdentifier: string, resource: array{filename: string, collectionName: string, mediaType: string, sha1: string}} $data */
$data = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
$data = json_decode($jsonString, true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
throw new \InvalidArgumentException(sprintf('Failed to decode JSON: %s', $e->getMessage()), 1646992457);
throw new \InvalidArgumentException(sprintf('Failed to JSON-decode "%s" for %s instance: %s', $jsonString, self::class, $e->getMessage()), 1646992457, $e);
}
return self::fromArray($data);
}
Expand Down Expand Up @@ -115,7 +115,7 @@ public function toJson(): string
try {
return json_encode($this, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT);
} catch (\JsonException $e) {
throw new \RuntimeException(sprintf('Failed to JSON encode asset "%s": %s', $this->identifier, $e->getMessage()), 1646314000);
throw new \RuntimeException(sprintf('Failed to JSON-encode instance of %s: %s', self::class, $e->getMessage()), 1646314000, $e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ public static function fromImageVariant(ImageVariant $imageVariant): self
);
}

public static function fromJson(string $json): self
public static function fromJsonString(string $jsonString): self
{
try {
/** @var array{identifier: string, originalAssetIdentifier: string, name: string, width: int, height: int, presetIdentifier: ?string, presetVariantName: ?string, imageAdjustments: array<array{type: string, properties: array<mixed>}>} $data */
$data = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
$data = json_decode($jsonString, true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
throw new \InvalidArgumentException(sprintf('Failed to decode JSON: %s', $e->getMessage()), 1646992457);
throw new \InvalidArgumentException(sprintf('Failed to JSON-decode "%s" for %s instance: %s', $jsonString, self::class, $e->getMessage()), 1646992457, $e);
}
return self::fromArray($data);
}
Expand Down Expand Up @@ -79,7 +79,7 @@ public function toJson(): string
try {
return json_encode($this, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT);
} catch (\JsonException $e) {
throw new \RuntimeException(sprintf('Failed to JSON encode asset "%s": %s', $this->identifier, $e->getMessage()), 1646314000);
throw new \RuntimeException(sprintf('Failed to JSON-encode instance of %s: %s', self::class, $e->getMessage()), 1646314000, $e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ public static function fromRawEvent(Event $event): self
);
}

public static function fromJson(string $json): self
public static function fromJsonString(string $jsonString): self
{
try {
/** @var array{identifier: string, type: string, payload: array<mixed>, metadata: array<mixed>} $data */
$data = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
$data = json_decode($jsonString, true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
throw new \InvalidArgumentException(sprintf('Failed to decode JSON "%s": %s', $json, $e->getMessage()), 1638432979, $e);
throw new \InvalidArgumentException(sprintf('Failed to JSON-decode "%s" for %s instance: %s', $jsonString, self::class, $e->getMessage()), 1716574888, $e);
}
return new self(
$data['identifier'],
Expand Down Expand Up @@ -76,7 +76,7 @@ public function toJson(): string
try {
return json_encode($this, JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
throw new \RuntimeException(sprintf('Failed to encode exported event to JSON: %s', $e->getMessage()), 1638432972, $e);
throw new \RuntimeException(sprintf('Failed to JSON-encode instance of %s: %s', self::class, $e->getMessage()), 1638432972, $e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function() use ($jsonl) {
if ($json === '') {
continue;
}
yield ExportedEvent::fromJson($json);
yield ExportedEvent::fromJsonString($json);
}
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function run(): ProcessorResult
private function importAsset(StorageAttributes $file): void
{
$fileContents = $this->files->read($file->path());
$serializedAsset = SerializedAsset::fromJson($fileContents);
$serializedAsset = SerializedAsset::fromJsonString($fileContents);
/** @var Asset|null $existingAsset */
$existingAsset = $this->assetRepository->findByIdentifier($serializedAsset->identifier);
if ($existingAsset !== null) {
Expand Down Expand Up @@ -123,7 +123,7 @@ private function importAsset(StorageAttributes $file): void
private function importImageVariant(StorageAttributes $file): void
{
$fileContents = $this->files->read($file->path());
$serializedImageVariant = SerializedImageVariant::fromJson($fileContents);
$serializedImageVariant = SerializedImageVariant::fromJsonString($fileContents);
$existingImageVariant = $this->assetRepository->findByIdentifier($serializedImageVariant->identifier);
assert($existingImageVariant === null || $existingImageVariant instanceof ImageVariant);
if ($existingImageVariant !== null) {
Expand Down
Loading
Loading