dg / dibi

Dibi - smart database abstraction layer
https://dibiphp.com
Other
487 stars 136 forks source link

JSON is decoded into objects #282

Closed jiripudil closed 6 years ago

jiripudil commented 6 years ago

Description

The automatic decoding of JSON types forcibly creates a tree of objects. Unfortunately, we have code that accepts an array structure, so that instead of replacing this:

$decoded = json_decode($row['json_column'], true);

with this:

$decoded = $row['json_column'];

we are forced to have this:

$decoded = json_decode(json_encode($row['json_column']), true);

which isn't much of an improvement.

Is it something we have to learn to live with, or would you consider making it somehow configurable? What do you think?

A quick research showed that e.g. doctrine/dbal's JsonType does the opposite and decodes into an array.

milo commented 6 years ago

I would vote for "always as associative array", so json_decode(..., true). Otherway, one row may be decode as array, other as stdClass. IMHO type stability is better even object notation row->col->key->key looks better.

milo commented 6 years ago

Thanks @dg

jiripudil commented 6 years ago

Thank you for resolving the two issues I've reported. Our tests are now green with dibi/dibi@dev-master, I'll push it to staging later this week and let you know if there're any more problems. Thanks for your effort with 4.0.0, it looks very promising!

dg commented 6 years ago

Thanks for testing :)

paveljanda commented 5 years ago

Is there any chance to disable that automatic json decoding?

dg commented 5 years ago

$result->setType('column', Dibi\Type::TEXT);

paveljanda commented 5 years ago

Oh, thank you. Would you like a pull-request that would set this behaviour globally? (For example for big projects that are not able to react to this new feature quickly but would like to migration to more strict version of Dibi)

dg commented 5 years ago

Yes. Probably the best way is to go via something like ->setFormat(Type::JSON, $this->config['result']['json']) .... in Connection::createResultSet()

paveljanda commented 5 years ago

https://github.com/dg/dibi/pull/335/files