예제 #1
0
파일: CBORNumber.cs 프로젝트: tspannhw/CBOR
        internal string ToJSONString()
        {
            switch (this.kind)
            {
            case Kind.Double: {
                var f = (double)this.value;
                if (Double.IsNegativeInfinity(f) ||
                    Double.IsPositiveInfinity(f) ||
                    Double.IsNaN(f))
                {
                    return("null");
                }
                string dblString = CBORUtilities.DoubleToString(f);
                return(CBORUtilities.TrimDotZero(dblString));
            }

            case Kind.Integer: {
                var longItem = (long)this.value;
                return(CBORUtilities.LongToString(longItem));
            }

            case Kind.EInteger: {
                return(((EInteger)this.value).ToString());
            }

            case Kind.EDecimal: {
                var dec = (EDecimal)this.value;
                if (dec.IsInfinity() || dec.IsNaN())
                {
                    return("null");
                }
                else
                {
                    return(dec.ToString());
                }
            }

            case Kind.EFloat: {
                var flo = (EFloat)this.value;
                if (flo.IsInfinity() || flo.IsNaN())
                {
                    return("null");
                }
                if (flo.IsFinite &&
                    flo.Exponent.Abs().CompareTo((EInteger)2500) > 0)
                {
                    // Too inefficient to convert to a decimal number
                    // from a bigfloat with a very high exponent,
                    // so convert to double instead
                    double f = flo.ToDouble();
                    if (Double.IsNegativeInfinity(f) ||
                        Double.IsPositiveInfinity(f) ||
                        Double.IsNaN(f))
                    {
                        return("null");
                    }
                    string dblString = CBORUtilities.DoubleToString(f);
                    return(CBORUtilities.TrimDotZero(dblString));
                }
                return(flo.ToString());
            }

            case Kind.ERational: {
                var      dec = (ERational)this.value;
                EDecimal f   = dec.ToEDecimalExactIfPossible(
                    EContext.Decimal128.WithUnlimitedExponents());
                if (!f.IsFinite)
                {
                    return("null");
                }
                else
                {
                    return(f.ToString());
                }
            }

            default: throw new InvalidOperationException();
            }
        }