Skip to content
7 changes: 7 additions & 0 deletions src/Illuminate/Collections/Enumerable.php
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,13 @@ public function jsonSerialize(): mixed;
*/
public function toJson($options = 0);

/**
* Get the collection of items as pretty print formatted JSON.
*
* @return string
*/
public function toPrettyJson();

/**
* Get a CachingIterator instance.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Illuminate/Collections/Traits/EnumeratesValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,16 @@ public function toJson($options = 0)
return json_encode($this->jsonSerialize(), $options);
}

/**
* Get the collection of items as pretty print formatted JSON.
*
* @return string
*/
public function toPrettyJson()
{
return $this->toJson(JSON_PRETTY_PRINT);
}

/**
* Get a CachingIterator instance.
*
Expand Down
12 changes: 12 additions & 0 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1800,6 +1800,18 @@ public function toJson($options = 0)
return $json;
}

/**
* Convert the model instance to pretty print formatted JSON.
*
* @return string
*
* @throws \Illuminate\Database\Eloquent\JsonEncodingException
*/
public function toPrettyJson()
{
return $this->toJson(JSON_PRETTY_PRINT);
}

/**
* Convert the object into something JSON serializable.
*
Expand Down
14 changes: 13 additions & 1 deletion src/Illuminate/Http/Resources/Json/JsonResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function toArray(Request $request)
}

/**
* Convert the model instance to JSON.
* Convert the resource to JSON.
*
* @param int $options
* @return string
Expand All @@ -153,6 +153,18 @@ public function toJson($options = 0)
return $json;
}

/**
* Convert the resource to pretty print formatted JSON.
*
* @return string
*
* @throws \Illuminate\Database\Eloquent\JsonEncodingException
*/
public function toPrettyJson()
{
return $this->toJson(JSON_PRETTY_PRINT);
}

/**
* Get any additional data that should be returned with the resource array.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Illuminate/Pagination/CursorPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,14 @@ public function toJson($options = 0)
{
return json_encode($this->jsonSerialize(), $options);
}

/**
* Convert the object to pretty print formatted JSON.
*
* @return string
*/
public function toPrettyJson()
{
return $this->toJson(JSON_PRETTY_PRINT);
}
}
10 changes: 10 additions & 0 deletions src/Illuminate/Pagination/LengthAwarePaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,14 @@ public function toJson($options = 0)
{
return json_encode($this->jsonSerialize(), $options);
}

/**
* Convert the object to pretty print formatted JSON.
*
* @return string
*/
public function toPrettyJson()
{
return $this->toJson(JSON_PRETTY_PRINT);
}
}
10 changes: 10 additions & 0 deletions src/Illuminate/Pagination/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,14 @@ public function toJson($options = 0)
{
return json_encode($this->jsonSerialize(), $options);
}

/**
* Convert the object to pretty print formatted JSON.
*
* @return string
*/
public function toPrettyJson()
{
return $this->toJson(JSON_PRETTY_PRINT);
}
}
10 changes: 10 additions & 0 deletions src/Illuminate/Support/Fluent.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,16 @@ public function toJson($options = 0)
return json_encode($this->jsonSerialize(), $options);
}

/**
* Convert the fluent instance to pretty print formatted JSON.
*
* @return string
*/
public function toPrettyJson()
{
return $this->toJson(JSON_PRETTY_PRINT);
}

/**
* Determine if the fluent instance is empty.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Illuminate/Support/MessageBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,16 @@ public function toJson($options = 0)
return json_encode($this->jsonSerialize(), $options);
}

/**
* Convert the object to pretty print formatted JSON.
*
* @return string
*/
public function toPrettyJson()
{
return $this->toJson(JSON_PRETTY_PRINT);
}

/**
* Convert the message bag to its string representation.
*
Expand Down
12 changes: 12 additions & 0 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3414,6 +3414,18 @@ public function testModelToJsonSucceedsWithPriorErrors(): void
$this->assertSame('{"name":"Mateus"}', $user->toJson(JSON_THROW_ON_ERROR));
}

public function testModelToPrettyJson(): void
{
$user = new EloquentModelStub(['name' => 'Mateus', 'active' => true]);
$results = $user->toPrettyJson();
$expected = $user->toJson(JSON_PRETTY_PRINT);

$this->assertJsonStringEqualsJsonString($expected, $results);
$this->assertSame($expected, $results);
$this->assertStringContainsString("\n", $results);
$this->assertStringContainsString(' ', $results);
}

public function testFillableWithMutators()
{
$model = new EloquentModelWithMutators;
Expand Down
21 changes: 20 additions & 1 deletion tests/Http/JsonResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function testJsonResourceToJsonSucceedsWithPriorErrors(): void

$resource = m::mock(JsonResource::class, ['resource' => $model])
->makePartial()
->shouldReceive('jsonSerialize')->once()->andReturn(['foo' => 'bar'])
->shouldReceive('jsonSerialize')->andReturn(['foo' => 'bar'])
->getMock();

// Simulate a JSON error
Expand All @@ -46,4 +46,23 @@ public function testJsonResourceToJsonSucceedsWithPriorErrors(): void

$this->assertSame('{"foo":"bar"}', $resource->toJson(JSON_THROW_ON_ERROR));
}

public function testJsonResourceToPrettyPrint(): void
{
$model = new class extends Model {
};

$resource = m::mock(JsonResource::class, ['resource' => $model])
->makePartial()
->shouldReceive('jsonSerialize')->andReturn(['foo' => 'bar', 'bar' => 'foo'])
->getMock();

$results = $resource->toPrettyJson();
$expected = $resource->toJson(JSON_PRETTY_PRINT);

$this->assertJsonStringEqualsJsonString($expected, $results);
$this->assertSame($expected, $results);
$this->assertStringContainsString("\n", $results);
$this->assertStringContainsString(' ', $results);
}
}
22 changes: 22 additions & 0 deletions tests/Pagination/CursorPaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,28 @@ public function testReturnEmptyCursorWhenItemsAreEmpty()
], $p->toArray());
}

public function testCursorPaginatorToJson()
{
$paginator = new CursorPaginator([['id' => 1], ['id' => 2], ['id' => 3], ['id' => 4]], 2, null);
$results = $paginator->toJson();
$expected = json_encode($paginator->toArray());

$this->assertJsonStringEqualsJsonString($expected, $results);
$this->assertSame($expected, $results);
}

public function testCursorPaginatorToPrettyJson()
{
$paginator = new CursorPaginator([['id' => 1], ['id' => 2], ['id' => 3], ['id' => 4]], 2, null);
$results = $paginator->toPrettyJson();
$expected = $paginator->toJson(JSON_PRETTY_PRINT);

$this->assertJsonStringEqualsJsonString($expected, $results);
$this->assertSame($expected, $results);
$this->assertStringContainsString("\n", $results);
$this->assertStringContainsString(' ', $results);
}

protected function getCursor($params, $isNext = true)
{
return (new Cursor($params, $isNext))->encode();
Expand Down
22 changes: 22 additions & 0 deletions tests/Pagination/PaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,26 @@ public function testCanTransformPaginatorItems()
$this->assertInstanceOf(Paginator::class, $p);
$this->assertSame(['1', '2', '3'], $p->items());
}

public function testPaginatorToJson()
{
$p = new Paginator(['item1', 'item2', 'item3'], 3, 1);
$results = $p->toJson();
$expected = json_encode($p->toArray());

$this->assertJsonStringEqualsJsonString($expected, $results);
$this->assertSame($expected, $results);
}

public function testPaginatorToPrettyJson()
{
$p = new Paginator(['item1', 'item2', 'item3'], 3, 1);
$results = $p->toPrettyJson();
$expected = $p->toJson(JSON_PRETTY_PRINT);

$this->assertJsonStringEqualsJsonString($expected, $results);
$this->assertSame($expected, $results);
$this->assertStringContainsString("\n", $results);
$this->assertStringContainsString(' ', $results);
}
}
13 changes: 13 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,19 @@ public function testToJsonEncodesTheJsonSerializeResult($collection)
$this->assertJsonStringEqualsJsonString(json_encode(['foo']), $results);
}

#[DataProvider('collectionClassProvider')]
public function testToPrettyJsonEncodesTheJsonSerializeResult($collection)
{
$c = $this->getMockBuilder($collection)->onlyMethods(['jsonSerialize'])->getMock();
$c->expects($this->once())->method('jsonSerialize')->willReturn(['foo' => 'bar', 'baz' => 'qux']);
$results = $c->toPrettyJson();
$expected = json_encode(['foo' => 'bar', 'baz' => 'qux'], JSON_PRETTY_PRINT);
$this->assertJsonStringEqualsJsonString($expected, $results);
$this->assertSame($expected, $results);
$this->assertStringContainsString("\n", $results);
$this->assertStringContainsString(' ', $results);
}

#[DataProvider('collectionClassProvider')]
public function testCastingToStringJsonEncodesTheToArrayResult($collection)
{
Expand Down
13 changes: 13 additions & 0 deletions tests/Support/SupportFluentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,19 @@ public function testToJsonEncodesTheToArrayResult()
$this->assertJsonStringEqualsJsonString(json_encode(['foo']), $results);
}

public function testToPrettyJson()
{
$fluent = $this->getMockBuilder(Fluent::class)->onlyMethods(['toArray'])->getMock();
$fluent->expects($this->exactly(2))->method('toArray')->willReturn(['foo' => 'bar', 'bar' => 'foo']);
$results = $fluent->toPrettyJson();
$expected = $fluent->toJson(JSON_PRETTY_PRINT);

$this->assertJsonStringEqualsJsonString($expected, $results);
$this->assertSame($expected, $results);
$this->assertStringContainsString("\n", $results);
$this->assertStringContainsString(' ', $results);
}

public function testScope()
{
$fluent = new Fluent(['user' => ['name' => 'taylor']]);
Expand Down
15 changes: 15 additions & 0 deletions tests/Support/SupportMessageBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,21 @@ public function testMessageBagReturnsExpectedJson()
$this->assertSame('{"foo":["bar"],"boom":["baz"]}', $container->toJson());
}

public function testMessageBagReturnsExpectedPrettyJson()
{
$container = new MessageBag;
$container->setFormat(':message');
$container->add('foo', 'bar');
$container->add('boom', 'baz');
$results = $container->toPrettyJson();
$expected = $container->toJson(JSON_PRETTY_PRINT);

$this->assertJsonStringEqualsJsonString($expected, $results);
$this->assertSame($expected, $results);
$this->assertStringContainsString("\n", $results);
$this->assertStringContainsString(' ', $results);
}

public function testCountReturnsCorrectValue()
{
$container = new MessageBag;
Expand Down