예제 #1
0
        Value HandleValue()
        {
            switch (reader.TokenType)
            {
            case JsonToken.StartObject:
                return(ReadObject());

            case JsonToken.StartArray:
                return(ReadArray());

            case JsonToken.Integer:
                return(LongV.Of((long)reader.Value));

            case JsonToken.Float:
                return(DoubleV.Of((double)reader.Value));

            case JsonToken.String:
                return(StringV.Of((string)reader.Value));

            case JsonToken.Boolean:
                return(BooleanV.Of((bool)reader.Value));

            case JsonToken.Null:
                return(NullV.Instance);

            default:
                return(Unexpected());
            }
        }
예제 #2
0
        Value EncodeIntern(object obj)
        {
            var type = obj.GetType();

            switch (Type.GetTypeCode(type))
            {
            case TypeCode.String:
                return(StringV.Of((string)obj));

            case TypeCode.Boolean:
                return(BooleanV.Of((bool)obj));

            case TypeCode.DateTime:
                return(WrapDateTime((DateTime)obj));

            case TypeCode.SByte:
            case TypeCode.Int16:
            case TypeCode.Int32:
            case TypeCode.Int64:
                return(LongV.Of(Convert.ToInt64(obj)));

            case TypeCode.Char:
            case TypeCode.Byte:
            case TypeCode.UInt16:
            case TypeCode.UInt32:
            case TypeCode.UInt64:
                return(LongV.Of(Convert.ToInt64(obj)));

            case TypeCode.Single:
            case TypeCode.Double:
            case TypeCode.Decimal:
                return(DoubleV.Of(Convert.ToDouble(obj)));

            case TypeCode.DBNull:
            case TypeCode.Empty:
                return(NullV.Instance);

            case TypeCode.Object:
                if (typeof(byte[]).IsInstanceOfType(obj))
                {
                    return(new BytesV((byte[])obj));
                }

                if (typeof(IDictionary).IsInstanceOfType(obj))
                {
                    return(WrapDictionary((IDictionary)obj));
                }

                if (typeof(IEnumerable).IsInstanceOfType(obj))
                {
                    return(WrapEnumerable((IEnumerable)obj));
                }

                return(WrapObj(obj));
            }

            throw new InvalidOperationException($"Unsupported type {type} at `{obj}`");
        }
예제 #3
0
        Value EncodeIntern(object obj, Type forceType)
        {
            var type = obj.GetType();

            if (forceType == typeof(StringV))
            {
                return(StringV.Of(obj.ToString()));
            }

            if (dbNullValue.Equals(obj))
            {
                return(NullV.Instance);
            }

            if (type.GetTypeInfo().IsEnum)
            {
                return(WrapEnum(obj, type));
            }

            switch (Type.GetTypeCode(type))
            {
            case TypeCode.String:
                return(StringV.Of((string)obj));

            case TypeCode.Boolean:
                return(BooleanV.Of((bool)obj));

            case TypeCode.DateTime:
                return(Value.FromDateTime((DateTime)obj, forceType));

            case TypeCode.SByte:
            case TypeCode.Int16:
            case TypeCode.Int32:
            case TypeCode.Int64:
                return(LongV.Of(Convert.ToInt64(obj)));

            case TypeCode.Char:
            case TypeCode.Byte:
            case TypeCode.UInt16:
            case TypeCode.UInt32:
            case TypeCode.UInt64:
                return(LongV.Of(Convert.ToInt64(obj)));

            case TypeCode.Single:
            case TypeCode.Double:
            case TypeCode.Decimal:
                return(DoubleV.Of(Convert.ToDouble(obj)));

#if !(NETSTANDARD1_5)
            case TypeCode.DBNull:
                return(NullV.Instance);
#endif

            case TypeCode.Empty:
                return(NullV.Instance);

            case TypeCode.Object:
                if (typeof(DateTimeOffset).IsInstanceOfType(obj))
                {
                    return(Value.FromDateTimeOffset((DateTimeOffset)obj, forceType));
                }

                if (typeof(byte[]).IsInstanceOfType(obj))
                {
                    return(new BytesV((byte[])obj));
                }

                if (typeof(IDictionary).IsInstanceOfType(obj))
                {
                    return(WrapDictionary((IDictionary)obj));
                }

                if (typeof(IEnumerable).IsInstanceOfType(obj))
                {
                    return(WrapEnumerable((IEnumerable)obj));
                }

                return(WrapObj(obj));
            }

            throw new InvalidOperationException($"Unsupported type {type} at `{obj}`");
        }