/// <summary>
 /// Overloaded constructor, uses user specified delegates
 /// </summary>
 /// <param name="read">A <see cref="ReadObjectDelegate"/> object</param>
 /// <param name="write">A <see cref="WriteObjectDelegate"/> object</param>
 public DynamicValueTypeSurrogate(ReadObjectDelegate read, WriteObjectDelegate write)
     : base(typeof(T), null)
 {
     CommonConstruct();
     mReadMtd  = read;
     mWriteMtd = write;
 }
Esempio n. 2
0
        public static void WriteGenericIDictionary(
            TextWriter writer,
            IDictionary <TKey, TValue> map,
            WriteObjectDelegate writeKeyFn,
            WriteObjectDelegate writeValueFn)
        {
            writer.Write(JsWriter.MapStartChar);

            var ranOnce = false;

            foreach (var kvp in map)
            {
                JsWriter.WriteItemSeperatorIfRanOnce(writer, ref ranOnce);

                JsState.WritingKeyCount++;
                JsState.IsWritingValue = false;
                writeKeyFn(writer, kvp.Key);
                JsState.WritingKeyCount--;

                writer.Write(JsWriter.MapKeySeperator);

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

            writer.Write(JsWriter.MapEndChar);
        }
Esempio n. 3
0
        public static void WriteIEnumerable(TextWriter writer, object oValueCollection)
        {
            if (oValueCollection == null)
            {
                writer.Write(JsonUtils.Null);
                return;
            }

            WriteObjectDelegate toStringFn = null;
            Type lastType = null;

            writer.Write(JsWriter.ListStartChar);

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

            foreach (var valueItem in valueCollection)
            {
                var thisType = valueItem.GetType();

                if (toStringFn == null || thisType != lastType)
                {
                    toStringFn = Serializer.GetWriteFn(thisType);
                    lastType   = thisType;
                }

                JsWriter.WriteItemSeperatorIfRanOnce(writer, ref ranOnce);

                toStringFn(writer, valueItem);
            }

            writer.Write(JsWriter.ListEndChar);
        }
Esempio n. 4
0
        /// <summary>Writes a generic i dictionary.</summary>
        /// <param name="writer">      The writer.</param>
        /// <param name="map">         The map.</param>
        /// <param name="writeKeyFn">  The write key function.</param>
        /// <param name="writeValueFn">The write value function.</param>
        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.IncludeNullValues)
                {
                    continue;
                }

                JsWriter.WriteItemSeperatorIfRanOnce(writer, ref ranOnce);

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

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

                JsState.WritingKeyCount--;

                writer.Write(JsWriter.MapKeySeperator);

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

            writer.Write(JsWriter.MapEndChar);
        }
        static QueryStringWriter()
        {
            if (typeof(T) == typeof(object))
            {
                CacheFn = QueryStringSerializer.WriteLateBoundObject;
            }
            else if (typeof(T).AssignableFrom(typeof(IDictionary)) ||
                     typeof(T).HasInterface(typeof(IDictionary)))
            {
                CacheFn = WriteIDictionary;
            }
            else
            {
                var isEnumerable = typeof(T).AssignableFrom(typeof(IEnumerable)) ||
                                   typeof(T).HasInterface(typeof(IEnumerable));

                if ((typeof(T).IsClass() || typeof(T).IsInterface()) &&
                    !isEnumerable)
                {
                    var canWriteType = WriteType <T, JsvTypeSerializer> .Write;
                    if (canWriteType != null)
                    {
                        CacheFn = WriteType <T, JsvTypeSerializer> .WriteQueryString;
                        return;
                    }
                }

                CacheFn = QueryStringSerializer.Instance.GetWriteFn <T>();
            }
        }
Esempio n. 6
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.GetType() != lastType)
                {
                    lastType   = valueItem.GetType();
                    toStringFn = Serializer.GetWriteFn(lastType);
                }

                JsWriter.WriteItemSeperatorIfRanOnce(writer, ref ranOnce);

                toStringFn(writer, valueItem);
            }

            writer.Write(JsWriter.ListEndChar);
        }
Esempio n. 7
0
 public TypePropertyWriter(string propertyName,
                           Func <T, object> getterFn, WriteObjectDelegate writeFn)
 {
     this.PropertyName = propertyName;
     this.GetterFn     = getterFn;
     this.WriteFn      = writeFn;
 }
Esempio n. 8
0
        static WriteType()
        {
            if (typeof(T) == typeof(Object))
            {
                Write = WriteObjectType;
            }
            else
            {
                Write = Init() ? GetWriteFn() : WriteEmptyType;
            }

            if (IsIncluded)
            {
                WriteTypeInfo = TypeInfoWriter;
            }

            if (typeof(T).IsAbstract)
            {
                WriteTypeInfo = TypeInfoWriter;
                if (!JsConfig.PreferInterfaces || !typeof(T).IsInterface)
                {
                    Write = WriteAbstractProperties;
                }
            }
        }
Esempio n. 9
0
 public static void WriteIDictionary(
     TextWriter writer,
     object oMap,
     WriteObjectDelegate writeKeyFn,
     WriteObjectDelegate writeValueFn)
 {
     WriteGenericIDictionary(writer, (IDictionary <TKey, TValue>)oMap, writeKeyFn, writeValueFn);
 }
        public static void WriteIDictionary(TextWriter writer, object oMap)
        {
            WriteObjectDelegate writeKeyFn   = null;
            WriteObjectDelegate writeValueFn = null;

            try
            {
                JsState.QueryStringMode = true;

                var map     = (IDictionary)oMap;
                var ranOnce = false;
                foreach (var key in map.Keys)
                {
                    var dictionaryValue = map[key];
                    if (dictionaryValue == null)
                    {
                        continue;
                    }

                    if (writeKeyFn == null)
                    {
                        var keyType = key.GetType();
                        writeKeyFn = Serializer.GetWriteFn(keyType);
                    }

                    if (writeValueFn == null)
                    {
                        writeValueFn = Serializer.GetWriteFn(dictionaryValue.GetType());
                    }

                    if (ranOnce)
                    {
                        writer.Write("&");
                    }
                    else
                    {
                        ranOnce = true;
                    }

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

                    writeKeyFn(writer, key);

                    JsState.WritingKeyCount--;

                    writer.Write("=");

                    JsState.IsWritingValue = true;
                    writeValueFn(writer, dictionaryValue);
                    JsState.IsWritingValue = false;
                }
            }
            finally
            {
                JsState.QueryStringMode = false;
            }
        }
Esempio n. 11
0
        static WriteType()
        {
            CacheFn = Init() ? GetWriteFn() : WriteEmptyType;

            if (typeof(T).IsAbstract)
            {
                WriteTypeInfo = TypeInfoWriter;
            }
        }
Esempio n. 12
0
 public TypePropertyWriter(string propertyName, string propertyNameCLSFriendly,
                           Func <T, object> getterFn, WriteObjectDelegate writeFn, object defaultValue)
 {
     this.propertyName            = propertyName;
     this.propertyNameCLSFriendly = propertyNameCLSFriendly;
     this.GetterFn     = getterFn;
     this.WriteFn      = writeFn;
     this.DefaultValue = defaultValue;
 }
Esempio n. 13
0
        static CsvWriter()
        {
            if (typeof(T) == typeof(string))
            {
                OptimizedWriter = (w, o) => WriteRow(w, (IEnumerable <string>)o);
                return;
            }

            Reset();
        }
Esempio n. 14
0
 public static void WriteIDictionary(
     TextWriter writer,
     object oMap,
     WriteObjectDelegate writeKeyFn,
     WriteObjectDelegate writeValueFn)
 {
     if (writer == null)
     {
         return;                             //AOT
     }
     WriteGenericIDictionary(writer, (IDictionary <TKey, TValue>)oMap, writeKeyFn, writeValueFn);
 }
Esempio n. 15
0
        static WriteType()
        {
            CacheFn = Init() ? GetWriteFn() : WriteEmptyType;

            if (typeof(T).IsAbstract)
            {
                WriteTypeInfo = TypeInfoWriter;
                if (!typeof(T).IsInterface)
                {
                    CacheFn = WriteAbstractProperties;
                }
            }
        }
Esempio n. 16
0
 public TypePropertyWriter(string propertyName, string propertyReflectedName, string propertyNameCLSFriendly, string propertyNameLowercaseUnderscore, int propertyOrder, bool propertySuppressDefaultConfig, bool propertySuppressDefaultAttribute,
                           Func <T, object> getterFn, WriteObjectDelegate writeFn, object defaultValue)
 {
     this.propertyName  = propertyName;
     this.propertyOrder = propertyOrder;
     this.propertySuppressDefaultConfig    = propertySuppressDefaultConfig;
     this.propertySuppressDefaultAttribute = propertySuppressDefaultAttribute;
     this.propertyReflectedName            = propertyReflectedName;
     this.propertyCombinedNameUpper        = propertyReflectedName.ToUpper() + "." + propertyName.ToUpper();
     this.propertyNameCLSFriendly          = propertyNameCLSFriendly;
     this.propertyNameLowercaseUnderscore  = propertyNameLowercaseUnderscore;
     this.GetterFn     = getterFn;
     this.WriteFn      = writeFn;
     this.DefaultValue = defaultValue;
 }
Esempio n. 17
0
 public TypePropertyWriter(string propertyName, string propertyDeclaredTypeName, string propertyNameCLSFriendly,
                           string propertyNameLowercaseUnderscore, int propertyOrder, bool propertySuppressDefaultConfig, bool propertySuppressDefaultAttribute,
                           Func <T, object> getterFn, WriteObjectDelegate writeFn, object defaultValue, Func <T, bool> shouldSerialize)
 {
     this.propertyName  = propertyName;
     this.propertyOrder = propertyOrder;
     this.propertySuppressDefaultConfig    = propertySuppressDefaultConfig;
     this.propertySuppressDefaultAttribute = propertySuppressDefaultAttribute;
     this.propertyReferenceName            = propertyDeclaredTypeName + "." + propertyName;
     this.propertyNameCLSFriendly          = propertyNameCLSFriendly;
     this.propertyNameLowercaseUnderscore  = propertyNameLowercaseUnderscore;
     this.GetterFn        = getterFn;
     this.WriteFn         = writeFn;
     this.DefaultValue    = defaultValue;
     this.shouldSerialize = shouldSerialize;
 }
Esempio n. 18
0
        static WriteType()
        {
            CacheFn = Init() ? GetWriteFn() : WriteEmptyType;

            if (IsIncluded)
            {
                WriteTypeInfo = TypeInfoWriter;
            }
            if (typeof(T).IsAbstract)
            {
                WriteTypeInfo = TypeInfoWriter;
                if (!JsConfig.PreferInterfaces || !typeof(T).IsInterface)
                {
                    CacheFn = WriteAbstractProperties;
                }
            }
        }
Esempio n. 19
0
        static QueryStringWriter()
        {
            if (typeof(T) == typeof(object))
            {
                CacheFn = QueryStringSerializer.WriteLateBoundObject;
            }
            else
            {
                if (typeof(T).IsClass || typeof(T).IsInterface)
                {
                    var canWriteType = WriteType <T, JsvTypeSerializer> .Write;
                    if (canWriteType != null)
                    {
                        CacheFn = WriteType <T, JsvTypeSerializer> .WriteQueryString;
                        return;
                    }
                }

                CacheFn = QueryStringSerializer.Instance.GetWriteFn <T>();
            }
        }
Esempio n. 20
0
 public TypePropertyWriter(Type propertyType, string propertyName, string propertyDeclaredTypeName, string propertyNameCLSFriendly,
                           string propertyNameLowercaseUnderscore, int propertyOrder, bool propertySuppressDefaultConfig, bool propertySuppressDefaultAttribute,
                           GetMemberDelegate <T> getterFn, WriteObjectDelegate writeFn, object defaultValue,
                           Func <T, bool> shouldSerialize,
                           Func <T, string, bool?> shouldSerializeDynamic,
                           bool isEnum)
 {
     this.PropertyType  = propertyType;
     this.propertyName  = propertyName;
     this.propertyOrder = propertyOrder;
     this.propertySuppressDefaultConfig    = propertySuppressDefaultConfig;
     this.propertySuppressDefaultAttribute = propertySuppressDefaultAttribute;
     this.propertyReferenceName            = propertyDeclaredTypeName + "." + propertyName;
     this.propertyNameCLSFriendly          = propertyNameCLSFriendly;
     this.propertyNameLowercaseUnderscore  = propertyNameLowercaseUnderscore;
     this.GetterFn               = getterFn;
     this.WriteFn                = writeFn;
     this.DefaultValue           = defaultValue;
     this.shouldSerialize        = shouldSerialize;
     this.shouldSerializeDynamic = shouldSerializeDynamic;
     this.isEnum = isEnum;
 }
Esempio n. 21
0
        public static void WriteIDictionary(TextWriter writer, object oMap)
        {
            WriteObjectDelegate writeKeyFn   = null;
            WriteObjectDelegate writeValueFn = null;

            writer.Write(JsWriter.MapStartChar);

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

            foreach (var key in map.Keys)
            {
                var dictionaryValue = map[key];
                if (writeKeyFn == null)
                {
                    writeKeyFn = Serializer.GetWriteFn(key.GetType());
                }

                if (writeValueFn == null)
                {
                    writeValueFn = Serializer.GetWriteFn(dictionaryValue.GetType());
                }

                JsWriter.WriteItemSeperatorIfRanOnce(writer, ref ranOnce);

                JsState.WritingKeyCount++;
                JsState.IsWritingValue = false;
                writeKeyFn(writer, key);
                JsState.WritingKeyCount--;

                writer.Write(JsWriter.MapKeySeperator);

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

            writer.Write(JsWriter.MapEndChar);
        }
Esempio n. 22
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);
        }
Esempio n. 23
0
        public static void WriteIDictionary(TextWriter writer, object oMap)
        {
            WriteObjectDelegate writeKeyFn   = null;
            Type writeKeyFnType              = null;
            WriteObjectDelegate writeValueFn = null;

            writer.Write(JsWriter.MapStartChar);
            var encodeMapKey = false;

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

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

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

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

                if (!isNull)
                {
                    writeValueFn = Serializer.GetWriteFn(dictionaryValue.GetType());
                }

                JsWriter.WriteItemSeperatorIfRanOnce(writer, ref ranOnce);

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

                    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
                {
                    JsState.IsWritingValue = true;
                    try
                    {
                        writeValueFn(writer, dictionaryValue);
                    }
                    finally
                    {
                        JsState.IsWritingValue = false;
                    }
                }
            }

            writer.Write(JsWriter.MapEndChar);
        }
Esempio n. 24
0
 static WriteType()
 {
     CacheFn = Init() ? GetWriteFn() : WriteEmptyType;
 }
        public static void WriteIDictionary(TextWriter writer, object oMap)
        {
            WriteObjectDelegate writeKeyFn   = null;
            WriteObjectDelegate writeValueFn = null;

            try
            {
                JsState.QueryStringMode = true;

                var isObjectDictionary = typeof(T) == typeof(Dictionary <string, object>);
                var map     = (IDictionary)oMap;
                var ranOnce = false;
                foreach (var key in map.Keys)
                {
                    var dictionaryValue = map[key];
                    if (dictionaryValue == null)
                    {
                        continue;
                    }

                    if (writeKeyFn == null)
                    {
                        var keyType = key.GetType();
                        writeKeyFn = Serializer.GetWriteFn(keyType);
                    }

                    if (writeValueFn == null || isObjectDictionary)
                    {
                        writeValueFn = dictionaryValue is string
                                       ?(w, x) => w.Write(((string)x).UrlEncode())
                                           : Serializer.GetWriteFn(dictionaryValue.GetType());
                    }

                    if (ranOnce)
                    {
                        writer.Write("&");
                    }
                    else
                    {
                        ranOnce = true;
                    }

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

                        writeKeyFn(writer, key);
                    }
                    finally
                    {
                        JsState.WritingKeyCount--;
                    }

                    writer.Write("=");

                    JsState.IsWritingValue = true;
                    try
                    {
                        writeValueFn(writer, dictionaryValue);
                    }
                    finally
                    {
                        JsState.IsWritingValue = false;
                    }
                }
            }
            finally
            {
                JsState.QueryStringMode = false;
            }
        }
Esempio n. 26
0
 /// <summary>
 /// Construtor padrão.
 /// </summary>
 public DynamicValueTypeSurrogate() : base(typeof(T))
 {
     this.CommonConstruct();
     _readMethod  = DynamicSurrogateBuilder.CreateReaderDelegate(typeof(T));
     _writeMethod = DynamicSurrogateBuilder.CreateWriterDelegate(typeof(T));
 }
Esempio n. 27
0
 /// <summary>
 /// Cria uma instancia com o delegate dos métodos para leitura e escrita dos dados.
 /// </summary>
 /// <param name="read">Método usado para fazer a leitura dos dados.</param>
 /// <param name="write">Método usado para escreve os dados.</param>
 public DynamicValueTypeSurrogate(ReadObjectDelegate read, WriteObjectDelegate write) : base(typeof(T))
 {
     this.CommonConstruct();
     this._readMethod  = read;
     this._writeMethod = write;
 }
 /// <summary>
 /// Default constructor
 /// </summary>
 /// <remarks>
 /// This constructor uses Reflection.Emit to generate default reader and writer delegates for
 /// serialization of specified type.
 /// </remarks>
 public DynamicValueTypeSurrogate() : base(typeof(T), null)
 {
     CommonConstruct();
     mReadMtd  = DynamicSurrogateBuilder.CreateReaderDelegate(typeof(T));
     mWriteMtd = DynamicSurrogateBuilder.CreateWriterDelegate(typeof(T));
 }
Esempio n. 29
0
 static WriteType()
 {
     CacheFn = Init() ? GetWriteFn() : null;
 }