Example #1
0
        /// <summary>Writes a string array.</summary>
        /// <param name="serializer">The serializer.</param>
        /// <param name="writer">    The writer.</param>
        /// <param name="oList">     The list.</param>
        public static void WriteStringArray(ITypeSerializer serializer, TextWriter writer, object oList)
        {
            writer.Write(JsWriter.ListStartChar);

            var list       = (string[])oList;
            var ranOnce    = false;
            var listLength = list.Length;

            for (var i = 0; i < listLength; i++)
            {
                JsWriter.WriteItemSeperatorIfRanOnce(writer, ref ranOnce);
                serializer.WriteString(writer, list[i]);
            }

            writer.Write(JsWriter.ListEndChar);
        }
Example #2
0
        /// <summary>Writes an i enumerable.</summary>
        /// <param name="writer">          The writer.</param>
        /// <param name="oValueCollection">Collection of values.</param>
        public static void WriteIEnumerable(TextWriter writer, object oValueCollection)
        {
            WriteObjectDelegate toStringFn = null;

            writer.Write(JsWriter.ListStartChar);

            var valueCollection = (IEnumerable)oValueCollection;
            var ranOnce         = false;

            foreach (var valueItem in valueCollection)
            {
                if (toStringFn == null)
                {
                    toStringFn = Serializer.GetWriteFn(valueItem.GetType());
                }

                JsWriter.WriteItemSeperatorIfRanOnce(writer, ref ranOnce);

                toStringFn(writer, valueItem);
            }

            writer.Write(JsWriter.ListEndChar);
        }
Example #3
0
        /// <summary>Writes an i dictionary.</summary>
        /// <param name="writer">The writer.</param>
        /// <param name="oMap">  The map.</param>
        public static void WriteIDictionary(TextWriter writer, object oMap)
        {
            WriteObjectDelegate writeKeyFn   = null;
            WriteObjectDelegate writeValueFn = null;

            writer.Write(JsWriter.MapStartChar);
            var  encodeMapKey  = false;
            Type lastKeyType   = null;
            Type lastValueType = null;

            var map     = (IDictionary)oMap;
            var ranOnce = false;

            foreach (var key in map.Keys)
            {
                var dictionaryValue = map[key];

                var isNull = (dictionaryValue == null);
                if (isNull && !Serializer.IncludeNullValues)
                {
                    continue;
                }

                var keyType = key.GetType();
                if (writeKeyFn == null || lastKeyType != keyType)
                {
                    lastKeyType  = keyType;
                    writeKeyFn   = Serializer.GetWriteFn(keyType);
                    encodeMapKey = Serializer.GetTypeInfo(keyType).EncodeMapKey;
                }

                JsWriter.WriteItemSeperatorIfRanOnce(writer, ref ranOnce);

                JsState.WritingKeyCount++;
                JsState.IsWritingValue = false;

                if (encodeMapKey)
                {
                    JsState.IsWritingValue = true; //prevent ""null""
                    writer.Write(JsWriter.QuoteChar);
                    writeKeyFn(writer, key);
                    writer.Write(JsWriter.QuoteChar);
                }
                else
                {
                    writeKeyFn(writer, key);
                }

                JsState.WritingKeyCount--;

                writer.Write(JsWriter.MapKeySeperator);

                if (isNull)
                {
                    writer.Write(JsonUtils.Null);
                }
                else
                {
                    var valueType = dictionaryValue.GetType();
                    if (writeValueFn == null || lastValueType != valueType)
                    {
                        lastValueType = valueType;
                        writeValueFn  = Serializer.GetWriteFn(valueType);
                    }

                    JsState.IsWritingValue = true;
                    writeValueFn(writer, dictionaryValue);
                    JsState.IsWritingValue = false;
                }
            }

            writer.Write(JsWriter.MapEndChar);
        }