Example #1
0
        // ----------------------------------------------------------------------------------------
        #endregion

        #region StructureValueType properties
        // ----------------------------------------------------------------------------------------
        // StructureValueType properties
        // ----------------------------------------------------------------------------------------

        // ----------------------------------------------------------------------------------------
        #endregion

        #region StructureValueType methods
        // ----------------------------------------------------------------------------------------
        // StructureValueType methods
        // ----------------------------------------------------------------------------------------
        /// <summary>
        /// Serializes the specified object.
        /// </summary>
        /// <param name="sb">The sb.</param>
        /// <param name="obj">The obj.</param>
        /// <param name="context">The context.</param>
        public override void Serialize(StringBuilder sb, object obj, SerializationContext context)
        {
            if (isNullableType)
            {
                if (obj == null)
                {
                    if (keyExpected)
                    {
                        sb.Append(Structure.QuotationMark);
                        sb.Append(Key);
                        sb.Append(Structure.QuotationColonNullValue);
                    }
                }
                else
                {
                    nullableUnderlyingTypeConverter.Serialize(sb, obj, context);
                }
            }
            else
            {
                if (keyExpected)
                {
                    sb.Append(Structure.QuotationMark);
                    sb.Append(Key);
                    sb.Append(Structure.QuotationColonSeparator);
                }

                if (useCultureSensitiveConvert)
                {
                    sb.Append(Convert.ChangeType(obj, typeof(string), CultureInfo.InvariantCulture));
                }
                else
                {
                    sb.Append(obj);
                }
            }
        }
Example #2
0
        // ----------------------------------------------------------------------------------------
        #endregion

        #region StructureArray properties
        // ----------------------------------------------------------------------------------------
        // StructureArray properties
        // ----------------------------------------------------------------------------------------

        // ----------------------------------------------------------------------------------------
        #endregion

        #region StructureArray methods
        // ----------------------------------------------------------------------------------------
        // StructureArray methods
        // ----------------------------------------------------------------------------------------

        /// <summary>
        /// Serializes the specified object.
        /// </summary>
        /// <param name="sb">The string builder.</param>
        /// <param name="obj">The object to serialize.</param>
        /// <param name="context">The context.</param>
        public override void Serialize(StringBuilder sb, object obj, SerializationContext context)
        {
            if (keyExpected)
            {
                sb.Append(Structure.QuotationMark);
                sb.Append(key);
                if (obj == null)
                {
                    sb.Append(Structure.QuotationColonNullValue);
                    return;
                }
                else
                {
                    sb.Append(Structure.QuotationColonSeparator);
                }
            }

            if (isByteArray)
            {
                // Optimization: add byte array as base64 encoded string
                sb.Append(Structure.CharQuotationMark);
                byte[] data = (byte[])obj;
                sb.Append(Convert.ToBase64String(data));
                sb.Append(Structure.CharQuotationMark);
            }
            else
            {
                sb.Append(Structure.CharLeftSquareBrace);


                //Type collType = obj.GetType();

                // check if own collection implementation exposes special target type // todo: check if possible and required
                //bool specialTypeAdded = false;
                //if (!collType.IsArray
                //    && !IsSystemCollection(collType))
                //    //&& !checkedSubInterfaceTypes.Contains(collType))
                //{
                //    var exposureAttributes = collType.GetCustomAttributes(typeof(ExposeSubTypeAttribute), false);
                //    if (exposureAttributes.Length > 0)
                //    {
                //        // expose specialized sub interface type
                //        if (this.typeSerializerCache == null)
                //            this.typeSerializerCache = new Dictionary<Type, IJsonTypeStructure>();

                //        // add typed collection attribute
                //        sb.Append(Structure.TypeMetaTagJson);
                //        sb.Append(this.type.FullName);
                //        sb.Append(Structure.CharQuotationMark);

                //        specialTypeAdded = true;
                //    }
                //    //checkedSubInterfaceTypes.Add(collType);
                //}

                IEnumerable collection = (IEnumerable)obj;

                int count = 0;
                foreach (var item in collection)
                {
                    //if (specialTypeAdded
                    //    && count == 0)
                    //{
                    //    sb.Append(Structure.CharComma);
                    //}

                    if (item != null)
                    {
                        if (isObject)
                        {
                            // determine serialize type
                            Type itemType = item.GetType();
                            IJsonTypeStructure currentObjectStructure;
                            if (!typeSerializerCache.TryGetValue(itemType, out currentObjectStructure))
                            {
                                Type targetType = null;
                                if (unknownTypeResolver != null)
                                {
                                    context.Key        = this.key;
                                    context.ArrayIndex = count;

                                    targetType = unknownTypeResolver(context);

                                    context.ArrayIndex = null;
                                }
                                if (targetType == null)
                                {
                                    targetType = itemType;
                                }
                                currentObjectStructure = Structure.DetermineStructure(targetType, GetNestedArrayKey(this.key, count), context, true);

                                typeSerializerCache[itemType] = currentObjectStructure;
                            }

                            currentObjectStructure.Serialize(sb, item, context);
                        }
                        else
                        {
                            if (itemSerializer == null)
                            {
                                this.itemSerializer = Structure.DetermineStructure(itemType, key, context, true);
                            }

                            // use static serialize type
                            itemSerializer.Serialize(sb, item, context);
                        }
                    }
                    else
                    {
                        sb.Append(Structure.NullValue);
                    }
                    sb.Append(Structure.CharComma);
                    count++;
                }
                if (count > 0)
                {
                    sb.Length -= 1; // remove last comma
                }

                sb.Append(Structure.CharRightSquareBracet);
            }
        }