Exemplo n.º 1
0
        public static object GetValue(this JsonElement json, Type type)
        {
            switch (Type.GetTypeCode(type))
            {
            case TypeCode.Boolean: return(json.GetBoolean());

            case TypeCode.SByte: return(json.GetSByte());

            case TypeCode.Int16: return(json.GetInt16());

            case TypeCode.Int32: return(json.GetInt32());

            case TypeCode.Int64: return(json.GetInt64());

            case TypeCode.Byte: return(json.GetByte());

            case TypeCode.UInt16: return(json.GetUInt16());

            case TypeCode.UInt32: return(json.GetUInt32());

            case TypeCode.UInt64: return(json.GetUInt64());

            case TypeCode.Single: return(json.GetSingle());

            case TypeCode.Double: return(json.GetDouble());

            case TypeCode.Decimal: return(json.GetDecimal());

            case TypeCode.String: return(json.GetString());

            case TypeCode.DateTime: return(json.GetDateTime());
            }
            return(Convert.ChangeType(json.GetRawText(), type));
        }
Exemplo n.º 2
0
        private static object WrapResultObject(JsonElement value)
        {
            switch (value.ValueKind)
            {
            case JsonValueKind.Array:
                return(value.EnumerateArray()
                       .Select(x => WrapResultObject(x))
                       .ToList());

            case JsonValueKind.Object:
                return(new DynamicJsonObject(value));

            case JsonValueKind.False:
            case JsonValueKind.True:
                return(value.GetBoolean());

            case JsonValueKind.String:
                return(value.GetString());

            case JsonValueKind.Number:
                return(value.GetInt64());

            case JsonValueKind.Null:
            case JsonValueKind.Undefined:
                return(null);

            default:
                return(value);
            }
        }
Exemplo n.º 3
0
    /// <summary>
    /// Sums the integer values in the specified JSON element, ignoring any values from
    /// child elements that contain the specified string value, if specified.
    /// </summary>
    /// <param name="element">The JSON element.</param>
    /// <param name="valueToIgnore">The elements to ignore if they contain this value.</param>
    /// <returns>The sum of the elements in <paramref name="element"/>.</returns>
    internal static long SumIntegerValues(JsonElement element, string valueToIgnore)
    {
        long sum = 0;

        if (element.ValueKind == JsonValueKind.Number)
        {
            sum = element.GetInt64();
        }
        else if (element.ValueKind == JsonValueKind.Array)
        {
            foreach (JsonElement child in element.EnumerateArray())
            {
                sum += SumIntegerValues(child, valueToIgnore);
            }
        }
        else if (element.ValueKind == JsonValueKind.Object)
        {
            foreach (JsonProperty child in element.EnumerateObject())
            {
                if (child.Value.ValueKind == JsonValueKind.String &&
                    child.Value.GetString() == valueToIgnore)
                {
                    return(0);
                }

                sum += SumIntegerValues(child.Value, valueToIgnore);
            }
        }

        return(sum);
    }
Exemplo n.º 4
0
        private object GetValue(JsonElement enumValue)
        {
            switch (enumValue.ValueKind)
            {
            case JsonValueKind.String: return(enumValue.GetString());

            case JsonValueKind.Number:
                if (enumValue.TryGetInt32(out Int32 intValue))
                {
                    return(enumValue.GetInt32());
                }
                if (enumValue.TryGetInt64(out Int64 LongValue))
                {
                    return(enumValue.GetInt64());
                }
                return(enumValue.GetDouble());

            case JsonValueKind.True:
            case JsonValueKind.False:
                return(enumValue.GetBoolean());

            case JsonValueKind.Null:
                return(null);

            default:
                return(enumValue.ToString());
            }
        }
Exemplo n.º 5
0
        public static void SetValueFromJson(this PropertyInfo property, Object obj, JsonElement value, string dateTimeFormat = null)
        {
            TypeCode typeCode = Type.GetTypeCode(property.PropertyType);

            switch (typeCode)
            {
            case TypeCode.Int32:
                int int32Value = value.GetInt32();
                property.SetValue(obj, int32Value);
                break;

            case TypeCode.Int64:
                long int64Value = value.GetInt64();
                property.SetValue(obj, int64Value);
                break;

            case TypeCode.Double:
                double doubleValue = value.GetDouble();
                property.SetValue(obj, doubleValue);
                break;

            case TypeCode.Decimal:
                decimal decimalValue = value.GetDecimal();
                property.SetValue(obj, decimalValue);
                break;

            case TypeCode.Boolean:
                bool boolValue = value.GetBoolean();
                property.SetValue(obj, boolValue);
                break;

            case TypeCode.DateTime:
                DateTime dateTimeValue;
                if (value.ValueKind == JsonValueKind.Null)
                {
                    dateTimeValue = DateTime.MinValue;
                }
                else if (!string.IsNullOrEmpty(dateTimeFormat))
                {
                    var stringDateTimeValue = value.GetString();
                    dateTimeValue = DateTime.ParseExact(stringDateTimeValue, dateTimeFormat, CultureInfo.InvariantCulture);
                }
                else
                {
                    dateTimeValue = value.GetDateTime();
                }

                property.SetValue(obj, dateTimeValue);
                break;

            case TypeCode.String:
                string stringValue = value.GetString();
                property.SetValue(obj, stringValue);
                break;

            default:
                //ThrowNotImplementedException(type); TODO: Сделать исключение.
                break;
            }
        }
Exemplo n.º 6
0
        public override void SerializePrimitive <T>(ref T val)
        {
            switch (val)
            {
            case bool:
                Unsafe.As <T, bool>(ref val) = currentNode.GetBoolean();
                break;

            case int:
                Unsafe.As <T, int>(ref val) = currentNode.GetInt32();
                break;

            case uint:
                Unsafe.As <T, uint>(ref val) = currentNode.GetUInt32();
                break;

            case float:
                Unsafe.As <T, float>(ref val) = currentNode.GetSingle();
                break;

            case double:
                Unsafe.As <T, double>(ref val) = currentNode.GetDouble();
                break;

            case long:
                Unsafe.As <T, long>(ref val) = currentNode.GetInt64();
                break;

            case ulong:
                Unsafe.As <T, ulong>(ref val) = currentNode.GetUInt64();
                break;

            case short:
                Unsafe.As <T, short>(ref val) = currentNode.GetInt16();
                break;

            case ushort:
                Unsafe.As <T, ushort>(ref val) = currentNode.GetUInt16();
                break;

            case char:
                Unsafe.As <T, char>(ref val) = (char)currentNode.GetUInt16();
                break;

            case sbyte:
                Unsafe.As <T, sbyte>(ref val) = currentNode.GetSByte();
                break;

            case byte:
                Unsafe.As <T, byte>(ref val) = currentNode.GetByte();
                break;

            case decimal:
                Unsafe.As <T, decimal>(ref val) = currentNode.GetDecimal();
                break;
            }
        }
Exemplo n.º 7
0
 public static object ToObject(this JsonElement element, ColumnType columnType)
 {
     return(columnType switch
     {
         ColumnType.Boolean => element.GetBoolean(),
         ColumnType.Decimal => element.GetDecimal(),
         ColumnType.Integer => element.GetInt64(),
         ColumnType.String => element.GetString(),
         ColumnType.DateTime => element.GetDateTime(),
         _ => throw new ArgumentException($"Unsupported columnType conversion [{columnType}]",
                                          nameof(columnType))
     });
 private static bool ValidateSimpleDataType(DataTypeEnum dataType, JsonElement prop, List <string> errors)
 {
     if (dataType == DataTypeEnum.Time)
     {
         return(CanConvertToTime(prop.GetString()));
     }
     else if (dataType == DataTypeEnum.DateTime || dataType == DataTypeEnum.Date)
     {
         return(CanConvertToDateTime(prop.GetString()));
     }
     else if (dataType == DataTypeEnum.Binary)
     {
         return(CanConvertToBase64(prop.GetString()));
     }
     else if (dataType == DataTypeEnum.GUID)
     {
         return(CanConvertToGuid(prop.GetString()));
     }
     else if (dataType == DataTypeEnum.Booelan)
     {
         return(CanConvertToBoolean(prop.GetString()));
     }
     else if (dataType == DataTypeEnum.String)
     {
         return(true);
     }
     else if (dataType == DataTypeEnum.Float)
     {
         try
         {
             prop.GetDouble();
             return(true);
         }
         catch
         {
             return(false);
         }
     }
     else if (dataType == DataTypeEnum.Integer)
     {
         try
         {
             prop.GetInt64();
             return(true);
         }
         catch
         {
             return(false);
         }
     }
     return(false);
 }
Exemplo n.º 9
0
 private long SumNumbers(JsonElement element, Func <JsonElement, bool> shouldSkipObject)
 {
     return(element.ValueKind switch
     {
         JsonValueKind.Undefined => 0,
         JsonValueKind.Object => shouldSkipObject(element) ? 0 : element.EnumerateObject().Sum(p => SumNumbers(p.Value, shouldSkipObject)),
         JsonValueKind.Array => element.EnumerateArray().Sum(e => SumNumbers(e, shouldSkipObject)),
         JsonValueKind.String => long.TryParse(element.GetString(), out long l) ? l : 0,
         JsonValueKind.Number => element.GetInt64(),
         JsonValueKind.True => 0,
         JsonValueKind.False => 0,
         JsonValueKind.Null => 0,
         _ => 0
     });
Exemplo n.º 10
0
        public Contributor(JsonElement contributor)
        {
            if (contributor.IsNull())
            {
                return;
            }

            if (contributor.ValueKind == JsonValueKind.Number)
            {
                ID = contributor.GetInt64().ToString();
            }
            else
            {
                ID         = contributor.GetLong("id_str").ToString();
                ScreenName = contributor.GetString("screen_name");
            }
        }
Exemplo n.º 11
0
        public override object?Invert(JsonElement value)
        {
            if (value.ValueKind == JsonValueKind.Null)
            {
                return(null);
            }

            return(columnType switch
            {
                DValueType.Bool => value.GetBoolean(),
                DValueType.Integer => value.GetInt64(),
                DValueType.Real => value.GetDouble(),
                DValueType.Date => value.GetDateTime(),
                DValueType.Datetime => value.GetDateTime(),
                DValueType.Text => value.GetString(),
                DValueType.Timestamp => value.GetDateTime(),
                DValueType.Unknown => value.GetString(),
                _ => throw new InvalidOperationException($"Unknown column type <{columnType}>"),
            });
        /// <summary>
        /// A potentially recursive function that deserializes a property from JSON to its .NET equivalent type.
        /// For properties that are class types, this will recurse and deserialize that type as well if there are any valid GraphQL properties
        /// </summary>
        /// <param name="value">The JSON element to deserialize</param>
        /// <param name="modelProperty">The destination model property</param>
        /// <param name="model">The model that owns the property being deserialized</param>
        public void DeserializeMember(JsonElement value, PropertyInfo modelProperty, object model)
        {
            if (modelProperty.PropertyType == typeof(string))
            {
                modelProperty.SetValue(model, value.GetString());
            }
            else if (modelProperty.PropertyType == typeof(double))
            {
                modelProperty.SetValue(model, value.GetDouble());
            }
            else if (modelProperty.PropertyType == typeof(short))
            {
                modelProperty.SetValue(model, value.GetInt16());
            }
            else if (modelProperty.PropertyType == typeof(int))
            {
                modelProperty.SetValue(model, value.GetInt32());
            }
            else if (modelProperty.PropertyType == typeof(long))
            {
                modelProperty.SetValue(model, value.GetInt64());
            }
            else if (modelProperty.PropertyType.IsEnum)
            {
                modelProperty.SetValue(model, Enum.Parse(modelProperty.PropertyType, value.GetRawText()));
            }
            else if (modelProperty.PropertyType.IsClass)
            {
                var properties = modelProperty.PropertyType
                                 .GetProperties()
                                 .Where(property => property.CustomAttributes.Any(attr => attr.AttributeType == typeof(GraphQLPropertyAttribute)))
                                 .ToList();

                foreach (var property in properties)
                {
                    var fieldName = property.GetCustomAttribute <GraphQLPropertyAttribute>().FieldName;
                    var propModel = Activator.CreateInstance(modelProperty.PropertyType);
                    modelProperty.SetValue(model, propModel);
                    DeserializeMember(value.GetProperty(fieldName), property, propModel);
                }
            }
        }
Exemplo n.º 13
0
        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
            });
        private static object ToObject(this JsonElement jsonElement)
        {
            switch (jsonElement.ValueKind)
            {
            case JsonValueKind.Null: return(null);

            case JsonValueKind.String: return(jsonElement.GetString());

            case JsonValueKind.Number: return(jsonElement.GetInt64());

            case JsonValueKind.True: return(true);

            case JsonValueKind.False: return(false);

            case JsonValueKind.Undefined:
            case JsonValueKind.Object:
            case JsonValueKind.Array:
            default:
                throw new NotSupportedException($"Unsupported ValueKind - {jsonElement.ValueKind}");
            }
        }
Exemplo n.º 15
0
        public async Task TestActorSettingsWithRemindersStoragePartitions()
        {
            var actorType = typeof(TestActor);

            var options = new ActorRuntimeOptions();

            options.Actors.RegisterActor <TestActor>();
            options.RemindersStoragePartitions = 12;

            var runtime = new ActorRuntime(options, loggerFactory, activatorFactory, proxyFactory);

            Assert.Contains(actorType.Name, runtime.RegisteredActors.Select(a => a.Type.ActorTypeName), StringComparer.InvariantCulture);

            ArrayBufferWriter <byte> writer = new ArrayBufferWriter <byte>();
            await runtime.SerializeSettingsAndRegisteredTypes(writer);

            // read back the serialized json
            var    array = writer.WrittenSpan.ToArray();
            string s     = Encoding.UTF8.GetString(array, 0, array.Length);

            JsonDocument document = JsonDocument.Parse(s);
            JsonElement  root     = document.RootElement;

            // parse out the entities array
            JsonElement element = root.GetProperty("entities");

            Assert.Equal(1, element.GetArrayLength());

            JsonElement arrayElement = element[0];
            string      actor        = arrayElement.GetString();

            Assert.Equal("TestActor", actor);

            element = root.GetProperty("remindersStoragePartitions");
            Assert.Equal(12, element.GetInt64());
        }
Exemplo n.º 16
0
 public override long Get(JsonElement property) => property.GetInt64();
Exemplo n.º 17
0
        private static object ParseValue(JsonElement jsonEl, Type type)
        {
            var valueKind  = jsonEl.ValueKind;
            var targetType = type;
            var isNullable = false;

            var nullableType = Nullable.GetUnderlyingType(type);

            if (nullableType != null)
            {
                targetType = nullableType;
                isNullable = true;
            }

            if (targetType.IsEnum)
            {
                targetType = Enum.GetUnderlyingType(targetType);
            }

            switch (valueKind)
            {
            case JsonValueKind.Array:
            case JsonValueKind.Null:
            case JsonValueKind.Undefined:
            case JsonValueKind.Object:
                return(isNullable ? null : targetType.GetDefault());

            case JsonValueKind.False:
            case JsonValueKind.True:
                return(jsonEl.GetBoolean());

            case JsonValueKind.String:
                return(targetType == typeof(string)
                        ? jsonEl.GetString()
                        : targetType == typeof(DateTime)
                        ? jsonEl.GetDateTime()
                        : targetType == typeof(Guid)
                        ? jsonEl.GetGuid()
                        : targetType.GetDefault());

            case JsonValueKind.Number:
                return(targetType == typeof(byte)
                        ? jsonEl.GetByte()
                        : targetType == typeof(decimal)
                        ? jsonEl.GetDecimal()
                        : targetType == typeof(double)
                        ? jsonEl.GetDouble()
                        : targetType == typeof(short)
                        ? jsonEl.GetInt16()
                        : targetType == typeof(int)
                        ? jsonEl.GetInt32()
                        : targetType == typeof(long)
                        ? jsonEl.GetInt64()
                        : targetType == typeof(sbyte)
                        ? jsonEl.GetSByte()
                        : targetType == typeof(float)
                        ? jsonEl.GetSingle()
                        : targetType == typeof(ushort)
                        ? jsonEl.GetUInt16()
                        : targetType == typeof(uint)
                        ? jsonEl.GetUInt32()
                        : targetType == typeof(ulong)
                        ? jsonEl.GetUInt64()
                        : targetType.GetDefault());

            default:
                return(targetType.GetDefault());
            }
        }
Exemplo n.º 18
0
        public static object ToObject(this JsonElement element, Type type)
        {
            object result = null;

            var nonNullType = Nullable.GetUnderlyingType(type);

            if (nonNullType != null)
            {
                type = nonNullType;
            }

            switch (Type.GetTypeCode(type))
            {
            case TypeCode.Boolean:
                result = element.GetBoolean();
                break;

            case TypeCode.Byte:
                result = element.GetByte();
                break;

            case TypeCode.SByte:
                result = element.GetSByte();
                break;

            case TypeCode.Int16:
                result = element.GetInt16();
                break;

            case TypeCode.UInt16:
                result = element.GetUInt16();
                break;

            case TypeCode.Int32:
                result = element.GetInt32();
                break;

            case TypeCode.UInt32:
                result = element.GetUInt32();
                break;

            case TypeCode.Int64:
                result = element.GetInt64();
                break;

            case TypeCode.UInt64:
                result = element.GetUInt64();
                break;

            case TypeCode.Single:
                result = element.GetSingle();
                break;

            case TypeCode.Double:
                result = element.GetDouble();
                break;

            case TypeCode.Decimal:
                result = element.GetDecimal();
                break;

            case TypeCode.DateTime:
                result = element.GetDateTime();
                break;

            case TypeCode.String:
                result = element.GetString();
                break;

            default:
                if (type == typeof(Guid))
                {
                    result = element.GetGuid();
                    break;
                }

                throw new NotImplementedException();
            }

            if (nonNullType != null)
            {
                return(result.ToNullable());
            }

            return(result);
        }
Exemplo n.º 19
0
        public dynamic Objectify(JsonElement graphsonObject, GraphSONReader reader)
        {
            var milliseconds = graphsonObject.GetInt64();

            return(UnixStart.AddTicks(TimeSpan.TicksPerMillisecond * milliseconds));
        }
Exemplo n.º 20
0
        private long DeserializeLong(JsonElement obj, string fieldName)
        {
            JsonElement value = obj.GetProperty(fieldName);

            return((long)_longSerializer.Deserialize(value.GetInt64()));
        }
Exemplo n.º 21
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));
     }
 }
Exemplo n.º 22
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());
            }
        }
Exemplo n.º 23
0
 public static long ParseInt64(this JsonElement el)
 {
     return(el.ValueKind == JsonValueKind.String
         ? long.Parse(el.GetString())
         : el.GetInt64());
 }
Exemplo n.º 24
0
 protected override dynamic FromJsonElement(JsonElement graphson) => graphson.GetInt64();
Exemplo n.º 25
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));
            }
        }
Exemplo n.º 26
0
   static object UnboxJson(JsonElement value)
   => value.ValueKind == JsonValueKind.String ? value.GetString()
 : value.ValueKind == JsonValueKind.Number ? value.GetInt64()
 : (object)value;
Exemplo n.º 27
0
 public override object Create(JsonElement reader, Type type, JsonSerializer serializer)
 {
     return(Enum.ToObject(type, reader.GetInt64()));
 }
Exemplo n.º 28
0
 public static object GetValue(JsonElement jsonElement)
 {
     if (_columnDataType == typeof(DateTime))
     {
         return(jsonElement.GetDateTime());
     }
     else if (_columnDataType.IsEnum)
     {
         if (jsonElement.ValueKind == JsonValueKind.Number)
         {
             return(Enum.Parse(THelper.GetUnderlyingType <TValue>(), jsonElement.GetInt32().ToString()));
         }
         else
         {
             return(Enum.Parse(THelper.GetUnderlyingType <TValue>(), jsonElement.GetString()));
         }
     }
     else if (_columnDataType == typeof(byte))
     {
         return(jsonElement.GetByte());
     }
     else if (_columnDataType == typeof(decimal))
     {
         return(jsonElement.GetDecimal());
     }
     else if (_columnDataType == typeof(double))
     {
         return(jsonElement.GetDouble());
     }
     else if (_columnDataType == typeof(short))
     {
         return(jsonElement.GetInt16());
     }
     else if (_columnDataType == typeof(int))
     {
         return(jsonElement.GetInt32());
     }
     else if (_columnDataType == typeof(long))
     {
         return(jsonElement.GetInt64());
     }
     else if (_columnDataType == typeof(sbyte))
     {
         return(jsonElement.GetSByte());
     }
     else if (_columnDataType == typeof(float))
     {
         return(jsonElement.GetSingle());
     }
     else if (_columnDataType == typeof(ushort))
     {
         return(jsonElement.GetUInt16());
     }
     else if (_columnDataType == typeof(uint))
     {
         return(jsonElement.GetUInt32());
     }
     else if (_columnDataType == typeof(ulong))
     {
         return(jsonElement.GetUInt64());
     }
     else if (_columnDataType == typeof(Guid))
     {
         return(jsonElement.GetGuid());
     }
     else if (_columnDataType == typeof(bool))
     {
         if (jsonElement.ValueKind == JsonValueKind.True)
         {
             return(true);
         }
         else
         {
             return(false);
         }
     }
     else
     {
         return(jsonElement.GetString());
     }
 }