Skip to content

Commit

Permalink
Fix json extension (#2357)
Browse files Browse the repository at this point in the history
* fixed json extension conversion to ds map and improved naming of some files

Signed-off-by: Saif Kandil <74428638+k0T0z@users.noreply.github.com>

* fix #2354

Signed-off-by: Saif Kandil <74428638+k0T0z@users.noreply.github.com>

---------

Signed-off-by: Saif Kandil <74428638+k0T0z@users.noreply.github.com>
  • Loading branch information
k0T0z committed Aug 16, 2023
1 parent d64a8ec commit 8d105a4
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 57 deletions.
59 changes: 58 additions & 1 deletion CommandLine/testing/SimpleTests/json_test.sog/create.edl
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
var json = '{"entries":{"entries":[{"name":"AlEw","score":196198,"rank":1,"userID":"1234"},{"name":"El Pice","score":58158,"rank":2,"userID":"12345"},{"name":"saif","score":505,"rank":3,"userID":"12346"},{"name":"Bucciarati (Couter55)","score":336,"rank":4,"userID":"12347"},{"name":"Haiyatin","score":236,"rank":5,"userID":"12348"},{"name":"Kaito","score":137,"rank":6,"userID":"12349"},{"name":"gamesadasteam","score":96,"rank":7,"userID":"123410"},{"name":"Bashkash2013","score":7,"rank":8,"userID":"123411"},{"name":"silveradagio","score":0,"rank":9,"userID":"123412"},{"name":"HappyStarXiXi","score":0,"rank":10,"userID":"123413"}]},"event_type":"leaderboard_download","id":0,"lb_name":"","num_entries":10,"status":0}';
var json = '{"entries":{"entries":[{"name":"AlEw","score":196198,"rank":1,"userID":"1234"},{"name":"El Pice","score":58158,"rank":2,"userID":"12345"},{"name":"saif","score":505,"rank":3,"userID":"12346"},{"name":"Bucciarati (Couter55)","score":336,"rank":4,"userID":"12347"},{"name":"Haiyatin","score":236,"rank":5,"userID":"12348"},{"name":"Kaito","score":137,"rank":6,"userID":"12349"},{"name":"gamesadasteam","score":96,"rank":7,"userID":"123410"},{"name":"Bashkash2013","score":7,"rank":8,"userID":"123411"},{"name":"silveradagio","score":0,"rank":9,"userID":"123412"},{"name":"HappyStarXiXi","score":0,"rank":10,"userID":"123413"}]},"event_type":"leaderboard_download","id":0,"lb_name":"SteamLeaderboard","num_entries":10,"status":0}';

var json_decoded = json_decode(json);

gtest_assert_eq(ds_map_size(json_decoded), 6);
gtest_assert_eq(ds_map_size(ds_map_find_value(json_decoded, "entries")), 1);

gtest_assert_eq(ds_map_exists(json_decoded, "entries"), true);
gtest_assert_eq(ds_map_exists(json_decoded, "lb_name"), true);
Expand All @@ -11,7 +12,63 @@ gtest_assert_eq(ds_map_exists(json_decoded, "id"), true);
gtest_assert_eq(ds_map_exists(json_decoded, "num_entries"), true);
gtest_assert_eq(ds_map_exists(json_decoded, "status"), true);

gtest_assert_eq(ds_map_find_value(json_decoded, "event_type"), "leaderboard_download");
gtest_assert_eq(ds_map_find_value(json_decoded, "id"), 0);
gtest_assert_eq(ds_map_find_value(json_decoded, "lb_name"), "SteamLeaderboard");
gtest_assert_eq(ds_map_find_value(json_decoded, "num_entries"), 10);
gtest_assert_eq(ds_map_find_value(json_decoded, "status"), 0);

var entries = ds_map_find_value(json_decoded, "entries");
var list = ds_map_find_value(entries, "entries");

gtest_assert_eq(ds_list_size(list), 10);

gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 0), "name"), "AlEw");
gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 0), "score"), 196198);
gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 0), "rank"), 1);
gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 0), "userID"), "1234");

gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 1), "name"), "El Pice");
gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 1), "score"), 58158);
gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 1), "rank"), 2);
gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 1), "userID"), "12345");

gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 2), "name"), "saif");
gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 2), "score"), 505);
gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 2), "rank"), 3);
gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 2), "userID"), "12346");

gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 3), "name"), "Bucciarati (Couter55)");
gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 3), "score"), 336);
gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 3), "rank"), 4);
gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 3), "userID"), "12347");

gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 4), "name"), "Haiyatin");
gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 4), "score"), 236);
gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 4), "rank"), 5);
gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 4), "userID"), "12348");

gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 5), "name"), "Kaito");
gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 5), "score"), 137);
gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 5), "rank"), 6);
gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 5), "userID"), "12349");

gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 6), "name"), "gamesadasteam");
gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 6), "score"), 96);
gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 6), "rank"), 7);
gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 6), "userID"), "123410");

gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 7), "name"), "Bashkash2013");
gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 7), "score"), 7);
gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 7), "rank"), 8);
gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 7), "userID"), "123411");

gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 8), "name"), "silveradagio");
gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 8), "score"), 0);
gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 8), "rank"), 9);
gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 8), "userID"), "123412");

gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 9), "name"), "HappyStarXiXi");
gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 9), "score"), 0);
gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 9), "rank"), 10);
gtest_assert_eq(ds_map_find_value(ds_list_find_value(list, 9), "userID"), "123413");
22 changes: 22 additions & 0 deletions ENIGMAsystem/SHELL/Universal_System/Extensions/Json/include.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,24 @@
/**
* @file include.h
* @section License
*
* Copyright (C) 2023 Saif Kandil
*
* This file is a part of the ENIGMA Development Environment.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
**/

#include "json.h"
#include "short_json.h"
73 changes: 17 additions & 56 deletions ENIGMAsystem/SHELL/Universal_System/Extensions/Json/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @section License
*
* Copyright (C) 2013 Ssss
* Copyright (C) 2023 Saif Kandil
*
* This file is a part of the ENIGMA Development Environment.
*
Expand Down Expand Up @@ -43,56 +44,38 @@ namespace enigma_user
{
if (root[*it].isNull())
{
int dsmap=ds_map_create();
ds_map_add(dsmap,"default",0);
ds_map_add(jsonObject,*it,dsmap);
ds_map_add(jsonObject,*it,0);
}
else if (root[*it].isBool())
{
int dsmap=ds_map_create();
ds_map_add(dsmap,"default",root[*it].asInt());
ds_map_add(jsonObject,*it,dsmap);
ds_map_add(jsonObject,*it,root[*it].asInt());
}
else if (root[*it].isInt())
{
int dsmap=ds_map_create();
ds_map_add(dsmap,"default",root[*it].asInt());
ds_map_add(jsonObject,*it,dsmap);
ds_map_add(jsonObject,*it,root[*it].asInt());
}
else if (root[*it].isUInt())
{
int dsmap=ds_map_create();
ds_map_add(dsmap,"default",root[*it].asUInt());
ds_map_add(jsonObject,*it,dsmap);
ds_map_add(jsonObject,*it,root[*it].asUInt());
}
else if (root[*it].isIntegral())
{
int dsmap=ds_map_create();
ds_map_add(dsmap,"default",root[*it].asInt());
ds_map_add(jsonObject,*it,dsmap);
ds_map_add(jsonObject,*it,root[*it].asInt());
}
else if (root[*it].isDouble())
{
int dsmap=ds_map_create();
ds_map_add(dsmap,"default",root[*it].asDouble());
ds_map_add(jsonObject,*it,dsmap);
ds_map_add(jsonObject,*it,root[*it].asDouble());
}
else if (root[*it].isNumeric())
{
int dsmap=ds_map_create();
ds_map_add(dsmap,"default",root[*it].asDouble());
ds_map_add(jsonObject,*it,dsmap);
ds_map_add(jsonObject,*it,root[*it].asDouble());
}
else if (root[*it].isString())
{
//ds_map with key "default" and value
int dsmap=ds_map_create();
ds_map_add(dsmap,"default",root[*it].asString());
ds_map_add(jsonObject,*it,dsmap);
ds_map_add(jsonObject,*it,root[*it].asString());
}
else if (root[*it].isArray())
{
//ds_map with key "default" and ds_list of the objects or values
int dsmap=RecursiveDSList(root[*it]);
if (dsmap==-1)
{
Expand Down Expand Up @@ -125,56 +108,38 @@ namespace enigma_user
{
if (root[i].isNull())
{
int dsmap=ds_map_create();
ds_map_add(dsmap,"default",0);
ds_list_add(jsonArray,(enigma::varargs(), dsmap));
ds_list_add(jsonArray,(enigma::varargs(), 0));
}
else if (root[i].isBool())
{
int dsmap=ds_map_create();
ds_map_add(dsmap,"default",root[i].asInt());
ds_list_add(jsonArray,(enigma::varargs(), dsmap));
ds_list_add(jsonArray,(enigma::varargs(), root[i].asInt()));
}
else if (root[i].isInt())
{
int dsmap=ds_map_create();
ds_map_add(dsmap,"default",root[i].asInt());
ds_list_add(jsonArray,(enigma::varargs(), dsmap));
ds_list_add(jsonArray,(enigma::varargs(), root[i].asInt()));
}
else if (root[i].isUInt())
{
int dsmap=ds_map_create();
ds_map_add(dsmap,"default",root[i].asUInt());
ds_list_add(jsonArray,(enigma::varargs(), dsmap));
ds_list_add(jsonArray,(enigma::varargs(), root[i].asUInt()));
}
else if (root[i].isIntegral())
{
int dsmap=ds_map_create();
ds_map_add(dsmap,"default",root[i].asInt());
ds_list_add(jsonArray,(enigma::varargs(), dsmap));
ds_list_add(jsonArray,(enigma::varargs(), root[i].asInt()));
}
else if (root[i].isDouble())
{
int dsmap=ds_map_create();
ds_map_add(dsmap,"default",root[i].asDouble());
ds_list_add(jsonArray,(enigma::varargs(), dsmap));
ds_list_add(jsonArray,(enigma::varargs(), root[i].asDouble()));
}
else if (root[i].isNumeric())
{
int dsmap=ds_map_create();
ds_map_add(dsmap,"default",root[i].asDouble());
ds_list_add(jsonArray,(enigma::varargs(), dsmap));
ds_list_add(jsonArray,(enigma::varargs(), root[i].asDouble()));
}
else if (root[i].isString())
{
//ds_map with key "default" and value
int dsmap=ds_map_create();
ds_map_add(dsmap,"default",root[i].asString());
ds_list_add(jsonArray,(enigma::varargs(), dsmap));
ds_list_add(jsonArray,(enigma::varargs(), root[i].asString()));
}
else if (root[i].isArray())
{
//ds_map with key "default" and ds_list of the objects or values
int dsmap=RecursiveDSList(root[i]);
if (dsmap==-1)
{
Expand Down Expand Up @@ -238,14 +203,10 @@ namespace enigma_user

// Add comma if not last element
if (i != enigma_user::ds_map_size(ds_map) - 1) encoding_accumulator << ',';

// DEBUG_MESSAGE(value, MESSAGE_TYPE::M_INFO);
}

encoding_accumulator << '}';

// DEBUG_MESSAGE(encoding_accumulator, MESSAGE_TYPE::M_INFO);

return encoding_accumulator.str();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright 2007-2010 Baptiste Lepilleur
// Copyright 2023 Saif Kandil
// Distributed under MIT license, or public domain if desired and
// recognized in your jurisdiction.
// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright 2007-2010 Baptiste Lepilleur
// Copyright 2023 Saif Kandil
// Distributed under MIT license, or public domain if desired and
// recognized in your jurisdiction.
// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
Expand Down

0 comments on commit 8d105a4

Please sign in to comment.