Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Encode.cs
1using System;
3using System.Threading.Tasks;
4using Waher.Content;
10
12{
17 {
26 : base(new ScriptNode[] { Object }, argumentTypes1Normal, Start, Length, Expression)
27 {
28 }
29
38 public Encode(ScriptNode Object, ScriptNode AcceptedTypes, int Start, int Length, Expression Expression)
39 : base(new ScriptNode[] { Object, AcceptedTypes }, new ArgumentType[] { ArgumentType.Normal, ArgumentType.Vector },
41 {
42 }
43
47 public override string FunctionName => nameof(Encode);
48
52 public override string[] DefaultArgumentNames => new string[] { "Object" };
53
58 public override bool IsAsynchronous => true;
59
67 {
68 return this.EvaluateAsync(Arguments, Variables).Result;
69 }
70
77 public override async Task<IElement> EvaluateAsync(IElement[] Arguments, Variables Variables)
78 {
79 ContentResponse Content;
80
81 if (Arguments.Length > 1)
82 {
83 if (!(Arguments[1].AssociatedObjectValue is Array A))
84 throw new ScriptRuntimeException("Second parameter to Encode should be an array of acceptable content types.", this);
85
86 int i, c = A.Length;
87 string[] AcceptedTypes = new string[c];
88
89 for (i = 0; i < c; i++)
90 AcceptedTypes[i] = (await WaitPossibleTask(A.GetValue(i)))?.ToString();
91
92 Content = await InternetContent.EncodeAsync(Arguments[0].AssociatedObjectValue, System.Text.Encoding.UTF8, AcceptedTypes);
93 }
94 else
95 Content = await InternetContent.EncodeAsync(Arguments[0].AssociatedObjectValue, System.Text.Encoding.UTF8);
96
97 return new ObjectVector(new ObjectValue(Content.Encoded), new StringValue(Content.ContentType));
98 }
99
100 }
101}
Contains information about a response to a content request.
byte[] Encoded
Encoded object.
string ContentType
Internet Content-Type of encoded object.
Static class managing encoding and decoding of internet content.
static Task< ContentResponse > EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
Encodes an object.
override string FunctionName
Name of the function
Definition: Encode.cs:47
Encode(ScriptNode Object, ScriptNode AcceptedTypes, int Start, int Length, Expression Expression)
Encode(Object,AcceptedTypes)
Definition: Encode.cs:38
Encode(ScriptNode Object, int Start, int Length, Expression Expression)
Encode(Object)
Definition: Encode.cs:25
override string[] DefaultArgumentNames
Default Argument names
Definition: Encode.cs:52
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
Definition: Encode.cs:58
override async Task< IElement > EvaluateAsync(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: Encode.cs:77
override IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: Encode.cs:66
Class managing a script expression.
Definition: Expression.cs:41
Base class for multivariate funcions.
ScriptNode[] Arguments
Function arguments.
static readonly ArgumentType[] argumentTypes1Normal
One scalar parameter.
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
static async Task< object > WaitPossibleTask(object Result)
Waits for any asynchronous process to terminate.
Definition: ScriptNode.cs:441
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
Basic interface for all types of elements.
Definition: IElement.cs:21
ArgumentType
Type of parameter used in a function definition or a lambda definition.
Definition: IFunction.cs:9