Skip to content

Instantly share code, notes, and snippets.

View mishrsud's full-sized avatar
🎯
Focusing

Sudhanshu Mishra mishrsud

🎯
Focusing
View GitHub Profile
@mishrsud
mishrsud / JsonSerializationExtensions.cs
Created May 28, 2021 12:48
Snake Case Serilization with System.Text.Json
using System;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Infrastructure
{
public static class JsonSerializationExtensions
{
public static readonly JsonSerializerOptions SnakeCaseSerializerOptions = new()
@mishrsud
mishrsud / JsonSerializationExtensions.cs
Created May 28, 2021 12:43
Customised Serialization with System.Text.Json
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Infrastructure
{
public static class JsonSerializationExtensions
{
public static JsonSerializerOptions DefaultSerializerOptions = new JsonSerializerOptions
{
WriteIndented = false,
void Main()
{
// 1: NOT QUITE IMMUTABLE
var mutablePerson = new MutablePerson();
mutablePerson.Name = "Clint";
mutablePerson.Age = 41;
mutablePerson.Dump("Initial Values");
mutablePerson.Name = "Jerry";
internal static class HttpClientExtensions
{
public static Task<HttpResponseMessage> PostAsJsonAsync<T>(this HttpClient client, string uri, T content, string messageId = null)
=> SendAsJsonAsync<T>(client, HttpMethod.Post, uri, content, messageId);
public static Task<HttpResponseMessage> PutAsJsonAsync<T>(this HttpClient client, string uri, T content)
=> SendAsJsonAsync<T>(client, HttpMethod.Put, uri, content, null);
public static Task<HttpResponseMessage> GetAsync(this HttpClient client, string uri)
{
// https://docs.microsoft.com/en-us/dotnet/api/system.timezoneinfo.createcustomtimezone?view=netframework-4.7.2
void Main()
{
TimeZoneInfo
.ConvertTime(
DateTimeOffset.Parse("2019-02-18 19:51 -05:00", CultureInfo.InvariantCulture),
TimeZoneInfo.FindSystemTimeZoneById("AUS Eastern Standard Time")).Dump("Sydney Time");
TimeZoneInfo
.ConvertTime(
DateTimeOffset.Parse("2019-02-18 19:51 -05:00", CultureInfo.InvariantCulture),
// Great explanation on Stackoverflow: https://stackoverflow.com/a/1710395/190476
void Main()
{
Predicate<Person> personWithId1 = person => person.Id == 1;
Predicate<Person> personsOver30 = person => person.Age > 30;
var persons = new List<Person>
{
new Person {
@mishrsud
mishrsud / ToUniversalTimeIsntReliable.cs
Created February 7, 2019 07:10
ToUniversalTime() Isn't Reliable, always use TimzoneInfo.ConvertXXX methods
public class Program
{
public static void Main()
{
Console.WriteLine(TimeZoneInfo.ConvertTimeToUtc(new DateTime(2019,2,2, 0,0,0), TimeZoneInfo.FindSystemTimeZoneById("AUS Eastern Standard Time")));
Console.WriteLine(TimeZoneInfo.ConvertTimeToUtc(new DateTime(2019,2,2, 23,59,59), TimeZoneInfo.FindSystemTimeZoneById("AUS Eastern Standard Time")));
Console.WriteLine(new DateTime(
2019,
2,
2,
@mishrsud
mishrsud / genrate-git-version.sh
Created January 17, 2019 04:39
Use gitversion as a docker container on a Linux build agent in Azure Devops (erstwhile VSTS)
# Docker container: gittools/gitversion-dotnetcore:linux-4.0.1
# NugetPackageVersion is a variable defined for the Azure DevOps pipeline
NEXTVERSION="$(docker run --rm -v "$(pwd):$BUILD_SOURCESDIRECTORY/path-to-repo" gittools/gitversion-dotnetcore:linux-4.0.1 $BUILD_SOURCESDIRECTORY/path-to-repo /output json /showvariable MajorMinorPatch)"
# the following command sets the pipeline variable's value to NEXTVERSION
echo "##vso[task.setvariable variable=NugetPackageVersion]$NEXTVERSION"
/*
* Use an approach similar to ASP.NET core for generic host in a console application
*/
public interface IStartup
{
void ConfigureServices(
HostBuilderContext hostBuilderContext,
IServiceCollection serviceCollection);
@mishrsud
mishrsud / UtcSydneyDateFormatConverter.cs
Last active November 21, 2018 10:54
A Date format converter that converts date and time consistently irrespective of the hosting platform or daylight savings
using System;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
public class UtcSydneyDateFormatConverter : IsoDateTimeConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
DateTimeStyles = DateTimeStyles.AdjustToUniversal;