Esempio n. 1
0
        public JsonWriter Value(object value)
        {
            if (value == null)
            {
                return(this.Value());
            }
            IConvertible convertible = value as IConvertible;

            if (convertible != null)
            {
                if (!this._EnumToNumber && value is Enum)
                {
                    this.PreValue().Append('"').Append(value.ToString()).Append('"');
                    return(this);
                }
                switch (convertible.GetTypeCode())
                {
                case TypeCode.Empty:
                case TypeCode.DBNull:
                    return(this.Value());

                case TypeCode.Boolean:
                    return(this.Value(convertible.ToBoolean(null)));

                case TypeCode.Char:
                    return(this.Value(convertible.ToChar(null)));

                case TypeCode.SByte:
                case TypeCode.Int16:
                case TypeCode.Int32:
                    this.PreValue().Append(convertible.ToInt32(null));
                    return(this);

                case TypeCode.Byte:
                case TypeCode.UInt16:
                case TypeCode.UInt32:
                    this.PreValue().Append(convertible.ToUInt32(null));
                    return(this);

                case TypeCode.Int64:
                    this.PreValue().Append(convertible.ToInt64(null));
                    return(this);

                case TypeCode.UInt64:
                    this.PreValue().Append(convertible.ToUInt64(null));
                    return(this);

                case TypeCode.Single:
                case TypeCode.Double:
                case TypeCode.Decimal:
                    this.PreValue().Append(convertible.ToString(NumberFormatInfo.InvariantInfo));
                    return(this);

                case TypeCode.DateTime:
                    return(this.Value(convertible.ToDateTime(null)));

                case TypeCode.String:
                    return(this.Value(value.ToString()));
                }
            }
            IToJson toJson = value as IToJson;

            if (toJson != null)
            {
                toJson.WriteTo(this);
                return(this);
            }
            JsonWriterEx ex = JsonWriterEx.GetEx(value.GetType());

            if (ex.IsSimpleType)
            {
                ex.Invoke(this, value);
                return(this);
            }
            if (!this._CheckLoopReference)
            {
                if (this._Depth > 32)
                {
                    Throw.NotSupported("对象过于复杂或存在循环引用", false);
                }
                this._Depth++;
                if (ex.IsCustomType)
                {
                    ex.WriteTo(this, value);
                }
                else
                {
                    ex.Invoke(this, value);
                }
                this._Depth--;
                return(this);
            }
            if (this._Loop.Contains(value))
            {
                this.PreValue().Append9(CharEx.PUndefined);
            }
            else
            {
                int index = this._Loop.Add(value);
                if (ex.IsCustomType)
                {
                    ex.WriteTo(this, value);
                }
                else
                {
                    ex.Invoke(this, value);
                }
                this._Loop.RemoveAt(index);
            }
            return(this);
        }
Esempio n. 2
0
 public JsonWriter Value(IToJson value)
 {
     value.WriteTo(this);
     return(this);
 }