Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Decode.cs
1using System;
3using System.Threading.Tasks;
4using Waher.Content;
11
13{
18 {
27 public Decode(ScriptNode Content, ScriptNode ContentType, int Start, int Length, Expression Expression)
28 : base(Content, ContentType, Start, Length, Expression)
29 {
30 }
31
35 public override string FunctionName => nameof(Decode);
36
41 public override bool IsAsynchronous => true;
42
46 public override bool UpgradeScalarsToSameSet => false;
47
56 {
57 return this.EvaluateScalarAsync(Argument1, Argument2, Variables).Result;
58 }
59
68 {
69 if (!(Argument1.AssociatedObjectValue is byte[] Bin))
71
72 string ContentType = Argument2.AssociatedObjectValue is string s2 ? s2 : Expression.ToExpressionString(Argument2.AssociatedObjectValue);
73
74 return this.DoDecodeAsync(Bin, ContentType, null);
75 }
76
77 private async Task<IElement> DoDecodeAsync(byte[] Data, string ContentType, System.Text.Encoding Encoding)
78 {
79 ContentResponse Content;
80
81 if (Encoding is null)
82 Content = await InternetContent.DecodeAsync(ContentType, Data, null);
83 else
84 Content = await InternetContent.DecodeAsync(ContentType, Data, Encoding, Array.Empty<KeyValuePair<string, string>>(), null);
85
86 if (Content.HasError)
87 throw new ScriptRuntimeException(Content.Error.Message, this, Content.Error);
88
89 if (Content.Decoded is string[][] Records)
90 {
91 int Rows = Records.Length;
92 int MaxCols = 0;
93 int i, c;
94
95 foreach (string[] Rec in Records)
96 {
97 if (Rec is null)
98 continue;
99
100 c = Rec.Length;
101 if (c > MaxCols)
102 MaxCols = c;
103 }
104
106
107 foreach (string[] Rec in Records)
108 {
109 i = 0;
110
111 if (!(Rec is null))
112 {
113 foreach (string s in Rec)
114 {
115 if (s is null || string.IsNullOrEmpty(s))
116 Elements.Add(ObjectValue.Null);
117 else if (CommonTypes.TryParse(s, out double dbl))
118 Elements.Add(new DoubleNumber(dbl));
119 else if (CommonTypes.TryParse(s, out bool b))
120 Elements.Add(new BooleanValue(b));
121 else if (XML.TryParse(s, out DateTime TP))
122 Elements.Add(new DateTimeValue(TP));
123 else if (TimeSpan.TryParse(s, out TimeSpan TS))
124 Elements.Add(new ObjectValue(TS));
125 else
126 Elements.Add(new StringValue(s));
127
128 i++;
129 }
130 }
131
132 while (i++ < MaxCols)
133 Elements.Add(new StringValue(string.Empty));
134 }
135
136 return Operators.Matrices.MatrixDefinition.Encapsulate(Elements, Rows, MaxCols, this);
137 }
138 else
139 return Expression.Encapsulate(Content.Decoded);
140 }
141
150 {
151 return this.EvaluateScalarAsync(Argument1, Argument2, Variables).Result;
152 }
153
161 public override Task<IElement> EvaluateScalarAsync(string Argument1, string Argument2, Variables Variables)
162 {
163 System.Text.Encoding Encoding = System.Text.Encoding.UTF8;
164 byte[] Bin = Encoding.GetBytes(Argument1);
165
166 return this.DoDecodeAsync(Bin, Argument2, Encoding);
167 }
168 }
169}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:15
static bool TryParse(string s, out double Value)
Tries to decode a string encoded double.
Definition: CommonTypes.cs:48
Contains information about a response to a content request.
bool HasError
If an error occurred.
object Decoded
Decoded object.
Exception Error
Error response.
Static class managing encoding and decoding of internet content.
static Task< ContentResponse > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri)
Decodes an object.
Helps with common XML-related tasks.
Definition: XML.cs:21
static bool TryParse(string s, out DateTime Value)
Tries to decode a string encoded DateTime.
Definition: XML.cs:892
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
void Add(T Item)
Adds an item to the collection.
Definition: ChunkedList.cs:272
Decode(Content,ContentType)
Definition: Decode.cs:18
override Task< IElement > EvaluateScalarAsync(string Argument1, string Argument2, Variables Variables)
Evaluates the function on two scalar arguments.
Definition: Decode.cs:161
override string FunctionName
Name of the function
Definition: Decode.cs:35
override Task< IElement > EvaluateScalarAsync(IElement Argument1, IElement Argument2, Variables Variables)
Evaluates the function on two scalar arguments.
Definition: Decode.cs:67
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
Definition: Decode.cs:41
override IElement EvaluateScalar(string Argument1, string Argument2, Variables Variables)
Evaluates the function on two scalar arguments.
Definition: Decode.cs:149
Decode(ScriptNode Content, ScriptNode ContentType, int Start, int Length, Expression Expression)
Decode(Content,ContentType)
Definition: Decode.cs:27
override IElement EvaluateScalar(IElement Argument1, IElement Argument2, Variables Variables)
Evaluates the function on two scalar arguments.
Definition: Decode.cs:55
override bool UpgradeScalarsToSameSet
If scalars should be upgraded to the same set before evaluation.
Definition: Decode.cs:46
Class managing a script expression.
Definition: Expression.cs:41
static IElement Encapsulate(object Value)
Encapsulates an object.
Definition: Expression.cs:5241
static string ToExpressionString(object Value)
Converts an object to a string, that can be parsed as part of an expression.
Definition: Expression.cs:5052
Base class for funcions of two scalar variables.
ScriptNode Argument2
Function argument 2.
ScriptNode Argument1
Function argument 1.
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
Boolean-valued number.
Definition: BooleanValue.cs:12
static readonly ObjectValue Null
Null value.
Definition: ObjectValue.cs:88
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:21