simresults icon indicating copy to clipboard operation
simresults copied to clipboard

Implement json export

Open mauserrifle opened this issue 9 years ago • 4 comments

Easy converting to JSON would be of big value for users who do not know PHP and just want to get a fast universal JSON output.

mauserrifle avatar Dec 07 '15 12:12 mauserrifle

Hi,

I'm interested in getting JSON output for your classes to store the results in MongoDB. I went ahead and experimented with the code base, knowing not much of PHP:

use JsonSerializable; class Vehicle implements JsonSerializable {

     * Get the json representation of the object
     *
     * @return  string
     */
    public function jsonSerialize() {
        return [
            'name' => $this->getName(),
            'type' => $this->getType(),
            'class' => $this->getClass(),
            'number' => $this->getNumber(),
            'ballast' => $this->getBallast(),
            'restrictor' => $this->getRestrictor(),
            'skin' => $this->getSkin(),
            'friendlyName' => $this->getFriendlyName()
        ];
    }

As you're far more knowledgable on PHP than I ever will be, do you think this is a sound approach? If so, I'll continue and push it to github.

tspindler-cms avatar Jun 26 '19 11:06 tspindler-cms

Yes I think this would be the approach :) Anyway create it what works for you. Eventually when I have time one day, I might use it. Thanks for wanting to share with others!

mauserrifle avatar Jun 28 '19 14:06 mauserrifle

Hi, thanks for the encouraging words. I cloned your repo and added JSON serialize functions to most classes: https://github.com/tspindler-cms/simresults I'm not sure on how to proceed on testing these, would you happen to have some ideas for that?

tspindler-cms avatar Jul 02 '19 07:07 tspindler-cms

Cool! :+1: Take a look at the specs folder. You could add a function called called it_can_cast_to_json to a specific class test file. Set some test values and then check the json output.

For example see it_can_cast_to_string on LapSpec. Very similar: https://github.com/mauserrifle/simresults/blob/develop/spec/Simresults/LapSpec.php#L23

You can run the spec tests using ./vendor/bin/phpspec run

mauserrifle avatar Jul 15 '19 07:07 mauserrifle