Added deserialzor node for parsing the json to model ot list of model
This commit is contained in:
130
Examples/Nodify.Calculator/NodeHandlers/DeserializeHandler.cs
Normal file
130
Examples/Nodify.Calculator/NodeHandlers/DeserializeHandler.cs
Normal file
@@ -0,0 +1,130 @@
|
||||
using Nodify.Calculator.Models;
|
||||
using System.Drawing;
|
||||
|
||||
namespace Nodify.Calculator.NodeHandlers
|
||||
{
|
||||
public class DeserializeHandler : INodeHandler
|
||||
{
|
||||
public string NodeTypeKey => "System";
|
||||
|
||||
public bool CanCreate(OperationInfoViewModel info)
|
||||
=> info.Type == OperationType.System && info.sysOp == SystemOperations.DESERIALIZE;
|
||||
|
||||
public bool CanRestore(NodeData data)
|
||||
=> data.NodeType == "System" && data.SystemOp == nameof(SystemOperations.DESERIALIZE);
|
||||
|
||||
public OperationViewModel Create(OperationInfoViewModel info)
|
||||
{
|
||||
var op = new DeserializeOperationViewModel();
|
||||
|
||||
// Flow in
|
||||
op.Input.Add(new ConnectorViewModel
|
||||
{
|
||||
Title = "",
|
||||
Shape = ConnectorShape.Triangle,
|
||||
ConnectorColor = NodeColors.Exec
|
||||
});
|
||||
|
||||
// String input (accepts any data connection)
|
||||
op.Input.Add(new ConnectorViewModel
|
||||
{
|
||||
Title = "Json",
|
||||
Shape = ConnectorShape.Circle,
|
||||
ConnectorColor = NodeColors.String,
|
||||
IsCopyConnector = true
|
||||
});
|
||||
|
||||
// Flow out
|
||||
op.Output.Add(new ConnectorViewModel
|
||||
{
|
||||
Title = "",
|
||||
IsInput = false,
|
||||
Shape = ConnectorShape.Triangle,
|
||||
ConnectorColor = NodeColors.Exec
|
||||
});
|
||||
|
||||
// Default output (will be updated when user selects target type)
|
||||
op.Output.Add(new ConnectorViewModel
|
||||
{
|
||||
Title = "Result",
|
||||
IsInput = false,
|
||||
Shape = ConnectorShape.Circle,
|
||||
ConnectorColor = Color.LimeGreen,
|
||||
DataType = "object"
|
||||
});
|
||||
|
||||
return op;
|
||||
}
|
||||
|
||||
public OperationViewModel Restore(NodeData data)
|
||||
{
|
||||
var op = new DeserializeOperationViewModel();
|
||||
|
||||
// Flow in
|
||||
op.Input.Add(new ConnectorViewModel
|
||||
{
|
||||
Title = "",
|
||||
Shape = ConnectorShape.Triangle,
|
||||
ConnectorColor = NodeColors.Exec
|
||||
});
|
||||
|
||||
// String input
|
||||
op.Input.Add(new ConnectorViewModel
|
||||
{
|
||||
Title = "Json",
|
||||
Shape = ConnectorShape.Circle,
|
||||
ConnectorColor = NodeColors.String,
|
||||
IsCopyConnector = true
|
||||
});
|
||||
|
||||
// Restore adapted input connector from saved data
|
||||
if (data.InputConnectors.Count > 1 && data.InputConnectors[1].Shape != "Circle")
|
||||
{
|
||||
var saved = data.InputConnectors[1];
|
||||
var inp = op.Input[1]; // the Json input (index 1, after flow)
|
||||
inp.Shape = NodeHandlerRegistry.ParseShape(saved.Shape);
|
||||
inp.ConnectorColor = Color.FromArgb(saved.ColorArgb);
|
||||
inp.DataType = saved.DataType;
|
||||
}
|
||||
|
||||
// Flow out
|
||||
op.Output.Add(new ConnectorViewModel
|
||||
{
|
||||
Title = "",
|
||||
IsInput = false,
|
||||
Shape = ConnectorShape.Triangle,
|
||||
ConnectorColor = NodeColors.Exec
|
||||
});
|
||||
|
||||
// Restore selected target type
|
||||
if (!string.IsNullOrEmpty(data.ParseJsonTargetType))
|
||||
{
|
||||
op.RefreshAvailableTypes();
|
||||
op.SelectedTargetType = data.ParseJsonTargetType;
|
||||
}
|
||||
else
|
||||
{
|
||||
op.Output.Add(new ConnectorViewModel
|
||||
{
|
||||
Title = "Result",
|
||||
IsInput = false,
|
||||
Shape = ConnectorShape.Circle,
|
||||
ConnectorColor = Color.LimeGreen,
|
||||
DataType = "object"
|
||||
});
|
||||
}
|
||||
|
||||
return op;
|
||||
}
|
||||
|
||||
public void Save(OperationViewModel vm, NodeData data)
|
||||
{
|
||||
data.NodeType = "System";
|
||||
data.SystemOp = nameof(SystemOperations.DESERIALIZE);
|
||||
if (vm is DeserializeOperationViewModel desVm)
|
||||
{
|
||||
data.ParseJsonTargetType = desVm.SelectedTargetType ?? "object";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,6 +43,7 @@ namespace Nodify.Calculator.NodeHandlers
|
||||
_handlers.Add(new CopyHandler());
|
||||
_handlers.Add(new SplitHandler());
|
||||
_handlers.Add(new ParseJsonHandler());
|
||||
_handlers.Add(new DeserializeHandler());
|
||||
_handlers.Add(new ListAddHandler());
|
||||
_handlers.Add(new ListRemoveHandler());
|
||||
_handlers.Add(new ListUpdateHandler());
|
||||
@@ -90,6 +91,7 @@ namespace Nodify.Calculator.NodeHandlers
|
||||
KnotOperationViewModel => _handlers.OfType<KnotHandler>().FirstOrDefault(),
|
||||
StringConcatOperationViewModel => _handlers.OfType<StringConcatHandler>().FirstOrDefault(),
|
||||
ParseJsonOperationViewModel => _handlers.OfType<ParseJsonHandler>().FirstOrDefault(),
|
||||
DeserializeOperationViewModel => _handlers.OfType<DeserializeHandler>().FirstOrDefault(),
|
||||
FunctionOperationViewModel => _handlers.OfType<FunctionHandler>().FirstOrDefault(),
|
||||
AuthOperationViewModel => _handlers.OfType<AuthHandler>().FirstOrDefault(),
|
||||
TakeOperationViewModel => _handlers.OfType<TakeHandler>().FirstOrDefault(),
|
||||
|
||||
Reference in New Issue
Block a user