From d026cd2ee71156dca5a34e5292572c17f2077f5a Mon Sep 17 00:00:00 2001 From: RabsRincon Date: Mon, 11 Jul 2016 13:33:13 +0200 Subject: [PATCH 1/3] JSON to KeyValues converter. Needs some more work, but it's pretty solid (Entry json keys are repeated as the subkey name and its child key) --- mp/src/game/client/client_momentum.vpc | 2 + mp/src/game/server/server_momentum.vpc | 3 +- mp/src/game/shared/momentum/util/jsontokv.cpp | 53 +++++++++++++++++++ mp/src/game/shared/momentum/util/jsontokv.h | 25 +++++++++ 4 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 mp/src/game/shared/momentum/util/jsontokv.cpp create mode 100644 mp/src/game/shared/momentum/util/jsontokv.h diff --git a/mp/src/game/client/client_momentum.vpc b/mp/src/game/client/client_momentum.vpc index ea9a70fc4e..d02c1c7920 100644 --- a/mp/src/game/client/client_momentum.vpc +++ b/mp/src/game/client/client_momentum.vpc @@ -178,6 +178,8 @@ $Project "Client (Momentum)" $File "$SRCDIR\game\shared\momentum\util\run_compare.h" $File "$SRCDIR\game\shared\momentum\util\run_stats.h" $File "$SRCDIR\game\shared\momentum\util\run_stats.cpp" + $File "$SRCDIR\game\shared\momentum\util\jsontokv.h" + $File "$SRCDIR\game\shared\momentum\util\jsontokv.cpp" } $Folder "Shader Editor" [$WIN32] diff --git a/mp/src/game/server/server_momentum.vpc b/mp/src/game/server/server_momentum.vpc index b13c2f17ee..63f8a5585e 100644 --- a/mp/src/game/server/server_momentum.vpc +++ b/mp/src/game/server/server_momentum.vpc @@ -52,7 +52,8 @@ $Project "Server (Momentum)" $File "$SRCDIR\game\shared\momentum\util\binary_reader.cpp" $File "$SRCDIR\game\shared\momentum\util\binary_reader.h" $File "$SRCDIR\game\shared\momentum\util\serialization.h" - + $File "$SRCDIR\game\shared\momentum\util\jsontokv.h" + $File "$SRCDIR\game\shared\momentum\util\jsontokv.cpp" } $Folder "Timer" diff --git a/mp/src/game/shared/momentum/util/jsontokv.cpp b/mp/src/game/shared/momentum/util/jsontokv.cpp new file mode 100644 index 0000000000..b8235763c9 --- /dev/null +++ b/mp/src/game/shared/momentum/util/jsontokv.cpp @@ -0,0 +1,53 @@ +#include "cbase.h" +#include "jsontokv.h" +KeyValues *CJsonToKeyValues::ConvertJsonToKeyValues(JsonNode *node) +{ + // This simply calls MapNode(node), which handles creating the new keyvalue and making sure node is not nullptr + return MapNode(node); +} + +void CJsonToKeyValues::MapNode(JsonNode *node, KeyValues *kv) +{ + if (!node || !kv) + return; + JsonValue value = node->value; + // What are we? + switch (value.getTag()) + { + case JSON_NUMBER: + kv->SetFloat(node->key, value.toNumber()); + break; + case JSON_STRING: + kv->SetString(node->key, value.toString()); + break; + case JSON_ARRAY: + case JSON_OBJECT: + for (auto i : value) + { + kv->AddSubKey(MapNode(i)); + } + break; + case JSON_TRUE: + kv->SetBool(node->key, true); + break; + case JSON_FALSE: + kv->SetBool(node->key, false); + break; + case JSON_NULL: + kv->SetString(node->key, nullptr); + break; + } +} + +KeyValues *CJsonToKeyValues::MapNode(JsonNode *node) +{ + // The parent KV holds the name of the first parent (Which, in case there is only 1 node/value, it will be the same + // as the only value it has) + if (!node) + return nullptr; + // Parent keyvalue. + KeyValues *pNodeValues = + new KeyValues((node->key == nullptr || POINTER_TO_INT(node->key) == 0xffeeffee) ? nullptr : node->key); + MapNode(node, pNodeValues); + return pNodeValues; +} \ No newline at end of file diff --git a/mp/src/game/shared/momentum/util/jsontokv.h b/mp/src/game/shared/momentum/util/jsontokv.h new file mode 100644 index 0000000000..c6d0aaa912 --- /dev/null +++ b/mp/src/game/shared/momentum/util/jsontokv.h @@ -0,0 +1,25 @@ +#pragma once + +#include "cbase.h" + +#include "gason.h" +#include + +class CJsonToKeyValues +{ + public: + DECLARE_CLASS_NOBASE(CJsonToKeyValues) + + CJsonToKeyValues(){}; + ~CJsonToKeyValues(){}; + + // Given a node, converts it and all its childern to KeyValues. + // Caller has to make sure the returned value is not of nullptr + KeyValues *ConvertJsonToKeyValues(JsonNode *); + + private: + // Maps a node (and later its childern) to a keyvalue + KeyValues *MapNode(JsonNode *); + // Maps a node and its children to the given keyvalue (recursively) + void MapNode(JsonNode *, KeyValues *); +}; \ No newline at end of file From a85a0869bf9d6305197a8504d826a6fd1da34237 Mon Sep 17 00:00:00 2001 From: RabsRincon Date: Mon, 11 Jul 2016 13:50:53 +0200 Subject: [PATCH 2/3] Printables (strings, numbers, booleans) are now not in its own subkey --- mp/src/game/shared/momentum/util/jsontokv.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/mp/src/game/shared/momentum/util/jsontokv.cpp b/mp/src/game/shared/momentum/util/jsontokv.cpp index b8235763c9..8e4d1b6ca4 100644 --- a/mp/src/game/shared/momentum/util/jsontokv.cpp +++ b/mp/src/game/shared/momentum/util/jsontokv.cpp @@ -1,4 +1,5 @@ #include "cbase.h" + #include "jsontokv.h" KeyValues *CJsonToKeyValues::ConvertJsonToKeyValues(JsonNode *node) { @@ -24,7 +25,15 @@ void CJsonToKeyValues::MapNode(JsonNode *node, KeyValues *kv) case JSON_OBJECT: for (auto i : value) { - kv->AddSubKey(MapNode(i)); + // If what we're going to parse is an object, then we need to add it as a subkey. + if (i->value.getTag() == JSON_OBJECT || i->value.getTag() == JSON_ARRAY) + { + kv->AddSubKey(MapNode(i)); + } + else //Otherwise (string, numbers, booleans) we just add them as an entry of the current key + { + MapNode(i, kv); + } } break; case JSON_TRUE: @@ -45,9 +54,14 @@ KeyValues *CJsonToKeyValues::MapNode(JsonNode *node) // as the only value it has) if (!node) return nullptr; + + // @Ruben: When node->key is null on the json, key is not nullptr, but 0xffeeffee. + // MOM_TODO: Is it always that adress? If not, when / how does it change? + // Parent keyvalue. KeyValues *pNodeValues = new KeyValues((node->key == nullptr || POINTER_TO_INT(node->key) == 0xffeeffee) ? nullptr : node->key); + MapNode(node, pNodeValues); return pNodeValues; } \ No newline at end of file From 383c8e99b03baa6e932ce2d6b5a50177e8e1391d Mon Sep 17 00:00:00 2001 From: RabsRincon Date: Mon, 11 Jul 2016 14:17:35 +0200 Subject: [PATCH 3/3] Nullptr check added on subkey --- mp/src/game/shared/momentum/util/jsontokv.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/mp/src/game/shared/momentum/util/jsontokv.cpp b/mp/src/game/shared/momentum/util/jsontokv.cpp index 8e4d1b6ca4..9a3827f4cf 100644 --- a/mp/src/game/shared/momentum/util/jsontokv.cpp +++ b/mp/src/game/shared/momentum/util/jsontokv.cpp @@ -28,9 +28,13 @@ void CJsonToKeyValues::MapNode(JsonNode *node, KeyValues *kv) // If what we're going to parse is an object, then we need to add it as a subkey. if (i->value.getTag() == JSON_OBJECT || i->value.getTag() == JSON_ARRAY) { - kv->AddSubKey(MapNode(i)); + KeyValues *pSub = MapNode(i); + if (pSub) + { + kv->AddSubKey(pSub); + } } - else //Otherwise (string, numbers, booleans) we just add them as an entry of the current key + else // Otherwise (string, numbers, booleans) we just add them as an entry of the current key { MapNode(i, kv); } @@ -57,7 +61,7 @@ KeyValues *CJsonToKeyValues::MapNode(JsonNode *node) // @Ruben: When node->key is null on the json, key is not nullptr, but 0xffeeffee. // MOM_TODO: Is it always that adress? If not, when / how does it change? - + // Parent keyvalue. KeyValues *pNodeValues = new KeyValues((node->key == nullptr || POINTER_TO_INT(node->key) == 0xffeeffee) ? nullptr : node->key);