Implemente parse json node having universal connector
Some checks failed
Build / build (push) Has been cancelled

This commit is contained in:
Ankitkumar Satapara
2026-04-22 21:21:00 +05:30
parent 132f6068b5
commit c8c556921f
7 changed files with 308 additions and 4 deletions

View File

@@ -135,18 +135,48 @@ namespace Nodify.Calculator.Execution.Resolvers
}
// ─────────────────────────────────────────────────────────────────────────────
// PARSEJSON: passthrough (output = parsed form of input). For value-reading
// purposes we just forward the raw JSON downstream.
// PARSEJSON: takes any input, ensures it is valid JSON, then parses it.
// If the input is not valid JSON, it serializes it first, then forwards
// the JSON string downstream for the selected target type.
// ─────────────────────────────────────────────────────────────────────────────
internal sealed class ParseJsonNodeValueResolver : INodeValueResolver
{
public bool CanResolve(OperationViewModel node, ConnectorViewModel c)
=> node is SystemOperationViewModel sys && sys.SystemOperationType == SystemOperations.PARSEJSON;
=> node is ParseJsonOperationViewModel
|| (node is SystemOperationViewModel sys && sys.SystemOperationType == SystemOperations.PARSEJSON);
public string Resolve(OperationViewModel node, ConnectorViewModel outputConnector, IValueResolutionContext ctx)
{
var input = ResolverUtils.FirstDataInput(node);
return input == null ? null : ctx.Read(input);
if (input == null) return null;
var raw = ctx.Read(input);
if (string.IsNullOrEmpty(raw)) return null;
// Ensure we have valid JSON — if not, serialize the raw value
var trimmed = raw.Trim();
bool isJson = (trimmed.StartsWith("{") && trimmed.EndsWith("}"))
|| (trimmed.StartsWith("[") && trimmed.EndsWith("]"))
|| (trimmed.StartsWith("\"") && trimmed.EndsWith("\""));
if (!isJson)
{
try
{
raw = Newtonsoft.Json.JsonConvert.SerializeObject(raw);
}
catch { /* keep raw as-is */ }
}
// Parse and re-serialize to ensure clean JSON output
try
{
var parsed = Newtonsoft.Json.JsonConvert.DeserializeObject(raw);
return Newtonsoft.Json.JsonConvert.SerializeObject(parsed);
}
catch
{
return raw;
}
}
}