private static ComplianceResult EvaluateExpression(JToken document, string expression) { try { var parser = new JmesPath(); parser.FunctionRepository .Register <ItemsFunction>() .Register <ToObjectFunction>() .Register <ZipFunction>() ; var result = parser.Transform(document, expression); return(new ComplianceResult { Success = true, Result = result, }); } catch (Exception exception) { return(new ComplianceResult { Success = false, Error = exception.Message, }); } }
/// <summary> /// Compute the bytes that should be signed. </summary> /// <exception cref="Exception"> /// @argument version impacts how those bytes are computed. /// @argument payloadPath parts of the link that should be signed. /// @returns bytes to be signed. </exception> public byte[] SignedBytes(string version, string payloadPath) { byte[] hashedResultBytes = null; switch (version) { case Constants.SIGNATURE_VERSION_1_0_0: if (string.IsNullOrEmpty(payloadPath)) { payloadPath = "[version,data,meta]"; } string input = null; try { input = Google.Protobuf.JsonFormatter.ToDiagnosticString(this.ALink); var jmes = new JmesPath(); string result = jmes.Transform(input, payloadPath); string canonicalResult = Canonicalizer.Canonicalize(result.ToString()); byte[] payloadBytes = Encoding.UTF8.GetBytes(canonicalResult); hashedResultBytes = CryptoUtils.Sha256(payloadBytes); } catch (IOException e1) { throw new ChainscriptException(e1); } break; default: throw new ChainscriptException(Error.SignatureVersionUnknown); } return(hashedResultBytes); }
public async Task WriteAsync(TextWriter writer, object content) { try { var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver(), Converters = (new JsonConverter[] { new VersionConverter(), new FileInfoConverter() }).ToList() }; var json = JsonConvert.SerializeObject(content, Formatting.Indented, settings); if (string.IsNullOrEmpty(this.Query)) { await writer.WriteLineAsync(json); } else { var jms = new JmesPath(); var result = jms.Transform(json, this.Query); var tmp = JsonConvert.DeserializeObject(result); await writer.WriteAsync(JsonConvert.SerializeObject(tmp, Formatting.Indented, settings)); } } catch (Exception ex) { await writer.WriteLineAsync($"Error: {ex.Message}"); } }
private object QueryCore(dynamic context, object[] arguments) { var hashParameters = arguments.OfType <HashParameterDictionary>(); var args = arguments.Except(hashParameters); var formatString = args.Take(1).SingleOrDefault()?.ToString() ?? "@"; var formatArguments = args.Skip(1).Select(ToJsonString).ToArray(); formatString = formatArguments.Select((_, index) => index).Aggregate(formatString.Replace("{", "{{").Replace("}", "}}"), (acc, index) => acc.Replace($"${index + 1}", $"{{{index}}}")); var query = string.Format(formatString, args: formatArguments.ToArray()); var json = arguments.OfType <HashParameterDictionary>().SingleOrDefault() ?? (object)context; var jmespath = new JmesPath(); jmespath.FunctionRepository .Register <ItemsFunction>() .Register <ToObjectFunction>() .Register <ZipFunction>(); var token = Services.Serializers.JTokenTranserializer(json); var transformOutput = jmespath.Transform(token, query); var transformObject = Services.Serializers.YamlDeserializer.Deserialize <object>(transformOutput.ToString()); return(transformObject); }
private void Transform() { ErrorTextBlock.Text = ""; ErrorTextBlock.ToolTip = ""; if (!string.IsNullOrWhiteSpace(FilterTextBox.Text) && !string.IsNullOrWhiteSpace(InputTextBox.Text)) { try { var text = _jmes.Transform(InputTextBox.Text, FilterTextBox.Text); var obj = JsonConvert.DeserializeObject(text); if (obj is null) { ErrorTextBlock.Text = "Output not updated due to improper filter"; } else { OutTextBox.Text = JsonConvert.SerializeObject(obj, Formatting.Indented); } } catch (Exception e) { ErrorTextBlock.Text = "[ERR]" + e.ToString(); ErrorTextBlock.ToolTip = e.ToString(); } } }
protected override void ProcessRecord() { var jmes = new JmesPath(); var result = jmes.Transform(json: Content, expression: JMESPath); WriteObject(result); }
public object Search(string expression, object json) { try { var jsonInput = _serializers.JsonSerializer.Serialize(json); var jsonOutput = _jmespath.Transform(jsonInput, expression); var result = _serializers.YamlDeserializer.Deserialize <object>(jsonOutput); return(result); } catch (Exception ex) { throw new QueryException(ex.Message + Environment.NewLine + expression, ex) { Expression = expression }; } }
public static string Transform() { jmes = new JmesPath(); var resultJson = jmes.Transform(json[3], expression[3]); return(resultJson); }
protected void Assert(string expression, string input, string expected) { var path = new JmesPath(); var result = path.Transform(input, expression); Xunit.Assert.Equal(expected, result); }
public override JToken Execute(params JmesPathFunctionArgument[] args) { var expression = args[0].Token.Value <string>(); var token = args[1].Token; var json = token.ToString(); var transformed = _jmesPath.Transform(json, expression); return(JToken.Parse(transformed)); }
/// <inheritdoc /> public async Task <Stream> FilterOutputAsync(Stream content, string query, CancellationToken cancellationToken = default) { if (string.IsNullOrEmpty(query)) { return(content); } cancellationToken.ThrowIfCancellationRequested(); using var reader = new StreamReader(content); var strContent = await reader.ReadToEndAsync(); cancellationToken.ThrowIfCancellationRequested(); var filtered = jmesPath.Transform(strContent, query); cancellationToken.ThrowIfCancellationRequested(); var bytes = Encoding.UTF8.GetBytes(filtered); return(new MemoryStream(bytes)); }
public object Search(string expression, object json) { var jtokenEmitter = new JTokenEmitter(); _serializers.ValueSerialier.SerializeValue(jtokenEmitter, json, json?.GetType() ?? typeof(object)); var transformOutput = _jmespath.Transform(jtokenEmitter.Root, expression); using (var stringWriter = new StringWriter()) { using (var jsonWriter = new JsonTextWriter(stringWriter) { CloseOutput = false }) { transformOutput.WriteTo(jsonWriter); } var jsonText = stringWriter.GetStringBuilder().ToString(); return(_serializers.YamlDeserializer.Deserialize <object>(jsonText)); } }
public static string Transform() { var resultJson = jmes.Transform(json[0], expression[0]); return(resultJson); }
public JToken Select(string path) { return(_instance.Transform(Token, path)); }