示例#1
0
        /// <summary>
        /// Writes this token to a <see cref="JsonWriter"/>.
        /// </summary>
        /// <param name="writer">A <see cref="JsonWriter"/> into which this method will write.</param>
        /// <param name="converters">A collection of <see cref="JsonConverter"/> which will be used when writing the token.</param>
        public override void WriteTo(JsonWriter writer, params JsonConverter[] converters)
        {
            switch (_valueType)
            {
            case JTokenType.Comment:
                writer.WriteComment(_value.ToString());
                return;

            case JTokenType.Raw:
                writer.WriteRawValue((_value != null) ? _value.ToString() : null);
                return;

            case JTokenType.Null:
                writer.WriteNull();
                return;

            case JTokenType.Undefined:
                writer.WriteUndefined();
                return;
            }

            JsonConverter matchingConverter;

            if (_value != null && ((matchingConverter = GetMatchingConverter(converters, _value.GetType())) != null))
            {
                matchingConverter.WriteJson(writer, _value, JsonExtensions.CreateDefaultJsonSerializer());
                return;
            }

            switch (_valueType)
            {
            case JTokenType.Integer:
                writer.WriteValue(Convert.ToInt64(_value, CultureInfo.InvariantCulture));
                return;

            case JTokenType.Float:
                if (_value is decimal)
                {
                    writer.WriteValue((decimal)_value);
                    return;
                }
                if (_value is float)
                {
                    writer.WriteValue((float)_value);
                    return;
                }
                writer.WriteValue(Convert.ToDouble(_value, CultureInfo.InvariantCulture));
                return;

            case JTokenType.String:
                writer.WriteValue((_value != null) ? _value.ToString() : null);
                return;

            case JTokenType.Boolean:
                writer.WriteValue(Convert.ToBoolean(_value, CultureInfo.InvariantCulture));
                return;

            case JTokenType.Date:
#if !PocketPC && !NET20
                if (_value is DateTimeOffset)
                {
                    writer.WriteValue((DateTimeOffset)_value);
                }
                else
#endif
                writer.WriteValue(Convert.ToDateTime(_value, CultureInfo.InvariantCulture));
                return;

            case JTokenType.Bytes:
                writer.WriteValue((byte[])_value);
                return;

            case JTokenType.Guid:
            case JTokenType.Uri:
            case JTokenType.TimeSpan:
                writer.WriteValue((_value != null) ? _value.ToString() : null);
                return;
            }

            throw MiscellaneousUtils.CreateArgumentOutOfRangeException("TokenType", _valueType, "Unexpected token type.");
        }
示例#2
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));
            }
        }
示例#3
0
        public override void WriteTo(JsonWriter writer, JsonConverterCollection converters)
        {
            switch (_valueType)
            {
            case JTokenType.Comment:
                writer.WriteComment(_value.ToString());
                return;

            case JTokenType.Raw:
                writer.WriteRawValue((_value != null) ? _value.ToString() : null);
                return;

            case JTokenType.Null:
                writer.WriteNull();
                return;

            case JTokenType.Undefined:
                writer.WriteUndefined();
                return;
            }

            if (_value != null)
            {
                Type typeToFind = _value.GetType();

                // If we are using the default converters we will try to avoid repeatedly check the same types as
                // GetMatchingConverter is a costly call with a very low probability to hit (less than 1% in real scenarios).
                JsonConverter matchingConverter = JsonConverterCache.GetMatchingConverter(converters, typeToFind);
                if (matchingConverter != null)
                {
                    matchingConverter.WriteJson(writer, _value, JsonExtensions.CreateDefaultJsonSerializer());
                    return;
                }
            }

            switch (_valueType)
            {
            case JTokenType.Integer:
                writer.WriteValue(Convert.ToInt64(_value, CultureInfo.InvariantCulture));
                return;

            case JTokenType.Float:
                if (_value is decimal)
                {
                    writer.WriteValue((decimal)_value);
                    return;
                }
                if (_value is float)
                {
                    writer.WriteValue((float)_value);
                    return;
                }
                writer.WriteValue(Convert.ToDouble(_value, CultureInfo.InvariantCulture));
                return;

            case JTokenType.String:
                writer.WriteValue((_value != null) ? _value.ToString() : null);
                return;

            case JTokenType.Boolean:
                writer.WriteValue(Convert.ToBoolean(_value, CultureInfo.InvariantCulture));
                return;

            case JTokenType.Date:
#if !PocketPC && !NET20
                if (_value is DateTimeOffset)
                {
                    writer.WriteValue((DateTimeOffset)_value);
                }
                else
#endif
                writer.WriteValue(Convert.ToDateTime(_value, CultureInfo.InvariantCulture));
                return;

            case JTokenType.Bytes:
                writer.WriteValue((byte[])_value);
                return;

            case JTokenType.Guid:
            case JTokenType.Uri:
            case JTokenType.TimeSpan:
                writer.WriteValue((_value != null) ? _value.ToString() : null);
                return;
            }

            throw MiscellaneousUtils.CreateArgumentOutOfRangeException("TokenType", _valueType, "Unexpected token type.");
        }