public TryCatch Visit()

in Microsoft.Azure.Cosmos/src/Json/JsonSerializer.cs [313:502]


            public TryCatch<object> Visit(CosmosNumber cosmosNumber, Type type)
            {
                if (type == typeof(Number64))
                {
                    return TryCatch<object>.FromResult(cosmosNumber.Value);
                }

                switch (Type.GetTypeCode(type))
                {
                    case TypeCode.Byte:
                        {
                            if (!cosmosNumber.Value.IsInteger)
                            {
                                return TryCatch<object>.FromException(
                                    new CosmosElementWrongTypeException("Expected integral type for byte."));
                            }

                            long value = Number64.ToLong(cosmosNumber.Value);
                            if ((value < byte.MinValue) || (value > byte.MaxValue))
                            {
                                return TryCatch<object>.FromException(
                                    new OverflowException($"{value} was out of range for byte."));
                            }

                            return TryCatch<object>.FromResult((byte)value);
                        }

                    case TypeCode.Decimal:
                        {
                            decimal value;
                            if (cosmosNumber.Value.IsDouble)
                            {
                                value = (decimal)Number64.ToDouble(cosmosNumber.Value);
                            }
                            else
                            {
                                value = Number64.ToLong(cosmosNumber.Value);
                            }

                            return TryCatch<object>.FromResult(value);
                        }

                    case TypeCode.Double:
                        {
                            if (!cosmosNumber.Value.IsDouble)
                            {
                                return TryCatch<object>.FromException(
                                    new CosmosElementWrongTypeException("Expected floating point type for double."));
                            }

                            double value = Number64.ToDouble(cosmosNumber.Value);
                            return TryCatch<object>.FromResult(value);
                        }

                    case TypeCode.Int16:
                        {
                            if (!cosmosNumber.Value.IsInteger)
                            {
                                return TryCatch<object>.FromException(
                                    new CosmosElementWrongTypeException("Expected integral type for short."));
                            }

                            long value = Number64.ToLong(cosmosNumber.Value);
                            if ((value < short.MinValue) || (value > short.MaxValue))
                            {
                                return TryCatch<object>.FromException(
                                    new OverflowException($"{value} was out of range for short."));
                            }

                            return TryCatch<object>.FromResult((short)value);
                        }

                    case TypeCode.Int32:
                        {
                            if (!cosmosNumber.Value.IsInteger)
                            {
                                return TryCatch<object>.FromException(
                                    new CosmosElementWrongTypeException("Expected integral type for int."));
                            }

                            long value = Number64.ToLong(cosmosNumber.Value);
                            if ((value < int.MinValue) || (value > int.MaxValue))
                            {
                                return TryCatch<object>.FromException(
                                    new OverflowException($"{value} was out of range for int."));
                            }

                            return TryCatch<object>.FromResult((int)value);
                        }

                    case TypeCode.Int64:
                        {
                            if (!cosmosNumber.Value.IsInteger)
                            {
                                return TryCatch<object>.FromException(
                                    new CosmosElementWrongTypeException("Expected integral type for long."));
                            }

                            long value = Number64.ToLong(cosmosNumber.Value);
                            return TryCatch<object>.FromResult(value);
                        }

                    case TypeCode.SByte:
                        {
                            if (!cosmosNumber.Value.IsInteger)
                            {
                                return TryCatch<object>.FromException(
                                    new CosmosElementWrongTypeException("Expected integral type for sbyte."));
                            }

                            long value = Number64.ToLong(cosmosNumber.Value);
                            if ((value < sbyte.MinValue) || (value > sbyte.MaxValue))
                            {
                                return TryCatch<object>.FromException(
                                    new OverflowException($"{value} was out of range for sbyte."));
                            }

                            return TryCatch<object>.FromResult((sbyte)value);
                        }

                    case TypeCode.Single:
                        {
                            if (!cosmosNumber.Value.IsDouble)
                            {
                                return TryCatch<object>.FromException(
                                    new CosmosElementWrongTypeException("Expected floating point type for float."));
                            }

                            double value = Number64.ToDouble(cosmosNumber.Value);
                            if ((value < float.MinValue) || (value > float.MaxValue))
                            {
                                return TryCatch<object>.FromException(
                                    new OverflowException($"{value} was out of range for float."));
                            }

                            return TryCatch<object>.FromResult((float)value);
                        }

                    case TypeCode.UInt16:
                        {
                            if (!cosmosNumber.Value.IsInteger)
                            {
                                return TryCatch<object>.FromException(
                                    new CosmosElementWrongTypeException("Expected integral type for ushort."));
                            }

                            long value = Number64.ToLong(cosmosNumber.Value);
                            if ((value < ushort.MinValue) || (value > ushort.MaxValue))
                            {
                                return TryCatch<object>.FromException(
                                    new OverflowException($"{value} was out of range for ushort."));
                            }

                            return TryCatch<object>.FromResult((ushort)value);
                        }

                    case TypeCode.UInt32:
                        {
                            if (!cosmosNumber.Value.IsInteger)
                            {
                                return TryCatch<object>.FromException(
                                    new CosmosElementWrongTypeException("Expected integral type for uint."));
                            }

                            long value = Number64.ToLong(cosmosNumber.Value);
                            if ((value < uint.MinValue) || (value > uint.MaxValue))
                            {
                                return TryCatch<object>.FromException(
                                    new OverflowException($"{value} was out of range for uint."));
                            }

                            return TryCatch<object>.FromResult((uint)value);
                        }

                    case TypeCode.UInt64:
                        {
                            if (!cosmosNumber.Value.IsInteger)
                            {
                                return TryCatch<object>.FromException(
                                    new CosmosElementWrongTypeException("Expected integral type for ulong."));
                            }

                            long value = Number64.ToLong(cosmosNumber.Value);
                            return TryCatch<object>.FromResult((ulong)value);
                        }

                    default:
                        throw new ArgumentOutOfRangeException($"Unknown {nameof(TypeCode)}: {Type.GetTypeCode(type)}.");
                }
            }