Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Serialize.cs
1using System;
2using System.Text;
3using System.Threading.Tasks;
7using Waher.Script;
12
14{
19 {
29 {
30 }
31
35 public override string FunctionName => nameof(Serialize);
36
40 public override string[] Aliases => new string[] { "ToBinary" };
41
45 public override string[] DefaultArgumentNames => new string[] { "Object" };
46
53 {
54 return new BinarySerializer(string.Empty, Encoding.UTF8);
55 }
56
61 public override bool IsAsynchronous => true;
62
70 {
71 return this.EvaluateAsync(Argument, Variables).Result;
72 }
73
80 public override async Task<IElement> EvaluateAsync(IElement Argument, Variables Variables)
81 {
82 if (!(Ledger.Provider is NeuroLedgerProvider Provider))
83 throw new ScriptRuntimeException("Neuro-Ledger not initialized properly.", this);
84
85 ISerializer Output = this.GetSerializer(Variables);
86 IObjectSerializer Serializer = null;
87 Type LastType = null;
88 Type T;
89 object Value;
90
91 if (Argument is IVector Vector)
92 {
93 foreach (IElement E in Vector.ChildElements)
94 {
95 Value = E.AssociatedObjectValue;
96 T = Value?.GetType() ?? typeof(object);
97 if (T != LastType)
98 {
99 LastType = T;
100 Serializer = await Provider.GetObjectSerializer(T);
101 }
102
103 Output.WriteBit(true);
104 await Serializer.Serialize(Output, true, false, Value, null);
105 }
106 }
107 else
108 {
109 Value = Argument.AssociatedObjectValue;
110 Serializer = await Provider.GetObjectSerializer(Value?.GetType() ?? typeof(object));
111 Output.WriteBit(true);
112 await Serializer.Serialize(Output, true, false, Value, null);
113 }
114
115 Output.WriteBit(false);
116
117 byte[] Bin = Output.GetSerialization();
118
119 return new ObjectValue(Bin);
120 }
121
122 }
123}
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 serialization of data.
Class managing a script expression.
Definition: Expression.cs:39
Base class for funcions of one variable.
ScriptNode Argument
Function argument.
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
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
Collection of variables.
Definition: Variables.cs:25
Serializes an object to a binary string of octets, using existing serializers.
Definition: Serialize.cs:19
Serialize(ScriptNode Argument, int Start, int Length, Expression Expression)
Serializes an object to a binary string of octets, using existing serializers.
Definition: Serialize.cs:27
override string[] Aliases
Optional aliases. If there are no aliases for the function, null is returned.
Definition: Serialize.cs:40
override string FunctionName
Name of the function
Definition: Serialize.cs:35
override string[] DefaultArgumentNames
Default Argument names
Definition: Serialize.cs:45
override IElement Evaluate(IElement Argument, Variables Variables)
Evaluates the function.
Definition: Serialize.cs:69
virtual ISerializer GetSerializer(Variables Variables)
Creates a serializer for the operation.
Definition: Serialize.cs:52
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
Definition: Serialize.cs:61
override async Task< IElement > EvaluateAsync(IElement Argument, Variables Variables)
Evaluates the function.
Definition: Serialize.cs:80
Task Serialize(ISerializer Writer, bool WriteTypeCode, bool Embedded, object Value, object State)
Serializes an object to a binary destination.
byte[] GetSerialization()
Gets the binary serialization.
void WriteBit(bool Bit)
Serializes a bit.
Basic interface for all types of elements.
Definition: IElement.cs:20
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:33
ICollection< IElement > ChildElements
An enumeration of child elements. If the element is a scalar, this property will return null.
Definition: IElement.cs:49
Basic interface for vectors.
Definition: IVector.cs:9