Skip to content

[HttpFoundation] Add named constructor on JsonResponse #19552

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

Merged
merged 1 commit into from
Aug 16, 2016
Merged
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
8 changes: 8 additions & 0 deletions src/Symfony/Component/HttpFoundation/JsonResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ public static function create($data = null, $status = 200, $headers = array())
return new static($data, $status, $headers);
}

/**
* Make easier the creation of JsonResponse from raw json.
*/
public static function fromJsonString($data = null, $status = 200, $headers = array())
Copy link
Contributor

@ro0NL ro0NL Aug 6, 2016

Choose a reason for hiding this comment

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

What about createFromStringcreateFromJsonString? or just createFromJson

Copy link
Contributor

@ro0NL ro0NL Aug 7, 2016

Choose a reason for hiding this comment

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

Btw createFromString somewhat implies a callback (JSONP) is parsed as well.. so you know ;-)

Edit: i guess we dont wanna go that way.

Copy link
Contributor Author

@tyx tyx Aug 8, 2016

Choose a reason for hiding this comment

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

No problem to switch to createFromJsonString. I'm used to remove the create prefix but if everyone prefer with it, I'm ok !

{
return new static($data, $status, $headers, true);
}

/**
* Sets the JSONP callback.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ public function testSetEncodingOptions()
$this->assertEquals('{"0":{"0":1,"1":2,"2":3}}', $response->getContent());
}

public function testItAcceptsJsonAsString()
{
$response = JsonResponse::fromJsonString('{"foo":"bar"}');
$this->assertSame('{"foo":"bar"}', $response->getContent());
}

/**
* @expectedException \InvalidArgumentException
*/
Expand Down