-
-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from momentum-mod/jsontokv_wip
Jsontokv wip
- Loading branch information
Showing
4 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#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) | ||
{ | ||
// 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) | ||
{ | ||
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 | ||
{ | ||
MapNode(i, kv); | ||
} | ||
} | ||
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; | ||
|
||
// @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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#pragma once | ||
|
||
#include "cbase.h" | ||
|
||
#include "gason.h" | ||
#include <KeyValues.h> | ||
|
||
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 *); | ||
}; |