コード例 #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));
        }
コード例 #2
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;
            }
        }
コード例 #3
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
            });
コード例 #4
0
 public override byte Get(JsonElement property) => property.GetByte();
コード例 #5
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);
        }
コード例 #6
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));
     }
 }
コード例 #7
0
ファイル: Extensions.cs プロジェクト: smapiot/Piral.Blazor
        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));
            }
        }
コード例 #8
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());
            }
        }
コード例 #9
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());
     }
 }
コード例 #10
0
 protected override dynamic FromJsonElement(JsonElement graphson) => graphson.GetByte();