Beispiel #1
0
        private object BindType(JsonElement jsonObject, Type type)
        {
            if (type == typeof(DateTime))
            {
                return(jsonObject.GetDateTime());
            }
            else if (type == typeof(DateTimeOffset))
            {
                return(jsonObject.GetDateTimeOffset());
            }

            return(JsonSerializer.Parse(jsonObject.GetRawText(), type, _payloadSerializerOptions));
        }
        private object BindType(JsonElement jsonObject, Type type)
        {
            if (type == typeof(DateTime))
            {
                return(jsonObject.GetDateTime());
            }
            else if (type == typeof(DateTimeOffset))
            {
                return(jsonObject.GetDateTimeOffset());
            }

            if (jsonObject.Type == JsonValueType.Null)
            {
                return(null);
            }
            return(JsonSerializer.Parse(jsonObject.GetRawText(), type));
        }
        public static object ConsumeJsonElement(Type targetReturnType, JsonElement e)
        {
            // ReSharper disable once HeapView.BoxingAllocation
            object ParseNumber(JsonElement el)
            {
                if (el.TryGetInt64(out var l))
                {
                    return(l);
                }

                return(el.GetDouble());
            }

            return(targetReturnType switch
            {
                _ when targetReturnType == typeof(bool) => e.GetBoolean(),
                _ when targetReturnType == typeof(byte) => e.GetByte(),
                _ when targetReturnType == typeof(decimal) => e.GetDecimal(),
                _ when targetReturnType == typeof(double) => e.GetDouble(),
                _ when targetReturnType == typeof(Guid) => e.GetGuid(),
                _ when targetReturnType == typeof(short) => e.GetInt16(),
                _ when targetReturnType == typeof(int) => e.GetInt32(),
                _ when targetReturnType == typeof(long) => e.GetInt64(),
                _ when targetReturnType == typeof(float) => e.GetSingle(),
                _ when targetReturnType == typeof(string) => e.GetString(),
                _ when targetReturnType == typeof(DateTime) => e.GetDateTime(),
                _ when targetReturnType == typeof(DateTimeOffset) => e.GetDateTimeOffset(),
                _ when targetReturnType == typeof(ushort) => e.GetUInt16(),
                _ when targetReturnType == typeof(uint) => e.GetUInt32(),
                _ when targetReturnType == typeof(ulong) => e.GetUInt64(),
                _ when targetReturnType == typeof(sbyte) => e.GetSByte(),
                _ when targetReturnType == typeof(DynamicDictionary) => DynamicDictionary.Create(e),
                _ when targetReturnType == typeof(object) && e.ValueKind == JsonValueKind.Array =>
                e.EnumerateArray().Select(je => ConsumeJsonElement(targetReturnType, je)).ToArray(),
                _ when targetReturnType == typeof(object) && e.ValueKind == JsonValueKind.Object => e.ToDictionary(),
                _ when targetReturnType == typeof(object) && e.ValueKind == JsonValueKind.True => true,
                _ when targetReturnType == typeof(object) && e.ValueKind == JsonValueKind.False => false,
                _ when targetReturnType == typeof(object) && e.ValueKind == JsonValueKind.Null => null,
                _ when targetReturnType == typeof(object) && e.ValueKind == JsonValueKind.String => e.GetString(),
                _ when targetReturnType == typeof(object) && e.ValueKind == JsonValueKind.Number => ParseNumber(e),
                _ => null
            });
Beispiel #4
0
 public DateTimeOffset?GetDateTimeOffsetValue() => _jsonNode.GetDateTimeOffset();
Beispiel #5
0
 private static object DeserializeJsonValue(Type type, JsonElement value)
 {
     if (value.ValueKind == JsonValueKind.Null &&
         (type == typeof(string) || Nullable.GetUnderlyingType(type) != null))
     {
         return(null);
     }
     else if (type == typeof(bool) || type == typeof(bool?))
     {
         return(value.GetBoolean());
     }
     else if (type == typeof(byte) || type == typeof(byte?))
     {
         return(value.GetByte());
     }
     else if (type == typeof(short) || type == typeof(short?))
     {
         return(value.GetInt16());
     }
     else if (type == typeof(ushort) || type == typeof(ushort?))
     {
         return(value.GetUInt16());
     }
     else if (type == typeof(int) || type == typeof(int?))
     {
         return(value.GetInt32());
     }
     else if (type == typeof(uint) || type == typeof(uint?))
     {
         return(value.GetUInt32());
     }
     else if (type == typeof(long) || type == typeof(long?))
     {
         return(value.GetInt64());
     }
     else if (type == typeof(ulong) || type == typeof(ulong?))
     {
         return(value.GetUInt64());
     }
     else if (type == typeof(float) || type == typeof(float?))
     {
         return(value.GetSingle());
     }
     else if (type == typeof(double) || type == typeof(double?))
     {
         return(value.GetDouble());
     }
     else if (type == typeof(decimal) || type == typeof(decimal?))
     {
         return(value.GetDecimal());
     }
     else if (type == typeof(DateTime) || type == typeof(DateTime?))
     {
         return(value.GetDateTime());
     }
     else if (type == typeof(DateTimeOffset) || type == typeof(DateTimeOffset?))
     {
         return(value.GetDateTimeOffset());
     }
     else if (type == typeof(TimeSpan) || type == typeof(TimeSpan?))
     {
         return(TimeSpan.Parse(value.GetString()));
     }
     else if (type == typeof(string))
     {
         return(value.GetString());
     }
     else
     {
         return(JsonSerializer.Deserialize(value.GetRawText(), type));
     }
 }
Beispiel #6
0
        protected void AssertJsonValue(JsonElement expected, object actual)
        {
            bool switched = true;

            switch (actual)
            {
            case Guid guid:
                Assert.Equal(new Guid(expected.GetString()), guid);
                break;

            case DateTime dateTime:
                Assert.Equal(expected.GetDateTime(), dateTime);
                break;

            case DateTimeOffset dateTime:
                Assert.Equal(expected.GetDateTimeOffset(), dateTime);
                break;

            case TimeSpan timeSpan:
                Assert.Equal(TimeSpan.FromSeconds(expected.GetInt32()), timeSpan);
                break;

            case int @int:
                if (expected.ValueKind == JsonValueKind.String)
                {
                    Assert.Equal(int.Parse(expected.GetString()), @int);
                }
                else
                {
                    Assert.Equal(expected.GetInt32(), @int);
                }
                break;

            case long @long:
                if (expected.ValueKind == JsonValueKind.String)
                {
                    Assert.Equal(long.Parse(expected.GetString()), @long);
                }
                else
                {
                    Assert.Equal(expected.GetInt64(), @long);
                }
                break;

            case double @double:
                Assert.Equal(expected.GetDouble(), @double, 10);
                break;

            case bool @bool:
                Assert.Equal(expected.GetBoolean(), @bool);
                break;

            case string @string:
                Assert.Equal(expected.GetString(), @string);
                break;

            case null:
                if (expected.ValueKind == JsonValueKind.String)
                {
                    Assert.Equal(expected.GetString(), string.Empty);
                }
                break;

            default:
                switched = false;
                break;
            }

            if (!switched)
            {
                var type = actual !.GetType();
                if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(ApiEnum <>))
                {
                    var     enumType = type.GenericTypeArguments[0];
                    dynamic @enum    = actual; // Just for easiness

                    Assert.Equal(expected.GetString(), @enum.RawValue);
                    if (@enum.IsUnknown)
                    {
                        var enumNames = Enum.GetNames(enumType)
                                        .Select(x => enumType.GetField(x).GetCustomAttribute <EnumMemberAttribute>()?.Value ?? x);
                        Assert.True(enumNames.Contains((string)@enum.RawValue, StringComparer.OrdinalIgnoreCase), $"Expected '{expected}' to be a value in enumerator {@enum.Value.GetType().FullName}; detected value '{@enum.Value}'");
                    }

                    dynamic expectedEnum = Activator.CreateInstance(type, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public, null, new[] { expected.GetString() }, null);
                    Assert.Equal(expectedEnum.Value, @enum.Value);

                    switched = true;
                }
            }

            if (!switched)
            {
                Assert.Equal(expected.GetString(), actual !.ToString());
            }
        }
Beispiel #7
0
        private static object WithValue(this PropertyInfo property, JsonElement value)
        {
            var propType = property.PropertyType;

            if (value.ValueKind == JsonValueKind.Null)
            {
                return(propType.GetDefaultValue());
            }
            else if (typeof(bool) == propType)
            {
                return(value.GetBoolean());
            }
            else if (typeof(int) == propType)
            {
                return(value.GetInt32());
            }
            else if (typeof(uint) == propType)
            {
                return(value.GetUInt32());
            }
            else if (typeof(short) == propType)
            {
                return(value.GetInt16());
            }
            else if (typeof(ushort) == propType)
            {
                return(value.GetUInt16());
            }
            else if (typeof(long) == propType)
            {
                return(value.GetInt64());
            }
            else if (typeof(ulong) == propType)
            {
                return(value.GetUInt64());
            }
            else if (typeof(string) == propType)
            {
                return(value.GetString());
            }
            else if (typeof(byte) == propType)
            {
                return(value.GetByte());
            }
            else if (typeof(sbyte) == propType)
            {
                return(value.GetSByte());
            }
            else if (typeof(DateTime) == propType)
            {
                return(value.GetDateTime());
            }
            else if (typeof(DateTimeOffset) == propType)
            {
                return(value.GetDateTimeOffset());
            }
            else if (typeof(double) == propType)
            {
                return(value.GetDouble());
            }
            else if (typeof(float) == propType)
            {
                return(value.GetSingle());
            }
            else if (typeof(Guid) == propType)
            {
                return(value.GetGuid());
            }
            else if (typeof(decimal) == propType)
            {
                return(value.GetDecimal());
            }
            else if (value.GetType() == propType)
            {
                return(value);
            }
            else
            {
                return(value.ToObject(propType));
            }
        }