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

DDC-3863 add a json type that doesn't have the flaws of json_array #892

Merged
merged 3 commits into from
Jun 15, 2016

Conversation

Taluu
Copy link
Contributor

@Taluu Taluu commented Aug 4, 2015

As discussed in #891, the JsonArrayType was deprecated in favor of a new JsonType. :}

@doctrinebot
Copy link

Hello,

thank you for creating this pull request. I have automatically opened an issue
on our Jira Bug Tracker for you. See the issue link:

http://www.doctrine-project.org/jira/browse/DBAL-1277

We use Jira to track the state of pull requests and the versions they got
included in.

}

Copy link
Member

Choose a reason for hiding this comment

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

the extra empty line should be removed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@Taluu Taluu force-pushed the json-unflawed branch 4 times, most recently from 49945bb to 54f73b5 Compare August 5, 2015 15:06
return null;
}

return json_encode($value);
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we throw an exception here, too if encoding fails?

Copy link
Member

Choose a reason for hiding this comment

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

Should throw some variant of ConversionException with the return value of json_last_error().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should I do it too for the convertToPHPValue ?

Copy link
Member

Choose a reason for hiding this comment

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

No - the logic there is fine - ConversionException is missing a static constructor for this PHP->database scenario - there should probably be a ConversionException::serializationFailed method or something like that.

Copy link
Member

Choose a reason for hiding this comment

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

@Taluu the PR seems fine so far but still throwing a ConversionException if json_encode() fails is missing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, I was on it, and I just updated it (and rebased it with the master branch)

{
if (null === $value) {
return null;
}
Copy link
Member

Choose a reason for hiding this comment

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

should this actually be the case ? shouldn't it be converted to a json null instead ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not quite sure if it should be encoded too, if the field can be nullable ? Otherwise, it gives a "null" string when encoded in json...

Copy link
Member

Choose a reason for hiding this comment

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

@stof @Taluu Kind of debatable -

php > var_dump(json_decode('null') === json_decode(null));
bool(true)

If the platform has native json support (like postgresql) it will be stored as null. If the platform doesn't have native json support, then storing it as a SQL null will achieve much the same result, as deserializing the value to PHP value will be the same whether it's 'null' or NULL

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe, but on json_encode, it differs ;

Psy Shell v0.6.1 (PHP 7.0.2 — cli) by Justin Hileman
>>> json_encode('null');
=> ""null""
>>> json_encode(null);
=> "null"

And then on a json_decode call :

Psy Shell v0.6.1 (PHP 7.0.2 — cli) by Justin Hileman
>>> json_decode(null);
=> null
>>> json_decode('null');
=> null
>>> json_decode('"null"');
=> "null"

So I think returning a null value when the value is actually null should be more appropriate (Sorry for the late answer...)

Copy link
Member

Choose a reason for hiding this comment

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

I tend to prefer SQL NULL whenever possible as it seems more convenient than having a JSON null string stored (might also be more efficient storage wise...). Also it avoids unnecessary json_decode() calls for converting back to PHP which might improve performance. If nobody disagrees, I'll accept the current implementation.

Copy link
Member

Choose a reason for hiding this comment

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

What is also coming to my mind is database queries against JSON columns where you want to filter "empty" (NULL) values which would not be possible via IS NULL if we would put JSON NULL into DB. So returning null here is the better approach IMO.

@Taluu
Copy link
Contributor Author

Taluu commented Jan 25, 2016

The PR was updated with the latest comment and remarks. cc @deeky666 @zeroedin-bill @stof (sorry for the delay)

// extracted from symfony's php 5.5 polyfill
// @link https://github.com/symfony/polyfill-php55/blob/master/Php55.php
// @link http://nl1.php.net/manual/en/function.json-last-error-msg.php
private static function json_last_error_msg()
Copy link
Member

Choose a reason for hiding this comment

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

Why static? This method is not used in static context so we should make it non-static. Also please don't use underscore naming. I'd propose something like getLastErrorMessage().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed. I kind of copied the polyfill, and static seemed ok to be used here as it does not depend on any context. But I removed the static, as it doesn't really matter

@@ -99,4 +99,16 @@ static public function conversionFailedInvalidType($value, $toType, array $possi
implode(', ', $possibleTypes)
));
}

static public function conversionFailedSerialization($value, $format, $error, \Exception $previous = null)
Copy link
Member

Choose a reason for hiding this comment

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

Not sure if we really need to introduce another method here. Maybe it would suffice the purpose by sticking to conversionFailedFormat()

Copy link
Member

Choose a reason for hiding this comment

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

/cc @Ocramius @zeroedin-bill

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is also what I thought but as @zeroedin-bill suggested it, I went with it

Copy link
Member

Choose a reason for hiding this comment

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

@Taluu yeah sorry then for the extra effort. Could you be so kind to just use conversionFailedFormat() then? Also you might just put the error message into $expectedFormat. Like:

<?php

// snippet

if (JSON_ERROR_NONE !== json_last_error()) {
    throw ConversionException::conversionFailedFormat(
        $value,
        Type::JSON,
        'json (' . $this->getLastErrorMessage() . ')'
    );
}

Copy link
Member

Choose a reason for hiding this comment

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

@Taluu ah no wait.

Copy link
Member

Choose a reason for hiding this comment

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

The exception message says Could not convert database value to Doctrine type which would be wrong then.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm leaving this as is then, unless something better can be found ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not quite sure the message format is good too though

@Taluu
Copy link
Contributor Author

Taluu commented Feb 29, 2016

Ping. Any news around here ?

Thanks

@Taluu
Copy link
Contributor Author

Taluu commented Jun 8, 2016

Sorry to stir it up again, but any news around here ? /cc @deeky666 @zeroedin-bill @Ocramius

@deeky666
Copy link
Member

deeky666 commented Jun 8, 2016

LGTM. /cc @zeroedin-bill @Ocramius merge?

@deeky666 deeky666 added this to the 2.6 milestone Jun 8, 2016
@Ocramius
Copy link
Member

LGTM. @zeroedin-bill, can you check it and press the big green scary button? :-)

@billschaller
Copy link
Member

Driving all day, sry
On Jun 15, 2016 11:20 AM, "Marco Pivetta" notifications@github.com wrote:

Assigned #892 #892 to
@zeroedin-bill https://github.com/zeroedin-bill.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#892 (comment), or mute the
thread
https://github.com/notifications/unsubscribe/AEEbYQhpqLjs-55SirJX1yxK0RGK4ykOks5qMBhUgaJpZM4FlPPC
.

@Ocramius
Copy link
Member

Assigned it to you - whenever you got time :-)

Doesn't need merging back into 2.5, so it's as quick as the UI interaction,
after review.

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/

On 15 June 2016 at 17:21, Bill Schaller notifications@github.com wrote:

Driving all day, sry
On Jun 15, 2016 11:20 AM, "Marco Pivetta" notifications@github.com
wrote:

Assigned #892 #892 to
@zeroedin-bill https://github.com/zeroedin-bill.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#892 (comment), or mute the
thread
<
https://github.com/notifications/unsubscribe/AEEbYQhpqLjs-55SirJX1yxK0RGK4ykOks5qMBhUgaJpZM4FlPPC

.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#892 (comment), or mute
the thread
https://github.com/notifications/unsubscribe/AAJakIvj1Jhu4FA1YEIthqBmUf-EHKr6ks5qMBiPgaJpZM4FlPPC
.

@guilhermeblanco guilhermeblanco self-assigned this Jun 15, 2016
@guilhermeblanco guilhermeblanco merged commit 7d01ce7 into doctrine:master Jun 15, 2016
@guilhermeblanco
Copy link
Member

guilhermeblanco commented Jun 15, 2016

"Even though I walk through the valley of the shadow of death, I fear no evil"

Merged... =)

@Taluu Taluu deleted the json-unflawed branch June 15, 2016 15:31
@Ocramius Ocramius changed the title Json unflawed Type (DDC-3863) DDC-3863 add a json type that doesn't have the flaws of json_array Jul 22, 2017
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants