petlof / liveresults

EmmaClient is a client-server system for publishing liveresults from Orienteering events. It have been used on many major events such as JWOC, WOC, EOC,..
http://liveresultat.orientering.se
GNU Lesser General Public License v3.0
40 stars 24 forks source link

Refactoring JSON parsing in the API #43

Closed ghostops closed 5 years ago

ghostops commented 5 years ago

The JSON parsing in the web/api.php is done manually, which might lead to non-parseable JSON being served by the API. I propose re-writing the API to utilise json_encode instead. I will do it myself and submit a PR, but it would be a bit of work so just checking there isn't a reason for the API being built this way.

Example:

if ($_GET['method'] == 'getcompetitions')
{
    $comps = Emma::GetCompetitions();
    $parsedComps = [];

    foreach ($comps as $comp) {
        $parsed = [
            'id' => $comp["tavid"],
            'name' => $comp["compName"],
            'organizer' => $comp["organizer"],
            'date' => date("Y-m-d",strtotime($comp['compDate'])),
            'timediff' => $comp["timediff"]
        ];

        if ($comp["multidaystage"] != "") {
            $parsed['multidaystage'] = $comp["multidaystage"];
            $parsed['multidayfirstday'] = $comp["multidayparent"];
        }

        $parsedComps[] = $parsed;
    }

    echo json_encode($parsedComps);
}

Would like to hear from you @petlof 😃