public async Task <IActionResult> PostAsync([FromBody] JsonDocument body, CancellationToken cancellationToken) { // Authorizing interactions: https://discord.com/developers/docs/interactions/receiving-and-responding#security-and-authorization if (!Request.Headers.TryGetValue("X-Signature-Ed25519", out var sigString)) { return(Unauthorized()); } if (!Request.Headers.TryGetValue("X-Signature-Timestamp", out var timestamp)) { return(Unauthorized()); } var data = Encoding.UTF8.GetBytes(timestamp + body.RootElement.GetRawText()); var signature = Convert.FromHexString(sigString); if (!_verificationAlgorithm.Verify(_verificationPublicKey, data, signature)) { return(Unauthorized()); } var interaction = body.Deserialize <Interaction>() ?? throw new ArgumentException("Interaction body must not be null", nameof(body)); return(Ok(await HandleInteractionAsync(interaction, cancellationToken))); }
public static void JsonDocumentDeserialize_Null() { using JsonDocument dom = JsonDocument.Parse("null"); MyPoco obj = dom.Deserialize <MyPoco>(); Assert.Null(obj); }
public static void JsonDocumentDeserialize_NonGeneric() { using JsonDocument dom = JsonDocument.Parse(Json); MyPoco obj = (MyPoco)dom.Deserialize(typeof(MyPoco)); obj.Verify(); }
public static void JsonDocumentDeserialize_Generic() { using JsonDocument dom = JsonDocument.Parse(Json); MyPoco obj = dom.Deserialize <MyPoco>(); obj.Verify(); }
public override Task <object> DeserializeWrapper(string json, Type type, JsonSerializerContext context) { if (json is null) { // Emulate a null document for API validation tests. return(Task.FromResult(JsonSerializer.Deserialize(document: null, type, context))); } using JsonDocument document = JsonDocument.Parse(json); return(Task.FromResult(document.Deserialize(type, context))); }
public override Task <T> DeserializeWrapper <T>(string json, JsonTypeInfo <T> jsonTypeInfo) { if (json is null) { // Emulate a null document for API validation tests. return(Task.FromResult(JsonSerializer.Deserialize <T>(document: null, jsonTypeInfo))); } using JsonDocument document = JsonDocument.Parse(json); return(Task.FromResult(document.Deserialize <T>(jsonTypeInfo))); }
public override Task <object> DeserializeWrapper(string json, Type type, JsonSerializerOptions options = null) { if (json is null) { // Emulate a null document for API validation tests. return(Task.FromResult(JsonSerializer.Deserialize(document: null, type, options))); } using JsonDocument document = JsonDocument.Parse(json, OptionsHelpers.GetDocumentOptions(options)); return(Task.FromResult(document.Deserialize(type, options))); }
protected internal override Task <T> DeserializeWrapper <T>(string json, JsonSerializerOptions options = null) { if (json is null) { // Emulate a null document for API validation tests. return(Task.FromResult(JsonSerializer.Deserialize <T>(document: null))); } using JsonDocument document = JsonDocument.Parse(json); return(Task.FromResult(document.Deserialize <T>(options))); }
public override ComponentDescription?Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { JsonDocument document = JsonDocument.ParseValue(ref reader); document.RootElement.TryGetProperty("type", out JsonElement ty); int type = ty.GetInt32(); ComponentType ct = (ComponentType)type; JsonSerializerOptions jso = new JsonSerializerOptions(options); jso.Converters.Remove(this); switch (ct) { case ComponentType.SWITCH: return(document.Deserialize <DescriptionSwitch>(jso)); case ComponentType.LAMP: return(document.Deserialize <DescriptionLamp>(jso)); case ComponentType.LOGIC_GATE: return(document.Deserialize <DescriptionGate>(jso)); case ComponentType.BUFFER: return(document.Deserialize <DescriptionBuffer>(jso)); case ComponentType.TRI_STATE: return(document.Deserialize <DescriptionTriState>(jso)); case ComponentType.INTEGRATED: return(document.Deserialize <DescriptionIntegrated>(options)); } throw new ApplicationException(String.Format("The component type {0} is not supported!", type)); }
public ActionResult UpdateNamedConfiguration([FromRoute, Required] string key, [FromBody, Required] JsonDocument configuration) { var configurationType = _configurationManager.GetConfigurationType(key); var deserializedConfiguration = configuration.Deserialize(configurationType, _serializerOptions); if (deserializedConfiguration == null) { throw new ArgumentException("Body doesn't contain a valid configuration"); } _configurationManager.SaveConfiguration(key, deserializedConfiguration); return(NoContent()); }