示例#1
0
        private void SerializeValue(JsonWriter writer, object value)
        {
            //JsonConverter converter;
            _currentRecursionCounter++;
            if (RecursionLimit > 0 && _currentRecursionCounter > RecursionLimit) {
                throw new ArgumentException ("RecursionLimit exceeded.");
            }

            if (value == null) {
                writer.WriteNull ();
            } else {
                JavaScriptConverter jsconverter = _context.GetConverter (value.GetType ());
                if (jsconverter != null) {
                    value = jsconverter.Serialize (value, _context);
                    if (value == null) {
                        writer.WriteNull ();
                        return;
                    }
                }

                Type valueType = value.GetType ();
                switch (Type.GetTypeCode (valueType)) {
                case TypeCode.String:
                    writer.WriteValue ((string)value);
                    break;
                case TypeCode.Char:
                    writer.WriteValue ((char)value);
                    break;
                case TypeCode.Boolean:
                    writer.WriteValue ((bool)value);
                    break;
                case TypeCode.SByte:
                    writer.WriteValue ((sbyte)value);
                    break;
                case TypeCode.Int16:
                    writer.WriteValue ((short)value);
                    break;
                case TypeCode.UInt16:
                    writer.WriteValue ((ushort)value);
                    break;
                case TypeCode.Int32:
                    writer.WriteValue ((int)value);
                    break;
                case TypeCode.Byte:
                    writer.WriteValue ((byte)value);
                    break;
                case TypeCode.UInt32:
                    writer.WriteValue ((uint)value);
                    break;
                case TypeCode.Int64:
                    writer.WriteValue ((long)value);
                    break;
                case TypeCode.UInt64:
                    writer.WriteValue ((ulong)value);
                    break;
                case TypeCode.Single:
                    writer.WriteValue ((float)value);
                    break;
                case TypeCode.Double:
                    writer.WriteValue ((double)value);
                    break;
                case TypeCode.DateTime:
                    writer.WriteValue ((DateTime)value);
                    break;
                case TypeCode.Decimal:
                    writer.WriteValue ((decimal)value);
                    break;
                default:

                    ThrowOnReferenceLoop (writer, value);
                    writer.SerializeStack.Push (value);
                    try {
                        Type genDictType;
                        if (value is IDictionary)
                            SerializeDictionary (writer, (IDictionary)value);
                        else if (value is IDictionary<string, object>)
                            SerializeDictionary (writer, (IDictionary<string, object>)value, null);
                        else if ((genDictType = ReflectionUtils.GetGenericDictionary (valueType)) != null)
                            SerializeDictionary (writer, new GenericDictionaryLazyDictionary (value, genDictType), null);
                        else if (value is IEnumerable) {
                            SerializeEnumerable (writer, (IEnumerable)value);
                        } else {
                            SerializeCustomObject (writer, value, valueType);
                        }
                    } finally {

                        object x = writer.SerializeStack.Pop ();
                        if (x != value)
                            throw new InvalidOperationException ("Serialization stack is corrupted");
                    }

                    break;
                }
            }

            _currentRecursionCounter--;
        }
示例#2
0
 private void ThrowOnReferenceLoop(JsonWriter writer, object value)
 {
     switch (_referenceLoopHandling) {
     case ReferenceLoopHandling.Error:
         if (writer.SerializeStack.Contains (value))
             throw new JsonSerializationException ("Self referencing loop");
         break;
     case ReferenceLoopHandling.Ignore:
         // return from method
         return;
     case ReferenceLoopHandling.Serialize:
         // continue
         break;
     default:
         throw new InvalidOperationException (string.Format ("Unexpected ReferenceLoopHandling value: '{0}'", _referenceLoopHandling));
     }
 }
示例#3
0
        private void SerializeEnumerable(JsonWriter writer, IEnumerable values)
        {
            writer.WriteStartArray ();

            foreach (object value in values)
                SerializeValue (writer, value);

            writer.WriteEndArray ();
        }
示例#4
0
 private void SerializePair(JsonWriter writer, string key, object value)
 {
     writer.WritePropertyName (key);
     SerializeValue (writer, value);
 }
示例#5
0
        private void SerializeDictionary(JsonWriter writer, IDictionary<string, object> values, string typeID)
        {
            writer.WriteStartObject ();

            if (typeID != null) {
                SerializePair (writer, JavaScriptSerializer.SerializedTypeNameKey, typeID);
            }

            foreach (KeyValuePair<string, object> entry in values)
                SerializePair (writer, entry.Key, entry.Value);

            writer.WriteEndObject ();
        }
示例#6
0
        private void SerializeDictionary(JsonWriter writer, IDictionary values)
        {
            writer.WriteStartObject ();

            foreach (DictionaryEntry entry in values)
                SerializePair (writer, entry.Key.ToString (), entry.Value);

            writer.WriteEndObject ();
        }
示例#7
0
        private void SerializeCustomObject(JsonWriter writer, object value, Type valueType)
        {
            if (value is Uri) {
                Uri uri = value as Uri;
                writer.WriteValue (uri.GetComponents (UriComponents.AbsoluteUri, UriFormat.UriEscaped));
                return;
            }
            if (valueType == typeof(Guid)) {
                writer.WriteValue (((Guid)value).ToString ());
                return;
            }

            string typeID = null;
            if (_typeResolver != null) {
                typeID = _typeResolver.ResolveTypeId (valueType);
            }

            SerializeDictionary (writer, new SerializerLazyDictionary (value), typeID);
        }
示例#8
0
 /// <summary>
 /// Serializes the specified <see cref="Object"/> and writes the Json structure
 /// to a <c>Stream</c> using the specified <see cref="JsonWriter"/>. 
 /// </summary>
 /// <param name="jsonWriter">The <see cref="JsonWriter"/> used to write the Json structure.</param>
 /// <param name="value">The <see cref="Object"/> to serialize.</param>
 void Serialize(JsonWriter jsonWriter, object value)
 {
     SerializeValue (jsonWriter, value);
 }
示例#9
0
 public Stack(JsonWriter writer)
 {
     _list = new ArrayList ();
 }