Skip to content

Instantly share code, notes, and snippets.

View wipiano's full-sized avatar
🏠
Working from home

Kohei Hakoishi wipiano

🏠
Working from home
View GitHub Profile
@wipiano
wipiano / FormatJsonString.cs
Last active March 15, 2024 05:05
format arbitrary JSON string with System.Text.Json
string json = """
{ "X": 123, "Y": { "Y1": 1, "Y2": 2 } }
""";
JsonSerializerOptions options = new(JsonSerializerDefaults.General) { WriteIndented = true };
string formatted = JsonSerializer.Serialize(JsonSerializer.Deserialize<JsonElement>(json), options);
Console.WriteLine(formatted);
/* output:
{
"X": 123,
"Y": {
// <auto-generated/>
#nullable enable
#pragma warning disable CS0612, CS0618 // Suppress warnings about [Obsolete] member usage in generated code.
namespace System.Runtime.CompilerServices
{
using System;
using System.CodeDom.Compiler;
[GeneratedCode("Microsoft.Extensions.Configuration.Binder.SourceGeneration", "8.0.8.47906")]
@wipiano
wipiano / assume-role.sh
Created September 26, 2022 05:56
aws cli で assume role する bash スクリプト
#! /bin/bash
roleArn='arn:aws:iam::xxxx:role/xxxx'
serialNumber='arn:aws:iam::xxxx:mfa/xxxx'
assumerole() {
read -p "Enter mfa code: " mfacode
d=`date "+%Y%m%d%H%M%S"`
aws sts assume-role --role-arn $roleArn --serial-number $serialNumber --role-session-name cli${date} --token-code ${mfacode}
}
@wipiano
wipiano / Program.cs
Created August 12, 2022 00:46
techradar
// See https://aka.ms/new-console-template for more information
using System.Text;
using CodeHollow.FeedReader;
const int readCountPerFeed = 10;
string[] sites = new[]
{
"https://feed.infoq.com/",
@wipiano
wipiano / Directory.Build.props
Created February 23, 2022 11:40
my .NET 6 Directory.Build.props
<Project>
<PropertyGroup>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>latest</LangVersion>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<AnalysisMode>All</AnalysisMode>
<CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>
@wipiano
wipiano / .editorconfig
Created February 23, 2022 11:39
.NET 6 editorconfig
# EditorConfig is awesome: https://EditorConfig.org
# top-most EditorConfig file
root = true
# Don't use tabs for indentation.
[*]
indent_style = space
# (Please don't specify an indent_size here; that has too many unintended consequences.)
@wipiano
wipiano / FreezableObjectAttributes.cs
Last active July 6, 2021 05:24
SourceGenerator で実装された PocoFreezer がほしい
public class FreezableObjectAttribute : Attribute
{
public bool GenerateRecord { get; set; }
}
public class PocoFreezerGenerated : Attribute{}
@wipiano
wipiano / Program.Generated.cs
Created March 11, 2021 07:03
async stream の生成結果
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
using System.Security;
using System.Security.Permissions;
using System.Threading;
@wipiano
wipiano / Program.cs
Last active October 19, 2020 14:40
コラッツ予想
using System;
using System.Numerics;
namespace Collatz
{
class Program
{
static void Main(string[] args)
{
var n = new BigInteger(2);
@wipiano
wipiano / MessagePackSerializerEx.cs
Last active March 30, 2020 03:31
lazy msgpack deserializer to read large object array
public static class MessagePackSerializerEx
{
public static IEnumerable<T> LazyDeserializeArray<T>(ReadOnlyMemory<byte> bytes,
MessagePackSerializerOptions? options = null)
{
options ??= MessagePackSerializer.DefaultOptions;
if (options.Compression != MessagePackCompression.None)
{
throw new InvalidOperationException("lz4 圧縮には対応できません");