Skip to content

Instantly share code, notes, and snippets.

@ThisIsAreku
Created October 13, 2023 19:37
Show Gist options
  • Save ThisIsAreku/fd3dec66ceebc0899e081ff3e14b5fad to your computer and use it in GitHub Desktop.
Save ThisIsAreku/fd3dec66ceebc0899e081ff3e14b5fad to your computer and use it in GitHub Desktop.
Streamer.bot parse json to arguments
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