Convert object to System.Text.Json.JsonElement

In .NET 6 methods are being added to JsonSerializer to serialize an object directly to a JsonElement or JsonDocument: public static partial class JsonSerializer { public static JsonDocument SerializeToDocument<TValue>(TValue value, JsonSerializerOptions? options = null); public static JsonDocument SerializeToDocument(object? value, Type inputType, JsonSerializerOptions? options = null); public static JsonDocument SerializeToDocument<TValue>(TValue value, JsonTypeInfo<TValue> jsonTypeInfo); public static JsonDocument … Read more

System.Text.Json – Deserialize nested object as string

Found a right way how to correctly read the nested JSON object inside the JsonConverter. The complete solution is the following: public class SomeModel { public int Id { get; set; } public string Name { get; set; } [JsonConverter(typeof(InfoToStringConverter))] public string Info { get; set; } } public class InfoToStringConverter : JsonConverter<string> { public … Read more

Is there a built in way of using snake case as the naming policy for JSON in ASP.NET Core 3?

Just slight modification in pfx code to remove the dependency on Newtonsoft Json.Net. String extension method to convert the given string to SnakeCase. public static class StringUtils { public static string ToSnakeCase(this string str) { return string.Concat(str.Select((x, i) => i > 0 && char.IsUpper(x) ? “_” + x.ToString() : x.ToString())).ToLower(); } } Then in our … Read more

Parsing a JSON file with .NET core 3.0/System.text.Json

Update 2019-10-13: Rewritten the Utf8JsonStreamReader to use ReadOnlySequences internally, added wrapper for JsonSerializer.Deserialize method. I have created a wrapper around Utf8JsonReader for exactly this purpose: public ref struct Utf8JsonStreamReader { private readonly Stream _stream; private readonly int _bufferSize; private SequenceSegment? _firstSegment; private int _firstSegmentStartIndex; private SequenceSegment? _lastSegment; private int _lastSegmentEndIndex; private Utf8JsonReader _jsonReader; private bool … Read more

JsonConverter equivalent in using System.Text.Json

System.Text.Json now supports custom type converters in .NET 3.0 preview-7 and above. You can add converters that match on type, and use the JsonConverter attribute to use a specific converter for a property. Here’s an example to convert between long and string (because javascript doesn’t support 64-bit integers). public class LongToStringConverter : JsonConverter<long> { public … Read more

JsonConverter equivalent in using System.Text.Json

System.Text.Json now supports custom type converters in .NET 3.0 preview-7 and above. You can add converters that match on type, and use the JsonConverter attribute to use a specific converter for a property. Here’s an example to convert between long and string (because javascript doesn’t support 64-bit integers). public class LongToStringConverter : JsonConverter<long> { public … Read more

Ignore property when null using the new Net Core 3.0 Json

I’m looking at .Net Core 3.1, where this should ignore null values services.AddControllers().AddJsonOptions(options => { options.JsonSerializerOptions.IgnoreNullValues = true; }); While in .NET 5 and later, set JsonSerializerOptions.DefaultIgnoreCondition to JsonIgnoreCondition.WhenWritingNull: services.AddControllers().AddJsonOptions(options => { options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; }); Note, the above isn’t per property/attribute, although there is an attribute which may be helpful JsonIgnoreAttribute. An alternative, to … Read more

Equivalent of JObject in System.Text.Json

As of Nov 2021, .NET 6 introduces the System.Text.Json.Nodes namespace which: Provides types for handling an in-memory writeable document object model (DOM) for random access of the JSON elements within a structured view of the data The four new types are JsonArray, JsonObject, JsonNode and JsonValue. The closest type to JObject is JsonObject which offers … Read more

System.Text.Json: Deserialize JSON with automatic casting

Edit: You can use JsonNumberHandlingAttribute and it handles everything correctly in 1 line, no need to write any code: [JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)] public string Id { get; set; } …. Original answer: The new System.Text.Json api exposes a JsonConverter api which allows us to convert the type as we like. For example, we can create a generic … Read more