Beispiel #1
0
        public string ToDateTimeFormat(DateTimeKind kind)
        {
            switch (kind)
            {
            case DateTimeKind.Local:
                return(IsoDateFormat);

            case DateTimeKind.Unspecified:
                return("yyyy-MM-ddTHH:mm:ss.FFFFFFF");

            case DateTimeKind.Utc:
                return("yyyy-MM-ddTHH:mm:ss.FFFFFFFZ");

            default:
                throw MiscellaneousUtils.CreateArgumentOutOfRangeException(nameof(kind), kind, "Unexpected DateTimeKind value.");
            }
        }
Beispiel #2
0
        public static XmlDateTimeSerializationMode ToSerializationMode(DateTimeKind kind)
        {
            switch (kind)
            {
            case DateTimeKind.Unspecified:
                return(XmlDateTimeSerializationMode.Unspecified);

            case DateTimeKind.Utc:
                return(XmlDateTimeSerializationMode.Utc);

            case DateTimeKind.Local:
                return(XmlDateTimeSerializationMode.Local);

            default:
                throw MiscellaneousUtils.CreateArgumentOutOfRangeException("kind", kind, "Unexpected DateTimeKind value.");
            }
        }
Beispiel #3
0
        private JsonToken?GetEndToken(JContainer c)
        {
            switch (c.Type)
            {
            case JTokenType.Object:
                return(13);

            case JTokenType.Array:
                return(14);

            case JTokenType.Constructor:
                return(15);

            case JTokenType.Property:
                return(null);
            }
            throw MiscellaneousUtils.CreateArgumentOutOfRangeException("Type", c.Type, "Unexpected JContainer type.");
        }
Beispiel #4
0
        private static JsonToken GetJsonTokenType(RavenJToken token)
        {
            switch (token.Type)
            {
            case JTokenType.Integer:
                return(JsonToken.Integer);

            case JTokenType.Float:
                return(JsonToken.Float);

            case JTokenType.String:
                return(JsonToken.String);

            case JTokenType.Boolean:
                return(JsonToken.Boolean);

            case JTokenType.Null:
                return(JsonToken.Null);

            case JTokenType.Undefined:
                return(JsonToken.Undefined);

            case JTokenType.Date:
                return(JsonToken.Date);

            case JTokenType.Raw:
                return(JsonToken.Raw);

            case JTokenType.Bytes:
                return(JsonToken.Bytes);

            case JTokenType.Guid:
                return(JsonToken.String);

            case JTokenType.TimeSpan:
                return(JsonToken.String);

            case JTokenType.Uri:
                return(JsonToken.String);

            default:
                throw MiscellaneousUtils.CreateArgumentOutOfRangeException("Type", token.Type, "Unexpected JTokenType.");
            }
        }
        private JsonToken?GetEndToken(JContainer c)
        {
            switch (c.Type)
            {
            case JTokenType.Object:
                return(JsonToken.EndObject);

            case JTokenType.Array:
                return(JsonToken.EndArray);

            case JTokenType.Constructor:
                return(JsonToken.EndConstructor);

            case JTokenType.Property:
                return(null);

            default:
                throw MiscellaneousUtils.CreateArgumentOutOfRangeException(nameof(c.Type), c.Type, "Unexpected JContainer type.");
            }
        }
Beispiel #6
0
        internal 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 !(NET20 || NET35 || PORTABLE40 || PORTABLE)
                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 !(NET20 || NET35 || PORTABLE40 || PORTABLE)
                if (objA is BigInteger)
                {
                    return(CompareBigInteger((BigInteger)objA, objB));
                }
                if (objB is BigInteger)
                {
                    return(-CompareBigInteger((BigInteger)objB, objA));
                }
#endif
                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 !NET20
                if (objA is DateTime)
                {
#endif
                DateTime date1 = (DateTime)objA;
                DateTime date2;

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

                return(date1.CompareTo(date2));

#if !NET20
            }
            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:
                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))
                {
                    throw new ArgumentException("Object must be of type Guid.");
                }

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

                return(guid1.CompareTo(guid2));

            case JTokenType.Uri:
                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, "Unexpected value type: {0}".FormatWith(CultureInfo.InvariantCulture, valueType));
            }
        }
Beispiel #7
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 = JsonSerializer.GetMatchingConverter(converters, _value.GetType())) != null))
            {
                matchingConverter.WriteJson(writer, _value, new JsonSerializer());
                return;
            }

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

            case JTokenType.Float:
                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.");
        }
Beispiel #8
0
        // Token: 0x060013CF RID: 5071 RVA: 0x000692E8 File Offset: 0x000674E8
        private void SetToken(JToken token)
        {
            switch (token.Type)
            {
            case JTokenType.Object:
                base.SetToken(JsonToken.StartObject);
                return;

            case JTokenType.Array:
                base.SetToken(JsonToken.StartArray);
                return;

            case JTokenType.Constructor:
                base.SetToken(JsonToken.StartConstructor, ((JConstructor)token).Name);
                return;

            case JTokenType.Property:
                base.SetToken(JsonToken.PropertyName, ((JProperty)token).Name);
                return;

            case JTokenType.Comment:
                base.SetToken(JsonToken.Comment, ((JValue)token).Value);
                return;

            case JTokenType.Integer:
                base.SetToken(JsonToken.Integer, ((JValue)token).Value);
                return;

            case JTokenType.Float:
                base.SetToken(JsonToken.Float, ((JValue)token).Value);
                return;

            case JTokenType.String:
                base.SetToken(JsonToken.String, ((JValue)token).Value);
                return;

            case JTokenType.Boolean:
                base.SetToken(JsonToken.Boolean, ((JValue)token).Value);
                return;

            case JTokenType.Null:
                base.SetToken(JsonToken.Null, ((JValue)token).Value);
                return;

            case JTokenType.Undefined:
                base.SetToken(JsonToken.Undefined, ((JValue)token).Value);
                return;

            case JTokenType.Date:
            {
                object obj = ((JValue)token).Value;
                if (obj is DateTime)
                {
                    DateTime value = (DateTime)obj;
                    obj = DateTimeUtils.EnsureDateTime(value, base.DateTimeZoneHandling);
                }
                base.SetToken(JsonToken.Date, obj);
                return;
            }

            case JTokenType.Raw:
                base.SetToken(JsonToken.Raw, ((JValue)token).Value);
                return;

            case JTokenType.Bytes:
                base.SetToken(JsonToken.Bytes, ((JValue)token).Value);
                return;

            case JTokenType.Guid:
                base.SetToken(JsonToken.String, this.SafeToString(((JValue)token).Value));
                return;

            case JTokenType.Uri:
            {
                object    value2   = ((JValue)token).Value;
                JsonToken newToken = JsonToken.String;
                Uri       uri      = value2 as Uri;
                base.SetToken(newToken, (uri != null) ? uri.OriginalString : this.SafeToString(value2));
                return;
            }

            case JTokenType.TimeSpan:
                base.SetToken(JsonToken.String, this.SafeToString(((JValue)token).Value));
                return;

            default:
                throw MiscellaneousUtils.CreateArgumentOutOfRangeException("Type", token.Type, "Unexpected JTokenType.");
            }
        }
Beispiel #9
0
        internal void WriteToken(JsonReader reader, int initialDepth)
        {
            for (;;)
            {
                switch (reader.TokenType)
                {
                case JsonToken.None:
                    goto IL_1DB;

                case JsonToken.StartObject:
                    this.WriteStartObject();
                    goto IL_1DB;

                case JsonToken.StartArray:
                    this.WriteStartArray();
                    goto IL_1DB;

                case JsonToken.StartConstructor:
                {
                    string strA = reader.Value.ToString();
                    if (string.Compare(strA, "Date", StringComparison.Ordinal) == 0)
                    {
                        this.WriteConstructorDate(reader);
                    }
                    else
                    {
                        this.WriteStartConstructor(reader.Value.ToString());
                    }
                    goto IL_1DB;
                }

                case JsonToken.PropertyName:
                    this.WritePropertyName(reader.Value.ToString());
                    goto IL_1DB;

                case JsonToken.Comment:
                    this.WriteComment(reader.Value.ToString());
                    goto IL_1DB;

                case JsonToken.Raw:
                    this.WriteRawValue((string)reader.Value);
                    goto IL_1DB;

                case JsonToken.Integer:
                    this.WriteValue(Convert.ToInt64(reader.Value, CultureInfo.InvariantCulture));
                    goto IL_1DB;

                case JsonToken.Float:
                    this.WriteValue(Convert.ToDouble(reader.Value, CultureInfo.InvariantCulture));
                    goto IL_1DB;

                case JsonToken.String:
                    this.WriteValue(reader.Value.ToString());
                    goto IL_1DB;

                case JsonToken.Boolean:
                    this.WriteValue(Convert.ToBoolean(reader.Value, CultureInfo.InvariantCulture));
                    goto IL_1DB;

                case JsonToken.Null:
                    this.WriteNull();
                    goto IL_1DB;

                case JsonToken.Undefined:
                    this.WriteUndefined();
                    goto IL_1DB;

                case JsonToken.EndObject:
                    this.WriteEndObject();
                    goto IL_1DB;

                case JsonToken.EndArray:
                    this.WriteEndArray();
                    goto IL_1DB;

                case JsonToken.EndConstructor:
                    this.WriteEndConstructor();
                    goto IL_1DB;

                case JsonToken.Date:
                    this.WriteValue((DateTime)reader.Value);
                    goto IL_1DB;

                case JsonToken.Bytes:
                    this.WriteValue((byte[])reader.Value);
                    goto IL_1DB;
                }
                break;
IL_1DB:
                if (initialDepth - 1 >= reader.Depth - ((!this.IsEndToken(reader.TokenType)) ? 0 : 1) || !reader.Read())
                {
                    return;
                }
            }
            throw MiscellaneousUtils.CreateArgumentOutOfRangeException("TokenType", reader.TokenType, "Unexpected token type.");
        }
Beispiel #10
0
        public override void WriteTo(JsonWriter writer, params JsonConverter[] converters)
        {
            JTokenType valueType = this._valueType;

            switch (valueType)
            {
            case JTokenType.Null:
                writer.WriteNull();
                return;

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

            default:
            {
                if (valueType == JTokenType.Comment)
                {
                    writer.WriteComment(this._value.ToString());
                    return;
                }
                JsonConverter matchingConverter;
                if (this._value != null && (matchingConverter = JsonSerializer.GetMatchingConverter(converters, this._value.GetType())) != null)
                {
                    matchingConverter.WriteJson(writer, this._value, new JsonSerializer());
                    return;
                }
                switch (this._valueType)
                {
                case JTokenType.Integer:
                    writer.WriteValue(Convert.ToInt64(this._value, CultureInfo.InvariantCulture));
                    return;

                case JTokenType.Float:
                    writer.WriteValue(Convert.ToDouble(this._value, CultureInfo.InvariantCulture));
                    return;

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

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

                case JTokenType.Date:
                    if (this._value is DateTimeOffset)
                    {
                        writer.WriteValue((DateTimeOffset)this._value);
                    }
                    else
                    {
                        writer.WriteValue(Convert.ToDateTime(this._value, CultureInfo.InvariantCulture));
                    }
                    return;

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

                case JTokenType.Guid:
                case JTokenType.Uri:
                case JTokenType.TimeSpan:
                    writer.WriteValue((this._value == null) ? null : this._value.ToString());
                    return;
                }
                throw MiscellaneousUtils.CreateArgumentOutOfRangeException("TokenType", this._valueType, "Unexpected token type.");
            }

            case JTokenType.Raw:
                writer.WriteRawValue((this._value == null) ? null : this._value.ToString());
                return;
            }
        }
Beispiel #11
0
        internal static int Compare(JTokenType valueType, object objA, object objB)
        {
            DateTime time;
            DateTime dateTime;

            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.Comment:
            case JTokenType.String:
            case JTokenType.Raw:
                return(string.CompareOrdinal(Convert.ToString(objA, CultureInfo.InvariantCulture), Convert.ToString(objB, CultureInfo.InvariantCulture)));

            case JTokenType.Integer:
                switch (objA)
                {
                case (BigInteger _):
                    return(CompareBigInteger((BigInteger)objA, objB));

                    break;
                }
                if (objB is BigInteger)
                {
                    return(-CompareBigInteger((BigInteger)objB, objA));
                }
                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)));
                }
                if (((objA is float) || (objB is float)) || ((objA is double) || (objB is double)))
                {
                    return(CompareFloat(objA, objB));
                }
                return(Convert.ToInt64(objA, CultureInfo.InvariantCulture).CompareTo(Convert.ToInt64(objB, CultureInfo.InvariantCulture)));

            case JTokenType.Float:
                if (objA is BigInteger)
                {
                    return(CompareBigInteger((BigInteger)objA, objB));
                }
                if (objB is BigInteger)
                {
                    return(-CompareBigInteger((BigInteger)objB, objA));
                }
                return(CompareFloat(objA, objB));

            case JTokenType.Boolean:
            {
                bool flag  = Convert.ToBoolean(objA, CultureInfo.InvariantCulture);
                bool flag2 = Convert.ToBoolean(objB, CultureInfo.InvariantCulture);
                return(flag.CompareTo(flag2));
            }

            case JTokenType.Date:
            {
                if (!(objA is DateTime))
                {
                    DateTimeOffset offset3;
                    DateTimeOffset offset2 = (DateTimeOffset)objA;
                    if (objB is DateTimeOffset)
                    {
                        offset3 = (DateTimeOffset)objB;
                    }
                    else
                    {
                        offset3 = new DateTimeOffset(Convert.ToDateTime(objB, CultureInfo.InvariantCulture));
                    }
                    return(offset2.CompareTo(offset3));
                }
                time = (DateTime)objA;
                if (!(objB is DateTimeOffset))
                {
                    dateTime = Convert.ToDateTime(objB, CultureInfo.InvariantCulture);
                    break;
                }
                DateTimeOffset offset = (DateTimeOffset)objB;
                dateTime = offset.DateTime;
                break;
            }

            case JTokenType.Bytes:
            {
                if (!(objB is byte[]))
                {
                    throw new ArgumentException("Object must be of type byte[].");
                }
                byte[] buffer  = objA as byte[];
                byte[] buffer2 = objB as byte[];
                if (buffer == null)
                {
                    return(-1);
                }
                if (buffer2 == null)
                {
                    return(1);
                }
                return(MiscellaneousUtils.ByteArrayCompare(buffer, buffer2));
            }

            case JTokenType.Guid:
            {
                if (!(objB is Guid))
                {
                    throw new ArgumentException("Object must be of type Guid.");
                }
                Guid guid  = (Guid)objA;
                Guid guid2 = (Guid)objB;
                return(guid.CompareTo(guid2));
            }

            case JTokenType.Uri:
            {
                if (!(objB is Uri))
                {
                    throw new ArgumentException("Object must be of type Uri.");
                }
                Uri uri  = (Uri)objA;
                Uri uri2 = (Uri)objB;
                return(Comparer <string> .Default.Compare(uri.ToString(), uri2.ToString()));
            }

            case JTokenType.TimeSpan:
            {
                if (!(objB is TimeSpan))
                {
                    throw new ArgumentException("Object must be of type TimeSpan.");
                }
                TimeSpan span  = (TimeSpan)objA;
                TimeSpan span2 = (TimeSpan)objB;
                return(span.CompareTo(span2));
            }

            default:
                throw MiscellaneousUtils.CreateArgumentOutOfRangeException("valueType", valueType, "Unexpected value type: {0}".FormatWith(CultureInfo.InvariantCulture, valueType));
            }
            return(time.CompareTo(dateTime));
        }
Beispiel #12
0
        public override void WriteTo(JsonWriter writer, params JsonConverter[] converters)
        {
            if (((converters != null) && (converters.Length != 0)) && (this._value != null))
            {
                JsonConverter matchingConverter = JsonSerializer.GetMatchingConverter(converters, this._value.GetType());
                if ((matchingConverter != null) && matchingConverter.CanWrite)
                {
                    matchingConverter.WriteJson(writer, this._value, JsonSerializer.CreateDefault());
                    return;
                }
            }
            switch (this._valueType)
            {
            case JTokenType.Comment:
                writer.WriteComment(this._value?.ToString());
                return;

            case JTokenType.Integer:
                switch (this._value)
                {
                case (int _):
                    break;

                case (long _):
                    writer.WriteValue((long)this._value);
                    return;

                    break;

                case (ulong _):
                    writer.WriteValue((ulong)this._value);
                    return;

                    break;

                case (BigInteger _):
                    writer.WriteValue((BigInteger)this._value);
                    return;

                    break;
                }
                writer.WriteValue((int)this._value);
                return;

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

            case JTokenType.String:
                writer.WriteValue(this._value?.ToString());
                return;

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

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

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

            case JTokenType.Date:
                if (!(this._value is DateTimeOffset))
                {
                    writer.WriteValue(Convert.ToDateTime(this._value, CultureInfo.InvariantCulture));
                    return;
                }
                writer.WriteValue((DateTimeOffset)this._value);
                return;

            case JTokenType.Raw:
                writer.WriteRawValue(this._value?.ToString());
                return;

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

            case JTokenType.Guid:
                writer.WriteValue((this._value != null) ? ((Guid?)this._value) : null);
                return;

            case JTokenType.Uri:
                writer.WriteValue((Uri)this._value);
                return;

            case JTokenType.TimeSpan:
                writer.WriteValue((this._value != null) ? ((TimeSpan?)this._value) : null);
                return;
            }
            throw MiscellaneousUtils.CreateArgumentOutOfRangeException("TokenType", this._valueType, "Unexpected token type.");
        }
Beispiel #13
0
        internal void WriteToken(JsonReader reader, int initialDepth, bool writeChildren, bool writeDateConstructorAsDate)
        {
            while (true)
            {
                switch (reader.TokenType)
                {
                case JsonToken.None:
                    goto IL_2AB;

                case JsonToken.StartObject:
                    this.WriteStartObject();
                    goto IL_2AB;

                case JsonToken.StartArray:
                    this.WriteStartArray();
                    goto IL_2AB;

                case JsonToken.StartConstructor:
                {
                    string a = reader.Value.ToString();
                    if (writeDateConstructorAsDate && string.Equals(a, "Date", StringComparison.Ordinal))
                    {
                        this.WriteConstructorDate(reader);
                        goto IL_2AB;
                    }
                    this.WriteStartConstructor(reader.Value.ToString());
                    goto IL_2AB;
                }

                case JsonToken.PropertyName:
                    this.WritePropertyName(reader.Value.ToString());
                    goto IL_2AB;

                case JsonToken.Comment:
                    this.WriteComment((reader.Value != null) ? reader.Value.ToString() : null);
                    goto IL_2AB;

                case JsonToken.Raw:
                    this.WriteRawValue((reader.Value != null) ? reader.Value.ToString() : null);
                    goto IL_2AB;

                case JsonToken.Integer:
                    if (reader.Value is BigInteger)
                    {
                        this.WriteValue((BigInteger)reader.Value);
                        goto IL_2AB;
                    }
                    this.WriteValue(Convert.ToInt64(reader.Value, CultureInfo.InvariantCulture));
                    goto IL_2AB;

                case JsonToken.Float:
                {
                    object value = reader.Value;
                    if (value is decimal)
                    {
                        this.WriteValue((decimal)value);
                        goto IL_2AB;
                    }
                    if (value is double)
                    {
                        this.WriteValue((double)value);
                        goto IL_2AB;
                    }
                    if (value is float)
                    {
                        this.WriteValue((float)value);
                        goto IL_2AB;
                    }
                    this.WriteValue(Convert.ToDouble(value, CultureInfo.InvariantCulture));
                    goto IL_2AB;
                }

                case JsonToken.String:
                    this.WriteValue(reader.Value.ToString());
                    goto IL_2AB;

                case JsonToken.Boolean:
                    this.WriteValue(Convert.ToBoolean(reader.Value, CultureInfo.InvariantCulture));
                    goto IL_2AB;

                case JsonToken.Null:
                    this.WriteNull();
                    goto IL_2AB;

                case JsonToken.Undefined:
                    this.WriteUndefined();
                    goto IL_2AB;

                case JsonToken.EndObject:
                    this.WriteEndObject();
                    goto IL_2AB;

                case JsonToken.EndArray:
                    this.WriteEndArray();
                    goto IL_2AB;

                case JsonToken.EndConstructor:
                    this.WriteEndConstructor();
                    goto IL_2AB;

                case JsonToken.Date:
                    if (reader.Value is DateTimeOffset)
                    {
                        this.WriteValue((DateTimeOffset)reader.Value);
                        goto IL_2AB;
                    }
                    this.WriteValue(Convert.ToDateTime(reader.Value, CultureInfo.InvariantCulture));
                    goto IL_2AB;

                case JsonToken.Bytes:
                    this.WriteValue((byte[])reader.Value);
                    goto IL_2AB;
                }
                break;
IL_2AB:
                if (initialDepth - 1 >= reader.Depth - (JsonWriter.IsEndToken(reader.TokenType) ? 1 : 0) || !writeChildren || !reader.Read())
                {
                    return;
                }
            }
            throw MiscellaneousUtils.CreateArgumentOutOfRangeException("TokenType", reader.TokenType, "Unexpected token type.");
        }
Beispiel #14
0
        internal 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)
            {
                return(-1);
            }

            switch (valueType)
            {
            case JTokenType.Integer:
                if (objA is BigInteger)
                {
                    return(CompareBigInteger((BigInteger)objA, objB));
                }
                if (objB is BigInteger)
                {
                    return(-CompareBigInteger((BigInteger)objB, objA));
                }
                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)));
                }
                if (objA is float || objB is float || objA is double || objB is double)
                {
                    return(CompareFloat(objA, objB));
                }
                return(Convert.ToInt64(objA, CultureInfo.InvariantCulture).CompareTo(Convert.ToInt64(objB, CultureInfo.InvariantCulture)));

            case JTokenType.Float:
                if (objA is BigInteger)
                {
                    return(CompareBigInteger((BigInteger)objA, objB));
                }
                if (objB is BigInteger)
                {
                    return(-CompareBigInteger((BigInteger)objB, objA));
                }
                return(CompareFloat(objA, objB));

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

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

            case JTokenType.Date:
                if (objA is DateTime)
                {
                    var date1 = (DateTime)objA;
                    var date2 = objB is DateTimeOffset ? ((DateTimeOffset)objB).DateTime : Convert.ToDateTime(objB, CultureInfo.InvariantCulture);
                    return(date1.CompareTo(date2));
                }
                else
                {
                    var date1 = (DateTimeOffset)objA;
                    var date2 = objB is DateTimeOffset ? (DateTimeOffset)objB : new DateTimeOffset(Convert.ToDateTime(objB, CultureInfo.InvariantCulture));
                    return(date1.CompareTo(date2));
                }

            case JTokenType.Bytes:
                if (!(objB is byte[]))
                {
                    throw new ArgumentException("Object must be of type byte[].");
                }
                var bytes1 = objA as byte[];
                var bytes2 = objB as byte[];
                if (bytes1 == null)
                {
                    return(-1);
                }
                return(MiscellaneousUtils.ByteArrayCompare(bytes1, bytes2));

            case JTokenType.Guid:
                if (!(objB is Guid))
                {
                    throw new ArgumentException("Object must be of type Guid.");
                }
                var guid1 = (Guid)objA;
                var guid2 = (Guid)objB;
                return(guid1.CompareTo(guid2));

            case JTokenType.Uri:
                if (!(objB is Uri))
                {
                    throw new ArgumentException("Object must be of type Uri.");
                }
                var uri1 = (Uri)objA;
                var 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.");
                }
                var ts1 = (TimeSpan)objA;
                var ts2 = (TimeSpan)objB;
                return(ts1.CompareTo(ts2));

            default:
                throw MiscellaneousUtils.CreateArgumentOutOfRangeException("valueType", valueType, "Unexpected value type: {0}".FormatWith(CultureInfo.InvariantCulture, valueType));
            }
        }
Beispiel #15
0
        /// <summary>
        /// Writes this token to a <see cref="T:Newtonsoft.Json.JsonWriter" />.
        /// </summary>
        /// <param name="writer">A <see cref="T:Newtonsoft.Json.JsonWriter" /> into which this method will write.</param>
        /// <param name="converters">A collection of <see cref="T:Newtonsoft.Json.JsonConverter" /> which will be used when writing the token.</param>
        public override void WriteTo(JsonWriter writer, params JsonConverter[] converters)
        {
            if (converters != null && converters.Length != 0 && this._value != null)
            {
                JsonConverter matchingConverter = JsonSerializer.GetMatchingConverter((IList <JsonConverter>)converters, this._value.GetType());
                if (matchingConverter != null && matchingConverter.CanWrite)
                {
                    matchingConverter.WriteJson(writer, this._value, JsonSerializer.CreateDefault());
                    return;
                }
            }
            switch (this._valueType)
            {
            case JTokenType.Comment:
                writer.WriteComment(this._value != null ? this._value.ToString() : (string)null);
                break;

            case JTokenType.Integer:
                if (this._value is int)
                {
                    writer.WriteValue((int)this._value);
                    break;
                }
                if (this._value is long)
                {
                    writer.WriteValue((long)this._value);
                    break;
                }
                if (this._value is ulong)
                {
                    writer.WriteValue((ulong)this._value);
                    break;
                }
                writer.WriteValue(Convert.ToInt64(this._value, (IFormatProvider)CultureInfo.InvariantCulture));
                break;

            case JTokenType.Float:
                if (this._value is Decimal)
                {
                    writer.WriteValue((Decimal)this._value);
                    break;
                }
                if (this._value is double)
                {
                    writer.WriteValue((double)this._value);
                    break;
                }
                if (this._value is float)
                {
                    writer.WriteValue((float)this._value);
                    break;
                }
                writer.WriteValue(Convert.ToDouble(this._value, (IFormatProvider)CultureInfo.InvariantCulture));
                break;

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

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

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

            case JTokenType.Undefined:
                writer.WriteUndefined();
                break;

            case JTokenType.Date:
                if (this._value is DateTimeOffset)
                {
                    writer.WriteValue((DateTimeOffset)this._value);
                    break;
                }
                writer.WriteValue(Convert.ToDateTime(this._value, (IFormatProvider)CultureInfo.InvariantCulture));
                break;

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

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

            case JTokenType.Guid:
                writer.WriteValue(this._value != null ? (Guid?)this._value : new Guid?());
                break;

            case JTokenType.Uri:
                writer.WriteValue((Uri)this._value);
                break;

            case JTokenType.TimeSpan:
                writer.WriteValue(this._value != null ? (TimeSpan?)this._value : new TimeSpan?());
                break;

            default:
                throw MiscellaneousUtils.CreateArgumentOutOfRangeException("TokenType", (object)this._valueType, "Unexpected token type.");
            }
        }
Beispiel #16
0
        internal void WriteToken(JsonReader reader, int initialDepth)
        {
            do
            {
                switch (reader.TokenType)
                {
                case JsonToken.None:
                    continue;

                case JsonToken.StartObject:
                    this.WriteStartObject();
                    goto case 0;

                case JsonToken.StartArray:
                    this.WriteStartArray();
                    goto case 0;

                case JsonToken.StartConstructor:
                    if (string.Equals(reader.Value.ToString(), "Date", StringComparison.Ordinal))
                    {
                        this.WriteConstructorDate(reader);
                        goto case 0;
                    }
                    else
                    {
                        this.WriteStartConstructor(reader.Value.ToString());
                        goto case 0;
                    }

                case JsonToken.PropertyName:
                    this.WritePropertyName(reader.Value.ToString());
                    goto case 0;

                case JsonToken.Comment:
                    this.WriteComment(reader.Value != null ? reader.Value.ToString() : (string)null);
                    goto case 0;

                case JsonToken.Raw:
                    this.WriteRawValue(reader.Value != null ? reader.Value.ToString() : (string)null);
                    goto case 0;

                case JsonToken.Integer:
                    this.WriteValue(Convert.ToInt64(reader.Value, (IFormatProvider)CultureInfo.InvariantCulture));
                    goto case 0;

                case JsonToken.Float:
                    object obj = reader.Value;
                    if (obj is Decimal)
                    {
                        this.WriteValue((Decimal)obj);
                        goto case 0;
                    }
                    else if (obj is double)
                    {
                        this.WriteValue((double)obj);
                        goto case 0;
                    }
                    else if (obj is float)
                    {
                        this.WriteValue((float)obj);
                        goto case 0;
                    }
                    else
                    {
                        this.WriteValue(Convert.ToDouble(obj, (IFormatProvider)CultureInfo.InvariantCulture));
                        goto case 0;
                    }

                case JsonToken.String:
                    this.WriteValue(reader.Value.ToString());
                    goto case 0;

                case JsonToken.Boolean:
                    this.WriteValue(Convert.ToBoolean(reader.Value, (IFormatProvider)CultureInfo.InvariantCulture));
                    goto case 0;

                case JsonToken.Null:
                    this.WriteNull();
                    goto case 0;

                case JsonToken.Undefined:
                    this.WriteUndefined();
                    goto case 0;

                case JsonToken.EndObject:
                    this.WriteEndObject();
                    goto case 0;

                case JsonToken.EndArray:
                    this.WriteEndArray();
                    goto case 0;

                case JsonToken.EndConstructor:
                    this.WriteEndConstructor();
                    goto case 0;

                case JsonToken.Date:
                    this.WriteValue(Convert.ToDateTime(reader.Value, (IFormatProvider)CultureInfo.InvariantCulture));
                    goto case 0;

                case JsonToken.Bytes:
                    this.WriteValue((byte[])reader.Value);
                    goto case 0;

                default:
                    throw MiscellaneousUtils.CreateArgumentOutOfRangeException("TokenType", (object)reader.TokenType, "Unexpected token type.");
                }
            }while (initialDepth - 1 < reader.Depth - (this.IsEndToken(reader.TokenType) ? 1 : 0) && reader.Read());
        }
Beispiel #17
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)
            {
                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)));
                    }
                    if (objA is float || objB is float || objA is double || objB is double)
                    {
                        return(CompareFloat(objA, objB));
                    }
                    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 text = Convert.ToString(objA, CultureInfo.InvariantCulture);
                    string strB = Convert.ToString(objB, CultureInfo.InvariantCulture);
                    return(text.CompareTo(strB));
                }

                case JTokenType.Boolean:
                {
                    bool flag   = Convert.ToBoolean(objA, CultureInfo.InvariantCulture);
                    bool value3 = Convert.ToBoolean(objB, CultureInfo.InvariantCulture);
                    return(flag.CompareTo(value3));
                }

                case JTokenType.Date:
                {
                    if (objA is DateTime)
                    {
                        DateTime dateTime = Convert.ToDateTime(objA, CultureInfo.InvariantCulture);
                        DateTime value2   = Convert.ToDateTime(objB, CultureInfo.InvariantCulture);
                        return(dateTime.CompareTo(value2));
                    }
                    if (!(objB is DateTimeOffset))
                    {
                        throw new ArgumentException("Object must be of type DateTimeOffset.");
                    }
                    DateTimeOffset dateTimeOffset = (DateTimeOffset)objA;
                    DateTimeOffset other          = (DateTimeOffset)objB;
                    return(dateTimeOffset.CompareTo(other));
                }

                case JTokenType.Bytes:
                {
                    if (!(objB is byte[]))
                    {
                        throw new ArgumentException("Object must be of type byte[].");
                    }
                    byte[] array  = objA as byte[];
                    byte[] array2 = objB as byte[];
                    if (array == null)
                    {
                        return(-1);
                    }
                    if (array2 == null)
                    {
                        return(1);
                    }
                    return(MiscellaneousUtils.ByteArrayCompare(array, array2));
                }

                case JTokenType.Guid:
                {
                    if (!(objB is Guid))
                    {
                        throw new ArgumentException("Object must be of type Guid.");
                    }
                    Guid guid   = (Guid)objA;
                    Guid value4 = (Guid)objB;
                    return(guid.CompareTo(value4));
                }

                case JTokenType.Uri:
                {
                    if (!(objB is Uri))
                    {
                        throw new ArgumentException("Object must be of type Uri.");
                    }
                    Uri uri  = (Uri)objA;
                    Uri uri2 = (Uri)objB;
                    return(Comparer <string> .Default.Compare(uri.ToString(), uri2.ToString()));
                }

                case JTokenType.TimeSpan:
                {
                    if (!(objB is TimeSpan))
                    {
                        throw new ArgumentException("Object must be of type TimeSpan.");
                    }
                    TimeSpan timeSpan = (TimeSpan)objA;
                    TimeSpan value    = (TimeSpan)objB;
                    return(timeSpan.CompareTo(value));
                }

                default:
                    throw MiscellaneousUtils.CreateArgumentOutOfRangeException("valueType", valueType, "Unexpected value type: {0}".FormatWith(CultureInfo.InvariantCulture, valueType));
                }
            }
            return(-1);
        }
Beispiel #18
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);
                }

                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, "Unexpected value type: {0}".FormatWith(CultureInfo.InvariantCulture, valueType));
            }
        }
Beispiel #19
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(s1.CompareTo(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));

            default:
                throw MiscellaneousUtils.CreateArgumentOutOfRangeException("valueType", valueType, "Unexpected value type: {0}".FormatWith(CultureInfo.InvariantCulture, valueType));
            }
        }
Beispiel #20
0
        private void SetToken(JToken token)
        {
            switch (token.Type)
            {
            case JTokenType.Object:
            {
                base.SetToken(JsonToken.StartObject);
                return;
            }

            case JTokenType.Array:
            {
                base.SetToken(JsonToken.StartArray);
                return;
            }

            case JTokenType.Constructor:
            {
                base.SetToken(JsonToken.StartConstructor, ((JConstructor)token).Name);
                return;
            }

            case JTokenType.Property:
            {
                base.SetToken(JsonToken.PropertyName, ((JProperty)token).Name);
                return;
            }

            case JTokenType.Comment:
            {
                base.SetToken(JsonToken.Comment, ((JValue)token).Value);
                return;
            }

            case JTokenType.Integer:
            {
                base.SetToken(JsonToken.Integer, ((JValue)token).Value);
                return;
            }

            case JTokenType.Float:
            {
                base.SetToken(JsonToken.Float, ((JValue)token).Value);
                return;
            }

            case JTokenType.String:
            {
                base.SetToken(JsonToken.String, ((JValue)token).Value);
                return;
            }

            case JTokenType.Boolean:
            {
                base.SetToken(JsonToken.Boolean, ((JValue)token).Value);
                return;
            }

            case JTokenType.Null:
            {
                base.SetToken(JsonToken.Null, ((JValue)token).Value);
                return;
            }

            case JTokenType.Undefined:
            {
                base.SetToken(JsonToken.Undefined, ((JValue)token).Value);
                return;
            }

            case JTokenType.Date:
            {
                base.SetToken(JsonToken.Date, ((JValue)token).Value);
                return;
            }

            case JTokenType.Raw:
            {
                base.SetToken(JsonToken.Raw, ((JValue)token).Value);
                return;
            }

            case JTokenType.Bytes:
            {
                base.SetToken(JsonToken.Bytes, ((JValue)token).Value);
                return;
            }

            case JTokenType.Guid:
            {
                base.SetToken(JsonToken.String, this.SafeToString(((JValue)token).Value));
                return;
            }

            case JTokenType.Uri:
            {
                object value = ((JValue)token).Value;
                if (value is Uri)
                {
                    base.SetToken(JsonToken.String, ((Uri)value).OriginalString);
                    return;
                }
                base.SetToken(JsonToken.String, this.SafeToString(value));
                return;
            }

            case JTokenType.TimeSpan:
            {
                base.SetToken(JsonToken.String, this.SafeToString(((JValue)token).Value));
                return;
            }
            }
            throw MiscellaneousUtils.CreateArgumentOutOfRangeException("Type", token.Type, "Unexpected JTokenType.");
        }
Beispiel #21
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 integerA)
                    {
                        return CompareBigInteger(integerA, objB);
                    }
                    if (objB is BigInteger integerB)
                    {
                        return -CompareBigInteger(integerB, 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 integerA)
                    {
                        return CompareBigInteger(integerA, objB);
                    }
                    if (objB is BigInteger integerB)
                    {
                        return -CompareBigInteger(integerB, 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 dateA)
                    {
#else
                    DateTime dateA = (DateTime)objA;
#endif
                    DateTime dateB;

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

                    return dateA.CompareTo(dateB);
#if HAVE_DATE_TIME_OFFSET
                    }
                    else
                    {
                        DateTimeOffset offsetA = (DateTimeOffset)objA;
                        if (!(objB is DateTimeOffset offsetB))
                        {
                            offsetB = new DateTimeOffset(
                                Convert.ToDateTime(objB, CultureInfo.InvariantCulture)
                            );
                        }

                        return offsetA.CompareTo(offsetB);
                    }
#endif
                case JTokenType.Bytes:
                    if (!(objB is byte[] bytesB))
                    {
                        throw new ArgumentException("Object must be of type byte[].");
                    }

                    byte[]? bytesA = objA as byte[];
                    MiscellaneousUtils.Assert(bytesA != null);

                    return MiscellaneousUtils.ByteArrayCompare(bytesA!, bytesB);
                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);
                default:
                    throw MiscellaneousUtils.CreateArgumentOutOfRangeException(
                        nameof(valueType),
                        valueType,
                        "Unexpected value type: {0}".FormatWith(
                            CultureInfo.InvariantCulture,
                            valueType
                        )
                    );
            }
        }
Beispiel #22
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.Comment:
            case JTokenType.String:
            case JTokenType.Raw:
            {
                string strA = Convert.ToString(objA, CultureInfo.InvariantCulture);
                string strB = Convert.ToString(objB, CultureInfo.InvariantCulture);
                return(string.CompareOrdinal(strA, strB));
            }

            case JTokenType.Integer:
                if (objA is System.Numerics.BigInteger)
                {
                    System.Numerics.BigInteger i = (System.Numerics.BigInteger)objA;
                    return(JValue.CompareBigInteger(i, objB));
                }
                if (objB is System.Numerics.BigInteger)
                {
                    System.Numerics.BigInteger i2 = (System.Numerics.BigInteger)objB;
                    return(-JValue.CompareBigInteger(i2, objA));
                }
                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)));
                }
                if (objA is float || objB is float || objA is double || objB is double)
                {
                    return(JValue.CompareFloat(objA, objB));
                }
                return(Convert.ToInt64(objA, CultureInfo.InvariantCulture).CompareTo(Convert.ToInt64(objB, CultureInfo.InvariantCulture)));

            case JTokenType.Float:
                if (objA is System.Numerics.BigInteger)
                {
                    System.Numerics.BigInteger i3 = (System.Numerics.BigInteger)objA;
                    return(JValue.CompareBigInteger(i3, objB));
                }
                if (objB is System.Numerics.BigInteger)
                {
                    System.Numerics.BigInteger i4 = (System.Numerics.BigInteger)objB;
                    return(-JValue.CompareBigInteger(i4, objA));
                }
                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(JValue.CompareFloat(objA, objB));

            case JTokenType.Boolean:
            {
                bool flag  = Convert.ToBoolean(objA, CultureInfo.InvariantCulture);
                bool value = Convert.ToBoolean(objB, CultureInfo.InvariantCulture);
                return(flag.CompareTo(value));
            }

            case JTokenType.Date:
            {
                if (objA is DateTime)
                {
                    DateTime dateTime = (DateTime)objA;
                    DateTime value2;
                    if (objB is DateTimeOffset)
                    {
                        value2 = ((DateTimeOffset)objB).DateTime;
                    }
                    else
                    {
                        value2 = Convert.ToDateTime(objB, CultureInfo.InvariantCulture);
                    }
                    return(dateTime.CompareTo(value2));
                }
                DateTimeOffset dateTimeOffset = (DateTimeOffset)objA;
                DateTimeOffset other;
                if (objB is DateTimeOffset)
                {
                    other = (DateTimeOffset)objB;
                }
                else
                {
                    other = new DateTimeOffset(Convert.ToDateTime(objB, CultureInfo.InvariantCulture));
                }
                return(dateTimeOffset.CompareTo(other));
            }

            case JTokenType.Bytes:
            {
                byte[] array = objB as byte[];
                if (array == null)
                {
                    throw new ArgumentException("Object must be of type byte[].");
                }
                return(MiscellaneousUtils.ByteArrayCompare(objA as byte[], array));
            }

            case JTokenType.Guid:
            {
                if (!(objB is Guid))
                {
                    throw new ArgumentException("Object must be of type Guid.");
                }
                Guid guid   = (Guid)objA;
                Guid value3 = (Guid)objB;
                return(guid.CompareTo(value3));
            }

            case JTokenType.Uri:
            {
                Uri uri = objB as Uri;
                if (uri == null)
                {
                    throw new ArgumentException("Object must be of type Uri.");
                }
                Uri uri2 = (Uri)objA;
                return(Comparer <string> .Default.Compare(uri2.ToString(), uri.ToString()));
            }

            case JTokenType.TimeSpan:
            {
                if (!(objB is TimeSpan))
                {
                    throw new ArgumentException("Object must be of type TimeSpan.");
                }
                TimeSpan timeSpan = (TimeSpan)objA;
                TimeSpan value4   = (TimeSpan)objB;
                return(timeSpan.CompareTo(value4));
            }
            }
            throw MiscellaneousUtils.CreateArgumentOutOfRangeException("valueType", valueType, "Unexpected value type: {0}".FormatWith(CultureInfo.InvariantCulture, valueType));
        }
Beispiel #23
0
        internal void WriteToken(JsonReader reader, int initialDepth)
        {
            do
            {
                switch (reader.TokenType)
                {
                case JsonToken.None:
                    // read to next
                    break;

                case JsonToken.StartObject:
                    WriteStartObject();
                    break;

                case JsonToken.StartArray:
                    WriteStartArray();
                    break;

                case JsonToken.StartConstructor:
                    string constructorName = reader.Value.ToString();
                    // write a JValue date when the constructor is for a date
                    if (string.Compare(constructorName, "Date", StringComparison.Ordinal) == 0)
                    {
                        WriteConstructorDate(reader);
                    }
                    else
                    {
                        WriteStartConstructor(reader.Value.ToString());
                    }
                    break;

                case JsonToken.PropertyName:
                    WritePropertyName(reader.Value.ToString());
                    break;

                case JsonToken.Comment:
                    WriteComment(reader.Value.ToString());
                    break;

                case JsonToken.Integer:
                    WriteValue(Convert.ToInt64(reader.Value, CultureInfo.InvariantCulture));
                    break;

                case JsonToken.Float:
                    WriteValue(Convert.ToDouble(reader.Value, CultureInfo.InvariantCulture));
                    break;

                case JsonToken.String:
                    WriteValue(reader.Value.ToString());
                    break;

                case JsonToken.Boolean:
                    WriteValue(Convert.ToBoolean(reader.Value, CultureInfo.InvariantCulture));
                    break;

                case JsonToken.Null:
                    WriteNull();
                    break;

                case JsonToken.Undefined:
                    WriteUndefined();
                    break;

                case JsonToken.EndObject:
                    WriteEndObject();
                    break;

                case JsonToken.EndArray:
                    WriteEndArray();
                    break;

                case JsonToken.EndConstructor:
                    WriteEndConstructor();
                    break;

                case JsonToken.Date:
                    WriteValue((DateTime)reader.Value);
                    break;

                case JsonToken.Raw:
                    WriteRawValue((string)reader.Value);
                    break;

                case JsonToken.Bytes:
                    WriteValue((byte[])reader.Value);
                    break;

                default:
                    throw MiscellaneousUtils.CreateArgumentOutOfRangeException("TokenType", reader.TokenType, "Unexpected token type.");
                }
            }while (
                // stop if we have reached the end of the token being read
                initialDepth - 1 < reader.Depth - (IsEndToken(reader.TokenType) ? 1 : 0) &&
                reader.Read());
        }
Beispiel #24
0
        // Token: 0x0600141E RID: 5150 RVA: 0x0006A834 File Offset: 0x00068A34
        public override void WriteTo(JsonWriter writer, params JsonConverter[] converters)
        {
            if (converters != null && converters.Length != 0 && this._value != null)
            {
                JsonConverter matchingConverter = JsonSerializer.GetMatchingConverter(converters, this._value.GetType());
                if (matchingConverter != null && matchingConverter.CanWrite)
                {
                    matchingConverter.WriteJson(writer, this._value, JsonSerializer.CreateDefault());
                    return;
                }
            }
            switch (this._valueType)
            {
            case JTokenType.Comment:
            {
                object value = this._value;
                writer.WriteComment((value != null) ? value.ToString() : null);
                return;
            }

            case JTokenType.Integer:
            {
                object value2 = this._value;
                if (value2 is int)
                {
                    int value3 = (int)value2;
                    writer.WriteValue(value3);
                    return;
                }
                value2 = this._value;
                if (value2 is long)
                {
                    long value4 = (long)value2;
                    writer.WriteValue(value4);
                    return;
                }
                value2 = this._value;
                if (value2 is ulong)
                {
                    ulong value5 = (ulong)value2;
                    writer.WriteValue(value5);
                    return;
                }
                value2 = this._value;
                if (value2 is System.Numerics.BigInteger)
                {
                    System.Numerics.BigInteger bigInteger = (System.Numerics.BigInteger)value2;
                    writer.WriteValue(bigInteger);
                    return;
                }
                writer.WriteValue(Convert.ToInt64(this._value, CultureInfo.InvariantCulture));
                return;
            }

            case JTokenType.Float:
            {
                object value2 = this._value;
                if (value2 is decimal)
                {
                    decimal value6 = (decimal)value2;
                    writer.WriteValue(value6);
                    return;
                }
                value2 = this._value;
                if (value2 is double)
                {
                    double value7 = (double)value2;
                    writer.WriteValue(value7);
                    return;
                }
                value2 = this._value;
                if (value2 is float)
                {
                    float value8 = (float)value2;
                    writer.WriteValue(value8);
                    return;
                }
                writer.WriteValue(Convert.ToDouble(this._value, CultureInfo.InvariantCulture));
                return;
            }

            case JTokenType.String:
            {
                object value9 = this._value;
                writer.WriteValue((value9 != null) ? value9.ToString() : null);
                return;
            }

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

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

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

            case JTokenType.Date:
            {
                object value2 = this._value;
                if (value2 is DateTimeOffset)
                {
                    DateTimeOffset value10 = (DateTimeOffset)value2;
                    writer.WriteValue(value10);
                    return;
                }
                writer.WriteValue(Convert.ToDateTime(this._value, CultureInfo.InvariantCulture));
                return;
            }

            case JTokenType.Raw:
            {
                object value11 = this._value;
                writer.WriteRawValue((value11 != null) ? value11.ToString() : null);
                return;
            }

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

            case JTokenType.Guid:
                writer.WriteValue((this._value != null) ? ((Guid?)this._value) : null);
                return;

            case JTokenType.Uri:
                writer.WriteValue((Uri)this._value);
                return;

            case JTokenType.TimeSpan:
                writer.WriteValue((this._value != null) ? ((TimeSpan?)this._value) : null);
                return;

            default:
                throw MiscellaneousUtils.CreateArgumentOutOfRangeException("Type", this._valueType, "Unexpected token type.");
            }
        }
Beispiel #25
0
        public void WriteToken(JsonToken token, object value)
        {
            switch (token)
            {
            case JsonToken.None:
                break;

            case JsonToken.StartObject:
                this.WriteStartObject();
                break;

            case JsonToken.StartArray:
                this.WriteStartArray();
                break;

            case JsonToken.StartConstructor:
                ValidationUtils.ArgumentNotNull(value, nameof(value));
                this.WriteStartConstructor(value.ToString());
                break;

            case JsonToken.PropertyName:
                ValidationUtils.ArgumentNotNull(value, nameof(value));
                this.WritePropertyName(value.ToString());
                break;

            case JsonToken.Comment:
                this.WriteComment(value?.ToString());
                break;

            case JsonToken.Raw:
                this.WriteRawValue(value?.ToString());
                break;

            case JsonToken.Integer:
                ValidationUtils.ArgumentNotNull(value, nameof(value));
                this.WriteValue(Convert.ToInt64(value, (IFormatProvider)CultureInfo.InvariantCulture));
                break;

            case JsonToken.Float:
                ValidationUtils.ArgumentNotNull(value, nameof(value));
                if (value is Decimal)
                {
                    this.WriteValue((Decimal)value);
                    break;
                }

                if (value is double)
                {
                    this.WriteValue((double)value);
                    break;
                }

                if (value is float)
                {
                    this.WriteValue((float)value);
                    break;
                }

                this.WriteValue(Convert.ToDouble(value, (IFormatProvider)CultureInfo.InvariantCulture));
                break;

            case JsonToken.String:
                ValidationUtils.ArgumentNotNull(value, nameof(value));
                this.WriteValue(value.ToString());
                break;

            case JsonToken.Boolean:
                ValidationUtils.ArgumentNotNull(value, nameof(value));
                this.WriteValue(Convert.ToBoolean(value, (IFormatProvider)CultureInfo.InvariantCulture));
                break;

            case JsonToken.Null:
                this.WriteNull();
                break;

            case JsonToken.Undefined:
                this.WriteUndefined();
                break;

            case JsonToken.EndObject:
                this.WriteEndObject();
                break;

            case JsonToken.EndArray:
                this.WriteEndArray();
                break;

            case JsonToken.EndConstructor:
                this.WriteEndConstructor();
                break;

            case JsonToken.Date:
                ValidationUtils.ArgumentNotNull(value, nameof(value));
                this.WriteValue(Convert.ToDateTime(value, (IFormatProvider)CultureInfo.InvariantCulture));
                break;

            case JsonToken.Bytes:
                ValidationUtils.ArgumentNotNull(value, nameof(value));
                if (value is Guid)
                {
                    this.WriteValue((Guid)value);
                    break;
                }

                this.WriteValue((byte[])value);
                break;

            default:
                throw MiscellaneousUtils.CreateArgumentOutOfRangeException(nameof(token), (object)token,
                                                                           "Unexpected token type.");
            }
        }
Beispiel #26
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"/>s which will be used when writing the token.</param>
        public override void WriteTo(JsonWriter writer, params JsonConverter[] converters)
        {
            if (converters != null && converters.Length > 0 && _value != null)
            {
                JsonConverter matchingConverter = JsonSerializer.GetMatchingConverter(converters, _value.GetType());
                if (matchingConverter != null && matchingConverter.CanWrite)
                {
                    matchingConverter.WriteJson(writer, _value, JsonSerializer.CreateDefault());
                    return;
                }
            }

            switch (_valueType)
            {
            case JTokenType.Comment:
                writer.WriteComment(_value?.ToString());
                return;

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

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

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

            case JTokenType.Integer:
                if (_value is int)
                {
                    writer.WriteValue((int)_value);
                }
                else if (_value is long)
                {
                    writer.WriteValue((long)_value);
                }
                else if (_value is ulong)
                {
                    writer.WriteValue((ulong)_value);
                }
#if HAVE_BIG_INTEGER
                else if (_value is BigInteger)
                {
                    writer.WriteValue((BigInteger)_value);
                }
#endif
                else
                {
                    writer.WriteValue(Convert.ToInt64(_value, CultureInfo.InvariantCulture));
                }
                return;

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

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

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

            case JTokenType.Date:
#if HAVE_DATE_TIME_OFFSET
                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:
                writer.WriteValue((_value != null) ? (Guid?)_value : null);
                return;

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

            case JTokenType.Uri:
                writer.WriteValue((Uri)_value);
                return;
            }

            throw MiscellaneousUtils.CreateArgumentOutOfRangeException(nameof(Type), _valueType, "Unexpected token type.");
        }
Beispiel #27
0
        private void SetToken(JToken token)
        {
            switch (token.Type)
            {
            case JTokenType.Object:
                SetToken(JsonToken.StartObject);
                break;

            case JTokenType.Array:
                SetToken(JsonToken.StartArray);
                break;

            case JTokenType.Constructor:
                SetToken(JsonToken.StartConstructor);
                break;

            case JTokenType.Property:
                SetToken(JsonToken.PropertyName, ((JProperty)token).Name);
                break;

            case JTokenType.Comment:
                SetToken(JsonToken.Comment, ((JValue)token).Value);
                break;

            case JTokenType.Integer:
                SetToken(JsonToken.Integer, ((JValue)token).Value);
                break;

            case JTokenType.Float:
                SetToken(JsonToken.Float, ((JValue)token).Value);
                break;

            case JTokenType.String:
                SetToken(JsonToken.String, ((JValue)token).Value);
                break;

            case JTokenType.Boolean:
                SetToken(JsonToken.Boolean, ((JValue)token).Value);
                break;

            case JTokenType.Null:
                SetToken(JsonToken.Null, ((JValue)token).Value);
                break;

            case JTokenType.Undefined:
                SetToken(JsonToken.Undefined, ((JValue)token).Value);
                break;

            case JTokenType.Date:
                SetToken(JsonToken.Date, ((JValue)token).Value);
                break;

            case JTokenType.Raw:
                SetToken(JsonToken.Raw, ((JValue)token).Value);
                break;

            case JTokenType.Bytes:
                SetToken(JsonToken.Bytes, ((JValue)token).Value);
                break;

            case JTokenType.Guid:
                SetToken(JsonToken.String, SafeToString(((JValue)token).Value));
                break;

            case JTokenType.Uri:
                SetToken(JsonToken.String, SafeToString(((JValue)token).Value));
                break;

            case JTokenType.TimeSpan:
                SetToken(JsonToken.String, SafeToString(((JValue)token).Value));
                break;

            default:
                throw MiscellaneousUtils.CreateArgumentOutOfRangeException("Type", token.Type, "Unexpected JTokenType.");
            }
        }
        /// <summary>
        /// Writes this token to a <see cref="JsonWriter"/> asynchronously.
        /// </summary>
        /// <param name="writer">A <see cref="JsonWriter"/> into which this method will write.</param>
        /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
        /// <param name="converters">A collection of <see cref="JsonConverter"/> which will be used when writing the token.</param>
        /// <returns>A <see cref="Task"/> that represents the asynchronous write operation.</returns>
        public override Task WriteToAsync(JsonWriter writer, CancellationToken cancellationToken, params JsonConverter[] converters)
        {
            if (converters != null && converters.Length > 0 && _value != null)
            {
                JsonConverter matchingConverter = JsonSerializer.GetMatchingConverter(converters, _value.GetType());
                if (matchingConverter != null && matchingConverter.CanWrite)
                {
                    // TODO: Call WriteJsonAsync when it exists.
                    matchingConverter.WriteJson(writer, _value, JsonSerializer.CreateDefault());
                    return(AsyncUtils.CompletedTask);
                }
            }

            switch (_valueType)
            {
            case JTokenType.Comment:
                return(writer.WriteCommentAsync(_value?.ToString(), cancellationToken));

            case JTokenType.Raw:
                return(writer.WriteRawValueAsync(_value?.ToString(), cancellationToken));

            case JTokenType.Null:
                return(writer.WriteNullAsync(cancellationToken));

            case JTokenType.Undefined:
                return(writer.WriteUndefinedAsync(cancellationToken));

            case JTokenType.Integer:
                if (_value is int)
                {
                    return(writer.WriteValueAsync((int)_value, cancellationToken));
                }

                if (_value is long)
                {
                    return(writer.WriteValueAsync((long)_value, cancellationToken));
                }

                if (_value is ulong)
                {
                    return(writer.WriteValueAsync((ulong)_value, cancellationToken));
                }

#if HAVE_BIG_INTEGER
                if (_value is BigInteger)
                {
                    return(writer.WriteValueAsync((BigInteger)_value, cancellationToken));
                }
#endif

                return(writer.WriteValueAsync(Convert.ToInt64(_value, CultureInfo.InvariantCulture), cancellationToken));

            case JTokenType.Float:
                if (_value is decimal)
                {
                    return(writer.WriteValueAsync((decimal)_value, cancellationToken));
                }

                if (_value is double)
                {
                    return(writer.WriteValueAsync((double)_value, cancellationToken));
                }

                if (_value is float)
                {
                    return(writer.WriteValueAsync((float)_value, cancellationToken));
                }

                return(writer.WriteValueAsync(Convert.ToDouble(_value, CultureInfo.InvariantCulture), cancellationToken));

            case JTokenType.String:
                return(writer.WriteValueAsync(_value?.ToString(), cancellationToken));

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

            case JTokenType.Date:
                if (_value is DateTimeOffset)
                {
                    return(writer.WriteValueAsync((DateTimeOffset)_value, cancellationToken));
                }

                return(writer.WriteValueAsync(Convert.ToDateTime(_value, CultureInfo.InvariantCulture), cancellationToken));

            case JTokenType.Bytes:
                return(writer.WriteValueAsync((byte[])_value, cancellationToken));

            case JTokenType.Guid:
                return(writer.WriteValueAsync(_value != null ? (Guid?)_value : null, cancellationToken));

            case JTokenType.TimeSpan:
                return(writer.WriteValueAsync(_value != null ? (TimeSpan?)_value : null, cancellationToken));

            case JTokenType.Uri:
                return(writer.WriteValueAsync((Uri)_value, cancellationToken));
            }

            throw MiscellaneousUtils.CreateArgumentOutOfRangeException("TokenType", _valueType, "Unexpected token type.");
        }
Beispiel #29
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)
        {
            if (converters != null && converters.Length > 0 && _value != null)
            {
                JsonConverter matchingConverter = JsonSerializer.GetMatchingConverter(converters, _value.GetType());
                if (matchingConverter != null && matchingConverter.CanWrite)
                {
                    matchingConverter.WriteJson(writer, _value, JsonSerializer.CreateDefault());
                    return;
                }
            }

            switch (_valueType)
            {
            case JTokenType.Comment:
                writer.WriteComment((_value != null) ? _value.ToString() : null);
                return;

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

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

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

            case JTokenType.Integer:
#if !(NET20 || NET35 || PORTABLE40 || PORTABLE)
                if (_value is BigInteger)
                {
                    writer.WriteValue((BigInteger)_value);
                }
                else
#endif
                writer.WriteValue(Convert.ToInt64(_value, CultureInfo.InvariantCulture));
                return;

            case JTokenType.Float:
                if (_value is decimal)
                {
                    writer.WriteValue((decimal)_value);
                }
                else if (_value is double)
                {
                    writer.WriteValue((double)_value);
                }
                else if (_value is float)
                {
                    writer.WriteValue((float)_value);
                }
                else
                {
                    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 !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.");
        }
Beispiel #30
0
        private void SetToken(JToken token)
        {
            switch (token.Type)
            {
            case JTokenType.Object:
                this.SetToken(JsonToken.StartObject);
                break;

            case JTokenType.Array:
                this.SetToken(JsonToken.StartArray);
                break;

            case JTokenType.Constructor:
                this.SetToken(JsonToken.StartConstructor, (object)((JConstructor)token).Name);
                break;

            case JTokenType.Property:
                this.SetToken(JsonToken.PropertyName, (object)((JProperty)token).Name);
                break;

            case JTokenType.Comment:
                this.SetToken(JsonToken.Comment, ((JValue)token).Value);
                break;

            case JTokenType.Integer:
                this.SetToken(JsonToken.Integer, ((JValue)token).Value);
                break;

            case JTokenType.Float:
                this.SetToken(JsonToken.Float, ((JValue)token).Value);
                break;

            case JTokenType.String:
                this.SetToken(JsonToken.String, ((JValue)token).Value);
                break;

            case JTokenType.Boolean:
                this.SetToken(JsonToken.Boolean, ((JValue)token).Value);
                break;

            case JTokenType.Null:
                this.SetToken(JsonToken.Null, ((JValue)token).Value);
                break;

            case JTokenType.Undefined:
                this.SetToken(JsonToken.Undefined, ((JValue)token).Value);
                break;

            case JTokenType.Date:
                this.SetToken(JsonToken.Date, ((JValue)token).Value);
                break;

            case JTokenType.Raw:
                this.SetToken(JsonToken.Raw, ((JValue)token).Value);
                break;

            case JTokenType.Bytes:
                this.SetToken(JsonToken.Bytes, ((JValue)token).Value);
                break;

            case JTokenType.Guid:
                this.SetToken(JsonToken.String, (object)this.SafeToString(((JValue)token).Value));
                break;

            case JTokenType.Uri:
                object obj = ((JValue)token).Value;
                Uri    uri = obj as Uri;
                this.SetToken(JsonToken.String,
                              uri != (Uri)null ? (object)uri.OriginalString : (object)this.SafeToString(obj));
                break;

            case JTokenType.TimeSpan:
                this.SetToken(JsonToken.String, (object)this.SafeToString(((JValue)token).Value));
                break;

            default:
                throw MiscellaneousUtils.CreateArgumentOutOfRangeException("Type", (object)token.Type,
                                                                           "Unexpected JTokenType.");
            }
        }