Example #1
0
        public static JsonSerializerOptions BuildDefaultSettings(bool formatted = false)
        {
            var options = new JsonSerializerOptions
            {
                IgnoreNullValues            = true,
                WriteIndented               = formatted,
                PropertyNameCaseInsensitive = true,
            };

            options.Converters.Add(new CustomJsonStringEnumConverter());
            options.AddStructConverter <Date>((w, v) => w.WriteStringValue(v.ToCharSpan()), (ref Utf8JsonReader r) => r.GetString());
            options.AddStructConverter <MonthDayYear>((w, v) => w.WriteStringValue(v.ToCharSpan()), (ref Utf8JsonReader r) => r.GetString());
            options.AddStructConverter <Currency>((w, v) => w.WriteStringValue(v.ToString()), (ref Utf8JsonReader r) => r.GetString() ?? ZERO);
            options.AddStructConverter((w, v) => w.WriteStringValue(v.ToString()), (ref Utf8JsonReader r) => TimeSpan.Parse(r.GetString()));

            //string to number handlers... because it doesn't handle numbers encased in quotes by default
            options.AddStructConverter((w, v) => w.WriteNumberValue(v), (ref Utf8JsonReader r) => r.TokenType == NUMBER && r.TryGetByte(out var d) || byte.TryParse(r.GetString() ?? ZERO, out d) ? d : default);
Example #2
0
        public static JsonSerializerOptions BuildDefaultSettings(bool formatted = false)
        {
            var options = new JsonSerializerOptions();

            options.IgnoreNullValues            = true;
            options.WriteIndented               = formatted;
            options.PropertyNameCaseInsensitive = true;
            options.AddStructConverter <Date>((w, v) => w.WriteStringValue(v.ToCharSpan()), (ref Utf8JsonReader r) => r.GetString());
            options.AddStructConverter <MonthDayYear>((w, v) => w.WriteStringValue(v.ToCharSpan()), (ref Utf8JsonReader r) => r.GetString());

            options.AddStructConverter((w, v) => w.WriteNumberValue(v), (ref Utf8JsonReader r) => r.TokenType == JsonTokenType.Number && r.TryGetByte(out var d) ? d : byte.Parse(r.GetString()));
            options.AddStructConverter((w, v) => w.WriteNumberValue(v), (ref Utf8JsonReader r) => r.TokenType == JsonTokenType.Number && r.TryGetSByte(out var d) ? d : sbyte.Parse(r.GetString()));
            options.AddStructConverter((w, v) => w.WriteNumberValue(v), (ref Utf8JsonReader r) => r.TokenType == JsonTokenType.Number && r.TryGetInt16(out var d) ? d : short.Parse(r.GetString()));
            options.AddStructConverter((w, v) => w.WriteNumberValue(v), (ref Utf8JsonReader r) => r.TokenType == JsonTokenType.Number && r.TryGetUInt16(out var d) ? d : ushort.Parse(r.GetString()));
            options.AddStructConverter((w, v) => w.WriteNumberValue(v), (ref Utf8JsonReader r) => r.TokenType == JsonTokenType.Number && r.TryGetInt32(out var d) ? d : int.Parse(r.GetString()));
            options.AddStructConverter((w, v) => w.WriteNumberValue(v), (ref Utf8JsonReader r) => r.TokenType == JsonTokenType.Number && r.TryGetUInt32(out var d) ? d : uint.Parse(r.GetString()));
            options.AddStructConverter((w, v) => w.WriteNumberValue(v), (ref Utf8JsonReader r) => r.TokenType == JsonTokenType.Number && r.TryGetInt64(out var d) ? d : long.Parse(r.GetString()));
            options.AddStructConverter((w, v) => w.WriteNumberValue(v), (ref Utf8JsonReader r) => r.TokenType == JsonTokenType.Number && r.TryGetUInt64(out var d) ? d : ulong.Parse(r.GetString()));
            options.AddStructConverter((w, v) => w.WriteNumberValue(v), (ref Utf8JsonReader r) => r.TokenType == JsonTokenType.Number && r.TryGetSingle(out var d) ? d : float.Parse(r.GetString()));
            options.AddStructConverter((w, v) => w.WriteNumberValue(v), (ref Utf8JsonReader r) => r.TokenType == JsonTokenType.Number && r.TryGetDouble(out var d) ? d : double.Parse(r.GetString()));
            options.AddStructConverter((w, v) => w.WriteNumberValue(v), (ref Utf8JsonReader r) => r.TokenType == JsonTokenType.Number && r.TryGetDecimal(out var d) ? d : r.GetString().ToDecimal());

            //options.MissingMemberHandling = MissingMemberHandling.Error;
            options.AddClassConverter(e => e.ToString(), (s, t) =>
            {
                if (s.IsNullOrWhiteSpace())
                {
                    return((Enum)Activator.CreateInstance(t));
                }
                if (Enum.TryParse(t, s, true, out var result))
                {
                    return((Enum)result);
                }
                var clean = new string(s.SkipWhile(c => !c.IsLetter()).Where(c => char.IsLetterOrDigit(c) || c == '_').ToArray());
                if (Enum.TryParse(t, clean, true, out result))
                {
                    return((Enum)result);
                }
                throw new Exception($"Invalid enum - Value: {s}, Type: {t.FullName}");
            });
            return(options);
        }