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

Entity castAsJson returns an empty array #2080

Closed
ghost opened this issue Jun 29, 2019 · 7 comments
Closed

Entity castAsJson returns an empty array #2080

ghost opened this issue Jun 29, 2019 · 7 comments

Comments

@ghost
Copy link

ghost commented Jun 29, 2019

Describe the bug
Entity castAsJson returns an empty array

CodeIgniter 4 version
Codeigniter 4.0.0-beta.3

Affected module(s)
Entity

Expected behavior, and steps to reproduce if appropriate
I was testing the Entity class.

I called castAsJson('"{\"name\":\"test\",\"age\":0}"',$asArray=true) and expected to get ['name' => 'test','age' => 0],but the function returned an empty array instead.

Context

  • OS: Ubuntu 18.04
  • PHP version 7.3
@nowackipawel
Copy link
Contributor

I think everything is ok.
Just check difference between:

<?php

print_r(json_decode('"{\"name\":\"test\",\"age\":0}"'));

and

<?php

print_r(json_decode(json_decode('"{\"name\":\"test\",\"age\":0}"')));

@nowackipawel
Copy link
Contributor

or just try to pass: '{"name":"test","age":0}' instead of '"{\"name\":\"test\",\"age\":0}"' to that method.

@ghost
Copy link
Author

ghost commented Jun 29, 2019

Entity.php Line 591 (strpos($value, '"') === 0 && strrpos($value, '"') === 0 )

Seems we need to change the code above.

@nowackipawel
Copy link
Contributor

nowackipawel commented Jun 29, 2019

This code will check if first and last char from the string is - ". Am I wrong?
I think one improvement for this place could be extending entire line:
if ((is_string($value) && ((strpos($value, '[') === 0 && strrpos($value, ']') === 0) || (strpos($value, '{') === 0 && strrpos($value, '}') === 0) || (strpos($value, '"') === 0 && strrpos($value, '"') === 0 ))) || is_numeric($value))

As I said in my opinion for your parameter '"{\"name\":\"test\",\"age\":0}"' which is correct JSON STRING function should return the string : {"name":"test","age":0} not an object/array. If you'd like to get an object you should pass to it '{"name":"test","age":0}' or "{\"name\":\"test\",\"age\":0}" instead of '"{\"name\":\"test\",\"age\":0}"'

@ghost
Copy link
Author

ghost commented Jun 29, 2019

Well,actually '{\"name\":\"test\",\"age\":0}"' is a result that I got from MySQL DB

@ghost
Copy link
Author

ghost commented Jun 29, 2019

And I thought the correct code was (strpos($value, '"') === 0 && strrpos($value, '"') === strlen($value) - 1 )

nowackipawel added a commit to nowackipawel/ci4-old that referenced this issue Jun 29, 2019
codeigniter4#2080 
(...) And I thought the correct code was (strpos($value, '"') === 0 && strrpos($value, '"') === strlen($value) - 1 ) (...)
@nowackipawel
Copy link
Contributor

you are right in this point.

@ghost ghost closed this as completed Jul 4, 2019
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant