Example #1
0
        public static RavenJToken Load(JsonTextReaderAsync reader)
        {
            RavenJValue v;

            switch (reader.TokenType)
            {
            case JsonToken.String:
            case JsonToken.Integer:
            case JsonToken.Float:
            case JsonToken.Date:
            case JsonToken.Boolean:
            case JsonToken.Bytes:
                v = new RavenJValue(reader.Value);
                break;

            case JsonToken.Null:
                v = new RavenJValue(null, JTokenType.Null);
                break;

            case JsonToken.Undefined:
                v = new RavenJValue(null, JTokenType.Undefined);
                break;

            default:
                throw new InvalidOperationException(StringUtils.FormatWith("The JsonReader should not be on a token of type {0}.", CultureInfo.InvariantCulture,
                                                                           reader.TokenType));
            }
            return(v);
        }
Example #2
0
        private static JTokenType GetValueType(JTokenType?current, object value)
        {
            if (value == null)
            {
                return(JTokenType.Null);
            }
            else if (value is string)
            {
                return(GetStringValueType(current));
            }
            else if (value is long || value is int || value is short || value is sbyte ||
                     value is ulong || value is uint || value is ushort || value is byte)
            {
                return(JTokenType.Integer);
            }
            else if (value is Enum)
            {
                return(JTokenType.Integer);
            }
            else if (value is double || value is float || value is decimal)
            {
                return(JTokenType.Float);
            }
            else if (value is DateTime)
            {
                return(JTokenType.Date);
            }
#if !PocketPC && !NET20
            else if (value is DateTimeOffset)
            {
                return(JTokenType.Date);
            }
#endif
            else if (value is byte[])
            {
                return(JTokenType.Bytes);
            }
            else if (value is bool)
            {
                return(JTokenType.Boolean);
            }
            else if (value is Guid)
            {
                return(JTokenType.Guid);
            }
            else if (value is Uri)
            {
                return(JTokenType.Uri);
            }
            else if (value is TimeSpan)
            {
                return(JTokenType.TimeSpan);
            }

            throw new ArgumentException(StringUtils.FormatWith("Could not determine JSON object type for type {0}.", CultureInfo.InvariantCulture, value.GetType()));
        }
Example #3
0
        private static int Compare(JTokenType valueType, object objA, object objB)
        {
            if (objA == null && objB == null)
            {
                return(0);
            }
            if (objA != null && objB == null)
            {
                return(1);
            }
            if (objA == null && objB != null)
            {
                return(-1);
            }

            switch (valueType)
            {
            case JTokenType.Integer:
                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:
                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 (objA is DateTime)
                {
                    DateTime date1 = Convert.ToDateTime(objA, CultureInfo.InvariantCulture);
                    DateTime date2 = Convert.ToDateTime(objB, CultureInfo.InvariantCulture);

                    return(date1.CompareTo(date2));
                }
                else
                {
                    if (!(objB is DateTimeOffset))
                    {
                        throw new ArgumentException("Object must be of type DateTimeOffset.");
                    }

                    DateTimeOffset date1 = (DateTimeOffset)objA;
                    DateTimeOffset date2 = (DateTimeOffset)objB;

                    return(date1.CompareTo(date2));
                }

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

                byte[] bytes1 = objA as byte[];
                byte[] bytes2 = objB as byte[];
                if (bytes1 == null)
                {
                    return(-1);
                }
                if (bytes2 == null)
                {
                    return(1);
                }

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

            case JTokenType.Guid:
                if (!(objB is Guid))
                {
                    Guid guid;
                    if (Guid.TryParse((string)objB, out guid) == false)
                    {
                        throw new ArgumentException("Object must be of type Guid.");
                    }
                    objB = guid;
                }

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

                return(guid1.CompareTo(guid2));

            case JTokenType.Uri:
                if (objB is string)
                {
                    objB = new Uri((string)objB, UriKind.RelativeOrAbsolute);
                }

                if (!(objB is Uri))
                {
                    throw new ArgumentException("Object must be of type Uri.");
                }

                Uri uri1 = (Uri)objA;
                Uri uri2 = (Uri)objB;

                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));

            default:
                throw MiscellaneousUtils.CreateArgumentOutOfRangeException("valueType", valueType, StringUtils.FormatWith("Unexpected value type: {0}", CultureInfo.InvariantCulture, valueType));
            }
        }