using System; using System.Linq; using Codely.Newtonsoft.Json.Linq; namespace UnityTcp.Editor.Helpers { /// /// Helper class for JSON command processing utilities /// public static class JsonCommandHelper { /// /// Helper method to check if a string is valid JSON /// public static bool IsValidJson(string text) { if (string.IsNullOrWhiteSpace(text)) { return false; } text = text.Trim(); if ( (text.StartsWith("{") && text.EndsWith("}")) || // Object (text.StartsWith("[") && text.EndsWith("]")) ) // Array { try { JToken.Parse(text); return true; } catch { return false; } } return false; } /// /// Helper method to get a summary of parameters for error reporting /// public static string GetParamsSummary(JObject @params) { try { return @params == null || !@params.HasValues ? "No parameters" : string.Join( ", ", @params .Properties() .Select(static p => $"{p.Name}: {p.Value?.ToString()?[..Math.Min(20, p.Value?.ToString()?.Length ?? 0)]}" ) ); } catch { return "Could not summarize parameters"; } } } }