Skip to content

Commit

Permalink
implement json_encode() user function (#2352)
Browse files Browse the repository at this point in the history
* implemented json_encode()

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

* fixed json_encode and fixed tests

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

* ignore this

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

* bug fixes

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

* bug fixes

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 11, 2023
1 parent c571874 commit 3a2330f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
26 changes: 26 additions & 0 deletions CommandLine/testing/SimpleTests/json_encode_test.sog/create.edl
@@ -0,0 +1,26 @@
var async_load = ds_map_create();

//ds_map_add(async_load, "entries", "{\n\"entries\":[\n{\"name\":\"TomasJPereyra\",\"score\":1,\"rank\":1,\"userID\":\"@i64@1234$i64$\"},\n{\"name\":\"Benal\",\"score\":254,\"rank\":10,\"userID\":\"@i64@12345$i64$\",\"data\":\"SEJlbmFsIHdhcyBoZXJlIDopAAA=\"}\n\n]\n}\n");
ds_map_add(async_load, "entries", '{\n\"entries\":[\n{\"name\":\"TomasJPereyra\",\"score\":1,\"rank\":1,\"userID\":\"@i64@1234$i64$\"},\n{\"name\":\"Benal\",\"score\":254,\"rank\":10,\"userID\":\"@i64@12345$i64$\",\"data\":\"SEJlbmFsIHdhcyBoZXJlIDopAAA=\"}\n\n]\n}\n');
ds_map_add(async_load, "lb_name", "YYLeaderboard_10\/29\/21--");
ds_map_add(async_load, "event_type", "leaderboard_download");
ds_map_add(async_load, "id", 5.0);
ds_map_add(async_load, "num_entries", 2.0);
ds_map_add(async_load, "status", 1.0);

var decoded_string = json_encode(async_load);

var ds_map = json_decode(decoded_string);

gtest_assert_eq(ds_map_size(ds_map), 6);

gtest_assert_eq(ds_map_exists(ds_map, "entries"), true);
gtest_assert_eq(ds_map_exists(ds_map, "lb_name"), true);
gtest_assert_eq(ds_map_exists(ds_map, "event_type"), true);
gtest_assert_eq(ds_map_exists(ds_map, "id"), true);
gtest_assert_eq(ds_map_exists(ds_map, "num_entries"), true);
gtest_assert_eq(ds_map_exists(ds_map, "status"), true);

gtest_assert_eq(ds_map_exists(ds_map, "test_not_exist"), false);


39 changes: 35 additions & 4 deletions ENIGMAsystem/SHELL/Universal_System/Extensions/Json/json.cpp
Expand Up @@ -213,9 +213,40 @@ namespace enigma_user
return RecursiveDSMap(root);
}

string json_encode(variant ds_map)
{
Json::Value root;
return string("{ }");
string json_encode(variant ds_map) {
if (!enigma_user::ds_map_exists(ds_map)) {
DEBUG_MESSAGE("DS map does not exist", MESSAGE_TYPE::M_ERROR);
return string("{}");
}

string encoding_accumulator{""};

encoding_accumulator += '{';

variant key{enigma_user::ds_map_find_first(ds_map)};

for (int i = 0; i < enigma_user::ds_map_size(ds_map); i++) {
encoding_accumulator += '\"' + string(key) + '\"' + ':';

variant value {enigma_user::ds_map_find_value(ds_map, key)};

if (enigma_user::is_string(value))
encoding_accumulator += '\"' + enigma_user::toString(value) + '\"';
else
encoding_accumulator += enigma_user::toString(value);

key = enigma_user::ds_map_find_next(ds_map, key);

// 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;
}
}

0 comments on commit 3a2330f

Please sign in to comment.