Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Deserialize.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading.Tasks;
8using Waher.Script;
14
16{
21 {
30 : base(new ScriptNode[] { Argument }, new ArgumentType[] { ArgumentType.Normal }, Start, Length, Expression)
31 {
32 }
33
42 public Deserialize(ScriptNode Argument, ScriptNode BaseType, int Start, int Length, Expression Expression)
43 : base(new ScriptNode[] { Argument, BaseType }, new ArgumentType[] { ArgumentType.Normal, ArgumentType.Scalar },
45 {
46 }
47
51 public override string FunctionName => nameof(Deserialize);
52
56 public override string[] Aliases => new string[] { "FromBinary" };
57
61 public override string[] DefaultArgumentNames => new string[] { "ByteArray", "BaseType" };
62
69 protected virtual IDeserializer GetDeserializer(Variables Variables, byte[] Data)
70 {
71 return new BinaryDeserializer(string.Empty, Encoding.UTF8, Data, 1);
72 }
73
78 public override bool IsAsynchronous => true;
79
87 {
88 return this.EvaluateAsync(Arguments, Variables).Result;
89 }
90
97 public override async Task<IElement> EvaluateAsync(IElement[] Arguments, Variables Variables)
98 {
99 if (!(Arguments[0].AssociatedObjectValue is byte[] Bin))
100 throw new ScriptRuntimeException("The first argument to the Deserialize function must be a byte array.", this);
101
102 Type BaseType;
103
104 if (Arguments.Length < 2)
105 BaseType = typeof(object);
106 else if (Arguments[1].AssociatedObjectValue is Type TypeValue)
107 BaseType = TypeValue;
108 else
109 throw new ScriptRuntimeException("The optional second argument to the Deserialize function must be a type value.", this);
110
111 if (!(Ledger.Provider is NeuroLedgerProvider Provider))
112 throw new ScriptRuntimeException("Neuro-Ledger not initialized properly.", this);
113
114 IDeserializer Reader = this.GetDeserializer(Variables, Bin);
115 IObjectSerializer Serializer = await Provider.GetObjectSerializer(BaseType);
116 bool First = true;
117 object Result = null;
118 List<object> List = null;
119
120 while (Reader.ReadBit())
121 {
122 if (First)
123 First = false;
124 else if (List is null)
125 List = new List<object>() { Result };
126
127 Result = await Serializer.Deserialize(Reader, null, false);
128 List?.Add(Result);
129 }
130
131 if (List is null)
132 return new ObjectValue(Result);
133 else
134 return new ObjectVector(List.ToArray());
135 }
136
137 }
138}
Static interface for ledger persistence. In order to work, a ledger provider has to be assigned to it...
Definition: Ledger.cs:14
static ILedgerProvider Provider
Registered ledger provider.
Definition: Ledger.cs:83
Manages binary deserialization of data.
Class managing a script expression.
Definition: Expression.cs:39
Base class for multivariate funcions.
ScriptNode[] Arguments
Function arguments.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
Expression Expression
Expression of which the node is a part.
Definition: ScriptNode.cs:177
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
Collection of variables.
Definition: Variables.cs:25
Deserializes an object from a binary string of octets, using existing serializers.
Definition: Deserialize.cs:21
override async Task< IElement > EvaluateAsync(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: Deserialize.cs:97
override string FunctionName
Name of the function
Definition: Deserialize.cs:51
override string[] DefaultArgumentNames
Default Argument names
Definition: Deserialize.cs:61
override string[] Aliases
Optional aliases. If there are no aliases for the function, null is returned.
Definition: Deserialize.cs:56
Deserialize(ScriptNode Argument, ScriptNode BaseType, int Start, int Length, Expression Expression)
Deserializes an object from a binary string of octets, using existing serializers.
Definition: Deserialize.cs:42
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
Definition: Deserialize.cs:78
override IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: Deserialize.cs:86
Deserialize(ScriptNode Argument, int Start, int Length, Expression Expression)
Deserializes an object from a binary string of octets, using existing serializers.
Definition: Deserialize.cs:29
virtual IDeserializer GetDeserializer(Variables Variables, byte[] Data)
Creates a deserializer for the operation.
Definition: Deserialize.cs:69
Task< object > Deserialize(IDeserializer Reader, uint? DataType, bool Embedded)
Deserializes an object from a binary source.
Basic interface for all types of elements.
Definition: IElement.cs:20
ArgumentType
Type of parameter used in a function definition or a lambda definition.
Definition: IFunction.cs:9