Coder Social home page Coder Social logo

JSON_FORCE_OBJECT breaks arrays about pushok HOT 7 CLOSED

edamov avatar edamov commented on June 15, 2024
JSON_FORCE_OBJECT breaks arrays

from pushok.

Comments (7)

edamov avatar edamov commented on June 15, 2024 1

Seems now I understand. Array non-associative keys shouldn't exist in json, right?

from pushok.

edamov avatar edamov commented on June 15, 2024

I think you are right.
I added JSON_FORCE_OBJECT for passing tests. It is useful when the recipient of the output is expecting an object and the array is empty. It will not throw an exception.
So you can make a PR if you wish.

from pushok.

edamov avatar edamov commented on June 15, 2024

I just updated the test (setCustomValue('array', [1,2,3])) and it passed (without your PR):

    public function testConvertToJSon()
    {
        $alert = Alert::create()->setTitle('title');

        $payload = Payload::create()
            ->setAlert($alert)
            ->setBadge(1)
            ->setSound('sound')
            ->setCategory('category')
            ->setThreadId('tread-id')
            ->setContentAvailability(true)
            ->setMutableContent(true)
            ->setCustomValue('key', 'value')
            ->setCustomValue('array', [1,2,3]);

        $this->assertJsonStringEqualsJsonString(
            '{"aps": {"alert": {"title": "title"}, "badge": 1, "sound": "sound", "category": "category", ' .
            ' "thread-id": "tread-id", "mutable-content": 1, "content-available": 1}, "key": "value", "array": [1,2,3]}',
            $payload->toJson()
        );
    }

Can you please give an example of Payload data which doesn't work as expected?

from pushok.

hnicolas-w avatar hnicolas-w commented on June 15, 2024

It's because assertJsonStringEqualsJsonString doesn't compare properly the JSON string is this case. (I might create an issue on PHPUnit but each thing in time...).

Just var_dump($payload->toJson()) and you will see what i'm talking about.
The only way to properly test this case it to json_decode($payload->toJson()) and test the type of the array value like i did in my PR.

Add this and you will see your test breaking:

$decoded_json = json_decode($payload->toJson());
$this->assertEquals(gettype($decoded_json->custom_array), 'array');

from pushok.

edamov avatar edamov commented on June 15, 2024

If you want json_decode returns an array you should pass second parameter true.
This will pass:

$decoded_json = json_decode($payload->toJson(), true);
$this->assertEquals(gettype($decoded_json['custom_array']), 'array');

from pushok.

hnicolas-w avatar hnicolas-w commented on June 15, 2024

Yeah but why passing this ? It's not good. This 2nd parameter does : When TRUE, returned objects will be converted into associative arrays. (aka the exact opposite of JSON_FORCE_OBJECT). So ok it works for the test but the lib is still outputing {0: 1, 1:2, 2:3} when i want [1,2,3].

Also look at my PR request, i feel you used JSON_FORCE_OBJECT just to make the aps key always an object even when empty. To me this is bad pratice as the aps key should have been a PHP Object from the start. Not an array.

from pushok.

edamov avatar edamov commented on June 15, 2024

Thanks, now it is clear

from pushok.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.