Created
October 13, 2023 19:37
-
-
Save ThisIsAreku/fd3dec66ceebc0899e081ff3e14b5fad to your computer and use it in GitHub Desktop.
Streamer.bot parse json to arguments
This file contains hidden or 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
using Newtonsoft.Json.Linq; | |
public class CPHInline | |
{ | |
public bool Execute() | |
{ | |
string json = args.ContainsKey("json") ? args["json"].ToString().Trim() : ""; | |
string prefix = args.ContainsKey("parsePrefix") ? args["parsePrefix"].ToString() : "parse"; | |
if (json == "") | |
{ | |
CPH.LogWarn("json argument is empty"); | |
return false; | |
} | |
var allJsonItems = JObject.Parse(json).Descendants(); | |
foreach (var item in allJsonItems) | |
{ | |
var argName = prefix + "." + item.Path; | |
object argValue = null; | |
if (item is JArray) | |
{ | |
argName += ".__count"; | |
argValue = (item as JArray)?.Count ?? 0; | |
} | |
else if (item is JValue) | |
{ | |
argValue = (item as JValue)?.Value; | |
} | |
if (argValue == null) | |
{ | |
continue; | |
} | |
CPH.LogDebug(argName + " = " + argValue); | |
CPH.SetArgument(argName, argValue); | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment