public static bool ValueEquals(object objA, object objB)
        {
            if (objA == objB)
            {
                return(true);
            }
            if (objA == null || objB == null)
            {
                return(false);
            }

            // comparing an Int32 and Int64 both of the same value returns false
            // make types the same then compare
            if (objA.GetType() != objB.GetType())
            {
                if (ConvertUtils.IsInteger(objA) && ConvertUtils.IsInteger(objB))
                {
                    return(Convert.ToDecimal(objA, CultureInfo.CurrentCulture).Equals(Convert.ToDecimal(objB, CultureInfo.CurrentCulture)));
                }
                else if ((objA is double || objA is float || objA is decimal) && (objB is double || objB is float || objB is decimal))
                {
                    return(MathUtils.ApproxEquals(Convert.ToDouble(objA, CultureInfo.CurrentCulture), Convert.ToDouble(objB, CultureInfo.CurrentCulture)));
                }
#if (JSON_SmallDecSupport)
                else if (objA is SmallDec || objB is SmallDec)
                {
                    string   FullNameA = objA.GetType().FullName;
                    string   FullNameB = objA.GetType().FullName;
                    SmallDec i1        = objA is SmallDec ? (SmallDec)objA :
                                         (FullNameA == "MS.Internal.NamedObject" ? SmallDec.Zero : SmallDec.Initialize(objA));
                    SmallDec i2 = objB is SmallDec ? (SmallDec)objB :
                                  (FullNameB == "MS.Internal.NamedObject" ? SmallDec.Zero : SmallDec.Initialize(objB));
                    return(i1 == i2);
                }
#endif
                else
                {
                    return(false);
                }
            }

            return(objA.Equals(objB));
        }
        private void ValidateSmallDec(JsonSchemaModel schema)
        {
            if (schema == null)
            {
                return;
            }

            if (!TestType(schema, JsonSchemaType.SmallDec))
            {
                return;
            }

            ValidateNotDisallowed(schema);

            SmallDec value = (SmallDec)_reader.Value;

            if (schema.Maximum != null)
            {
                if (JValue.Compare(JTokenType.SmallDec, value, schema.Maximum) > 0)
                {
                    RaiseError("SmallDec {0} exceeds maximum value of {1}.".FormatWith(CultureInfo.InvariantCulture, value, schema.Maximum), schema);
                }
                if (schema.ExclusiveMaximum && JValue.Compare(JTokenType.SmallDec, value, schema.Maximum) == 0)
                {
                    RaiseError("SmallDec {0} equals maximum value of {1} and exclusive maximum is true.".FormatWith(CultureInfo.InvariantCulture, value, schema.Maximum), schema);
                }
            }

            if (schema.Minimum != null)
            {
                if (JValue.Compare(JTokenType.SmallDec, value, schema.Minimum) < 0)
                {
                    RaiseError("SmallDec {0} is less than minimum value of {1}.".FormatWith(CultureInfo.InvariantCulture, value, schema.Minimum), schema);
                }
                if (schema.ExclusiveMinimum && JValue.Compare(JTokenType.SmallDec, value, schema.Minimum) == 0)
                {
                    RaiseError("SmallDec {0} equals minimum value of {1} and exclusive minimum is true.".FormatWith(CultureInfo.InvariantCulture, value, schema.Minimum), schema);
                }
            }
        }
Exemple #3
0
        private void ReadType(BsonType type)
        {
            switch (type)
            {
            case BsonType.Number:
                double d = ReadDouble();

                if (_floatParseHandling == FloatParseHandling.Decimal)
                {
                    SetToken(JsonToken.Float, Convert.ToDecimal(d, CultureInfo.InvariantCulture));
                }
                else
                {
                    SetToken(JsonToken.Float, d);
                }
                break;

            case BsonType.String:
            case BsonType.Symbol:
                SetToken(JsonToken.String, ReadLengthString());
                break;

            case BsonType.Object:
            {
                SetToken(JsonToken.StartObject);

                ContainerContext newContext = new ContainerContext(BsonType.Object);
                PushContext(newContext);
                newContext.Length = ReadInt32();
                break;
            }

            case BsonType.Array:
            {
                SetToken(JsonToken.StartArray);

                ContainerContext newContext = new ContainerContext(BsonType.Array);
                PushContext(newContext);
                newContext.Length = ReadInt32();
                break;
            }

            case BsonType.Binary:
                BsonBinaryType binaryType;
                byte[]         data = ReadBinary(out binaryType);

                object value = (binaryType != BsonBinaryType.Uuid)
                        ? data
                        : (object)new Guid(data);

                SetToken(JsonToken.Bytes, value);
                break;

            case BsonType.Undefined:
                SetToken(JsonToken.Undefined);
                break;

            case BsonType.Oid:
                byte[] oid = ReadBytes(12);
                SetToken(JsonToken.Bytes, oid);
                break;

            case BsonType.Boolean:
                bool b = Convert.ToBoolean(ReadByte());
                SetToken(JsonToken.Boolean, b);
                break;

            case BsonType.Date:
                long     ticks       = ReadInt64();
                DateTime utcDateTime = DateTimeUtils.ConvertJavaScriptTicksToDateTime(ticks);

                DateTime dateTime;
                switch (DateTimeKindHandling)
                {
                case DateTimeKind.Unspecified:
                    dateTime = DateTime.SpecifyKind(utcDateTime, DateTimeKind.Unspecified);
                    break;

                case DateTimeKind.Local:
                    dateTime = utcDateTime.ToLocalTime();
                    break;

                default:
                    dateTime = utcDateTime;
                    break;
                }

                SetToken(JsonToken.Date, dateTime);
                break;

            case BsonType.Null:
                SetToken(JsonToken.Null);
                break;

            case BsonType.Regex:
                string expression = ReadString();
                string modifiers  = ReadString();

                string regex = @"/" + expression + @"/" + modifiers;
                SetToken(JsonToken.String, regex);
                break;

            case BsonType.Reference:
                SetToken(JsonToken.StartObject);
                _bsonReaderState = BsonReaderState.ReferenceStart;
                break;

            case BsonType.Code:
                SetToken(JsonToken.String, ReadLengthString());
                break;

            case BsonType.CodeWScope:
                SetToken(JsonToken.StartObject);
                _bsonReaderState = BsonReaderState.CodeWScopeStart;
                break;

            case BsonType.Integer:
                SetToken(JsonToken.Integer, (long)ReadInt32());
                break;

            case BsonType.TimeStamp:
            case BsonType.Long:
                SetToken(JsonToken.Integer, ReadInt64());
                break;

            case BsonType.SmallDec:
                SmallDec SmallDecObject = ReadSmallDec();
                SetToken(JsonToken.SmallDec, SmallDecObject);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(type), "Unexpected BsonType value: " + type);
            }
        }
 public static string ToString(SmallDec value)
 {
     return(value.ToString());
 }
Exemple #5
0
 public override void WriteValue(SmallDec value)
 {
     base.WriteValue(value);
     AddValue(value, BsonType.SmallDec);
 }
Exemple #6
0
        private static bool Operation(ExpressionType operation, object objA, object objB, out object result)
        {
            if (objA is string || objB is string)
            {
                if (operation == ExpressionType.Add || operation == ExpressionType.AddAssign)
                {
                    result = objA?.ToString() + objB?.ToString();
                    return(true);
                }
            }

#if HAVE_BIG_INTEGER
            if (objA is BigInteger || objB is BigInteger)
            {
                if (objA == null || objB == null)
                {
                    result = null;
                    return(true);
                }

                // not that this will lose the fraction
                // BigInteger doesn't have operators with non-integer types
                BigInteger i1 = ConvertUtils.ToBigInteger(objA);
                BigInteger i2 = ConvertUtils.ToBigInteger(objB);

                switch (operation)
                {
                case ExpressionType.Add:
                case ExpressionType.AddAssign:
                    result = i1 + i2;
                    return(true);

                case ExpressionType.Subtract:
                case ExpressionType.SubtractAssign:
                    result = i1 - i2;
                    return(true);

                case ExpressionType.Multiply:
                case ExpressionType.MultiplyAssign:
                    result = i1 * i2;
                    return(true);

                case ExpressionType.Divide:
                case ExpressionType.DivideAssign:
                    result = i1 / i2;
                    return(true);
                }
            }
#if (JSON_SmallDecSupport)
            else if (objA is SmallDec || objB is SmallDec)
            {
                string   FullNameA = objA.GetType().FullName;
                string   FullNameB = objA.GetType().FullName;
                SmallDec i1        = objA is SmallDec?(SmallDec)objA :
                                     (FullNameA == "MS.Internal.NamedObject"?SmallDec.Zero:SmallDec.Initialize(objA));
                SmallDec i2 = objB is SmallDec ? (SmallDec)objB :
                              (FullNameB == "MS.Internal.NamedObject" ? SmallDec.Zero : SmallDec.Initialize(objB));
                switch (operation)
                {
                case ExpressionType.Add:
                case ExpressionType.AddAssign:
                    result = i1 + i2;
                    return(true);

                case ExpressionType.Subtract:
                case ExpressionType.SubtractAssign:
                    result = i1 - i2;
                    return(true);

                case ExpressionType.Multiply:
                case ExpressionType.MultiplyAssign:
                    result = i1 * i2;
                    return(true);

                case ExpressionType.Divide:
                case ExpressionType.DivideAssign:
                    result = i1 / i2;
                    return(true);
                }
            }
#endif
            else
#endif
            if (objA is ulong || objB is ulong || objA is decimal || objB is decimal)
            {
                if (objA == null || objB == null)
                {
                    result = null;
                    return(true);
                }

                decimal d1 = Convert.ToDecimal(objA, CultureInfo.InvariantCulture);
                decimal d2 = Convert.ToDecimal(objB, CultureInfo.InvariantCulture);

                switch (operation)
                {
                case ExpressionType.Add:
                case ExpressionType.AddAssign:
                    result = d1 + d2;
                    return(true);

                case ExpressionType.Subtract:
                case ExpressionType.SubtractAssign:
                    result = d1 - d2;
                    return(true);

                case ExpressionType.Multiply:
                case ExpressionType.MultiplyAssign:
                    result = d1 * d2;
                    return(true);

                case ExpressionType.Divide:
                case ExpressionType.DivideAssign:
                    result = d1 / d2;
                    return(true);
                }
            }
            else if (objA is float || objB is float || objA is double || objB is double)
            {
                if (objA == null || objB == null)
                {
                    result = null;
                    return(true);
                }

                double d1 = Convert.ToDouble(objA, CultureInfo.InvariantCulture);
                double d2 = Convert.ToDouble(objB, CultureInfo.InvariantCulture);

                switch (operation)
                {
                case ExpressionType.Add:
                case ExpressionType.AddAssign:
                    result = d1 + d2;
                    return(true);

                case ExpressionType.Subtract:
                case ExpressionType.SubtractAssign:
                    result = d1 - d2;
                    return(true);

                case ExpressionType.Multiply:
                case ExpressionType.MultiplyAssign:
                    result = d1 * d2;
                    return(true);

                case ExpressionType.Divide:
                case ExpressionType.DivideAssign:
                    result = d1 / d2;
                    return(true);
                }
            }
            else if (objA is int || objA is uint || objA is long || objA is short || objA is ushort || objA is sbyte || objA is byte ||
                     objB is int || objB is uint || objB is long || objB is short || objB is ushort || objB is sbyte || objB is byte)
            {
                if (objA == null || objB == null)
                {
                    result = null;
                    return(true);
                }

                long l1 = Convert.ToInt64(objA, CultureInfo.InvariantCulture);
                long l2 = Convert.ToInt64(objB, CultureInfo.InvariantCulture);

                switch (operation)
                {
                case ExpressionType.Add:
                case ExpressionType.AddAssign:
                    result = l1 + l2;
                    return(true);

                case ExpressionType.Subtract:
                case ExpressionType.SubtractAssign:
                    result = l1 - l2;
                    return(true);

                case ExpressionType.Multiply:
                case ExpressionType.MultiplyAssign:
                    result = l1 * l2;
                    return(true);

                case ExpressionType.Divide:
                case ExpressionType.DivideAssign:
                    result = l1 / l2;
                    return(true);
                }
            }

            result = null;
            return(false);
        }
Exemple #7
0
        internal static int Compare(JTokenType valueType, object objA, object objB)
        {
            if (objA == objB)
            {
                return(0);
            }
            if (objB == null)
            {
                return(1);
            }
            if (objA == null)
            {
                return(-1);
            }

            switch (valueType)
            {
            case JTokenType.Integer:
#if HAVE_BIG_INTEGER
                if (objA is BigInteger)
                {
                    return(CompareBigInteger((BigInteger)objA, objB));
                }
                if (objB is BigInteger)
                {
                    return(-CompareBigInteger((BigInteger)objB, objA));
                }
#endif
                if (objA is ulong || objB is ulong || objA is decimal || objB is decimal)
                {
                    return(Convert.ToDecimal(objA, CultureInfo.InvariantCulture).CompareTo(Convert.ToDecimal(objB, CultureInfo.InvariantCulture)));
                }
                else if (objA is float || objB is float || objA is double || objB is double)
                {
                    return(CompareFloat(objA, objB));
                }
                else
                {
                    return(Convert.ToInt64(objA, CultureInfo.InvariantCulture).CompareTo(Convert.ToInt64(objB, CultureInfo.InvariantCulture)));
                }

            case JTokenType.Float:
#if HAVE_BIG_INTEGER
                if (objA is BigInteger)
                {
                    return(CompareBigInteger((BigInteger)objA, objB));
                }
                if (objB is BigInteger)
                {
                    return(-CompareBigInteger((BigInteger)objB, objA));
                }
#endif
                if (objA is ulong || objB is ulong || objA is decimal || objB is decimal)
                {
                    return(Convert.ToDecimal(objA, CultureInfo.InvariantCulture).CompareTo(Convert.ToDecimal(objB, CultureInfo.InvariantCulture)));
                }
                return(CompareFloat(objA, objB));

            case JTokenType.Comment:
            case JTokenType.String:
            case JTokenType.Raw:
                string s1 = Convert.ToString(objA, CultureInfo.InvariantCulture);
                string s2 = Convert.ToString(objB, CultureInfo.InvariantCulture);

                return(string.CompareOrdinal(s1, s2));

            case JTokenType.Boolean:
                bool b1 = Convert.ToBoolean(objA, CultureInfo.InvariantCulture);
                bool b2 = Convert.ToBoolean(objB, CultureInfo.InvariantCulture);

                return(b1.CompareTo(b2));

            case JTokenType.Date:
#if HAVE_DATE_TIME_OFFSET
                if (objA is DateTime)
                {
#endif
                DateTime date1 = (DateTime)objA;
                DateTime date2;

#if HAVE_DATE_TIME_OFFSET
                if (objB is DateTimeOffset)
                {
                    date2 = ((DateTimeOffset)objB).DateTime;
                }
                else
#endif
                {
                    date2 = Convert.ToDateTime(objB, CultureInfo.InvariantCulture);
                }

                return(date1.CompareTo(date2));

#if HAVE_DATE_TIME_OFFSET
            }
            else
            {
                DateTimeOffset date1 = (DateTimeOffset)objA;
                DateTimeOffset date2;

                if (objB is DateTimeOffset)
                {
                    date2 = (DateTimeOffset)objB;
                }
                else
                {
                    date2 = new DateTimeOffset(Convert.ToDateTime(objB, CultureInfo.InvariantCulture));
                }

                return(date1.CompareTo(date2));
            }
#endif
            case JTokenType.Bytes:
                byte[] bytes2 = objB as byte[];
                if (bytes2 == null)
                {
                    throw new ArgumentException("Object must be of type byte[].");
                }

                byte[] bytes1 = objA as byte[];
                Debug.Assert(bytes1 != null);

                return(MiscellaneousUtils.ByteArrayCompare(bytes1, bytes2));

            case JTokenType.Guid:
                if (!(objB is Guid))
                {
                    throw new ArgumentException("Object must be of type Guid.");
                }

                Guid guid1 = (Guid)objA;
                Guid guid2 = (Guid)objB;

                return(guid1.CompareTo(guid2));

            case JTokenType.Uri:
                Uri uri2 = objB as Uri;
                if (uri2 == null)
                {
                    throw new ArgumentException("Object must be of type Uri.");
                }

                Uri uri1 = (Uri)objA;

                return(Comparer <string> .Default.Compare(uri1.ToString(), uri2.ToString()));

            case JTokenType.TimeSpan:
                if (!(objB is TimeSpan))
                {
                    throw new ArgumentException("Object must be of type TimeSpan.");
                }

                TimeSpan ts1 = (TimeSpan)objA;
                TimeSpan ts2 = (TimeSpan)objB;

                return(ts1.CompareTo(ts2));

#if (JSON_SmallDecSupport)
            case JTokenType.SmallDec:
                SmallDec Value01 = (SmallDec)objA;
                SmallDec Value02 = (SmallDec)objB;
                return(Value01.CompareTo(Value02));
#endif
            default:
                throw MiscellaneousUtils.CreateArgumentOutOfRangeException(nameof(valueType), valueType, "Unexpected value type: {0}".FormatWith(CultureInfo.InvariantCulture, valueType));
            }
        }
Exemple #8
0
 public JValue(SmallDec value)
     : this(value, JTokenType.SmallDec)
 {
 }