Example #1
0
        public static void WriteListString(ITypeSerializer serializer, TextWriter writer, List <string> list)
        {
            writer.Write(JsWriter.ListStartChar);

            var ranOnce = false;

            foreach (var x in list)
            {
                JsWriter.WriteItemSeperatorIfRanOnce(writer, ref ranOnce);
                serializer.WriteString(writer, x);
            }

            writer.Write(JsWriter.ListEndChar);
        }
Example #2
0
        public static void WriteIListString(ITypeSerializer serializer, TextWriter writer, IList <string> list)
        {
            writer.Write(JsWriter.ListStartChar);

            var ranOnce    = false;
            var listLength = list.Count;

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

            writer.Write(JsWriter.ListEndChar);
        }
Example #3
0
        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 #4
0
        public static void WriteIEnumerable(TextWriter writer, object oValueCollection)
        {
            WriteObjectDelegate toStringFn = null;

            writer.Write(JsWriter.ListStartChar);

            var  valueCollection = (IEnumerable)oValueCollection;
            var  ranOnce         = false;
            Type lastType        = null;

            foreach (var valueItem in valueCollection)
            {
                if ((toStringFn == null) || (valueItem != null && valueItem.GetType() != lastType))
                {
                    if (valueItem != null)
                    {
                        if (valueItem.GetType() != lastType)
                        {
                            lastType   = valueItem.GetType();
                            toStringFn = Serializer.GetWriteFn(lastType);
                        }
                    }
                    else
                    {
                        // this can happen if the first item in the collection was null
                        lastType   = typeof(object);
                        toStringFn = Serializer.GetWriteFn(lastType);
                    }
                }

                JsWriter.WriteItemSeperatorIfRanOnce(writer, ref ranOnce);

                toStringFn(writer, valueItem);
            }

            writer.Write(JsWriter.ListEndChar);
        }
Example #5
0
        public static void WriteGenericIDictionary(
            TextWriter writer,
            IDictionary <TKey, TValue> map,
            WriteObjectDelegate writeKeyFn,
            WriteObjectDelegate writeValueFn)
        {
            if (map == null)
            {
                writer.Write(JsonUtils.Null);
                return;
            }
            writer.Write(JsWriter.MapStartChar);

            var encodeMapKey = Serializer.GetTypeInfo(typeof(TKey)).EncodeMapKey;

            var ranOnce = false;

            foreach (var kvp in map)
            {
                var isNull = (kvp.Value == null);
                if (isNull && !Serializer.IncludeNullValuesInDictionaries)
                {
                    continue;
                }

                JsWriter.WriteItemSeperatorIfRanOnce(writer, ref ranOnce);

                JsState.WritingKeyCount++;
                try
                {
                    if (encodeMapKey)
                    {
                        JsState.IsWritingValue = true; //prevent ""null""
                        try
                        {
                            writer.Write(JsWriter.QuoteChar);
                            writeKeyFn(writer, kvp.Key);
                            writer.Write(JsWriter.QuoteChar);
                        }
                        finally
                        {
                            JsState.IsWritingValue = false;
                        }
                    }
                    else
                    {
                        writeKeyFn(writer, kvp.Key);
                    }
                }
                finally
                {
                    JsState.WritingKeyCount--;
                }

                writer.Write(JsWriter.MapKeySeperator);

                if (isNull)
                {
                    writer.Write(JsonUtils.Null);
                }
                else
                {
                    JsState.IsWritingValue = true;
                    try
                    {
                        writeValueFn(writer, kvp.Value);
                    }
                    finally
                    {
                        JsState.IsWritingValue = false;
                    }
                }
            }

            writer.Write(JsWriter.MapEndChar);
        }
Example #6
0
        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.IncludeNullValuesInDictionaries)
                {
                    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++;
                try
                {
                    if (encodeMapKey)
                    {
                        JsState.IsWritingValue = true; //prevent ""null""
                        try
                        {
                            writer.Write(JsWriter.QuoteChar);
                            writeKeyFn(writer, key);
                            writer.Write(JsWriter.QuoteChar);
                        }
                        finally
                        {
                            JsState.IsWritingValue = false;
                        }
                    }
                    else
                    {
                        writeKeyFn(writer, key);
                    }
                }
                finally
                {
                    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;
                    try
                    {
                        writeValueFn(writer, dictionaryValue);
                    }
                    finally
                    {
                        JsState.IsWritingValue = false;
                    }
                }
            }

            writer.Write(JsWriter.MapEndChar);
        }