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

Fix JSON float formatting #1635

Merged
merged 6 commits into from
May 28, 2019
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file based on the
### Bugfixes
* Always set the Guzzle `base_uri` to support connecting to multiple ES hosts. [#1618](https://github.com/ruflin/Elastica/pull/1618)
* Properly handle underscore prefixes in options and bulk request metadata ([cf upstream](https://github.com/elastic/elasticsearch/issues/26886). [#1621](https://github.com/ruflin/Elastica/pull/1621)
* Preserve zeros while doing float serialization to JSON. [#1635](https://github.com/ruflin/Elastica/pull/1635)

### Added

Expand Down
2 changes: 1 addition & 1 deletion lib/Elastica/Index/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public function setBlocksRead(bool $state = true): Response
/**
* @return bool
*/
public function getBlocksWrite(): Bool
public function getBlocksWrite(): bool
{
return $this->getBool('blocks.write');
}
Expand Down
3 changes: 3 additions & 0 deletions lib/Elastica/JSON.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public static function stringify($args/* inherit from json_encode */)
// extract arguments
$args = \func_get_args();

// set defaults
isset($args[1]) ? $args[1] |= JSON_PRESERVE_ZERO_FRACTION : $args[1] = JSON_PRESERVE_ZERO_FRACTION;
Copy link
Owner

Choose a reason for hiding this comment

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

Had to read this line a few times but makes sense now :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, kinda cryptic. I couldn't find easier way to append const if arg[1] is set/not set.

Copy link

Choose a reason for hiding this comment

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

$args[1] = ($args[1] ?? 0) | JSON_PRESERVE_ZERO_FRACTION;

Copy link
Owner

Choose a reason for hiding this comment

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

Thanks for chiming in @chx . Should we update the code?

Copy link

Choose a reason for hiding this comment

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

I think so :)

Copy link
Owner

Choose a reason for hiding this comment

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

@chx @pySilver Someone wants to take it? ;-)


// run encode and output
$string = \call_user_func_array('json_encode', $args);

Expand Down