Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save habib-sadullaev/e5bccebabf65ad6a3405bff5ced9b496 to your computer and use it in GitHub Desktop.
Save habib-sadullaev/e5bccebabf65ad6a3405bff5ced9b496 to your computer and use it in GitHub Desktop.
using System;
using System.Text.Json;
var value = new { name = default(string)! };
//method #1
value = (dynamic)JsonSerializer.Deserialize("""{ "name": "xyz" }""", value.GetType())!;
Console.WriteLine(value);
//method #1
static void tryDeserialize<T>(string json, out T value) => value = JsonSerializer.Deserialize<T>(json)!;
tryDeserialize("""{ "name": "abc" }""", out value);
Console.WriteLine(value);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment