protected override Boolean ImplSerializeEnumerable(
            out ArrayList values,
            IEnumerable objValue,
            IHierarchicalMetadataSerializationContext context)
        {
            values = new ArrayList();

            if (objValue == null)
            {
                return(false);
            }
            if (context == null)
            {
                return(false);
            }

            IValueSequence valueSeq = context.Hierarchy.Peek() as IValueSequence;

            if (valueSeq == null)
            {
                return(false);
            }

            IList ctxts = valueSeq.Value as IList;

            if (ctxts == null)
            {
                return(false);
            }

            IValueUnit ctxUnit  = null;
            Type       instType = null;

            context.Hierarchy.Pop();

            if ((ctxUnit = context.Hierarchy.Peek() as IValueUnit) == null)
            {
                return(false);
            }

            // Add SerMgr key
            if ((instType = typeof(SequenceType)).IsGenericType)
            {
                ctxUnit.Add("SerializationManager",
                            GetType().GetGenericTypeDefinition().AssemblyQualifiedName);

                ArrayList typeParamGuids = new ArrayList();

                foreach (Type par in instType.GenericTypeArguments)
                {
                    typeParamGuids.Add(par.GetTypeGuid().ToString());
                }

                ctxUnit.Add("SerializationManagerInstanceTypeParamGuids",
                            new ValueSequence(typeParamGuids));
            }
            else
            {
                return(false);// ctxUnit.Add ( "SerializationManager", instType.AssemblyQualifiedName );
            }
            context.Hierarchy.Push(valueSeq);

            foreach (KeyValuePair <SequenceKeyType, SequenceValueType> kvp in objValue)
            {
                ValueUnit kvpValueUnit = new ValueUnit();
                Object    tmpValue     = null;

                ctxts.Add(ctxUnit = new ValueUnit());

                context.Hierarchy.Push(ctxUnit);

                ImplSerializeItem(
                    out tmpValue,
                    //delegate ( IValueUnit vu ) { ctxUnit.Add ( nameof ( kvp.Key ), vu ); },
                    delegate() {
                    ValueUnit vu = new ValueUnit();

                    ctxUnit.Add(nameof(kvp.Key), vu);
                    context.Hierarchy.Push(vu);
                },
                    delegate() { context.Hierarchy.Pop(); },
                    kvp.Key,
                    context);

                kvpValueUnit.Add(new ValueItem(nameof(kvp.Key), tmpValue));

                ImplSerializeItem(
                    out tmpValue,
                    //delegate ( IValueUnit vu ) { ctxUnit.Add ( nameof ( kvp.Value ), vu ); },
                    delegate() {
                    ValueUnit vu = new ValueUnit();

                    ctxUnit.Add(nameof(kvp.Value), vu);
                    context.Hierarchy.Push(vu);
                },
                    delegate() { context.Hierarchy.Pop(); },
                    kvp.Value,
                    context);

                kvpValueUnit.Add(new ValueItem(nameof(kvp.Value), tmpValue));

                values.Add(kvpValueUnit);

                context.Hierarchy.Pop();
            }

            return(true);
        } // End of ImplSerializeEnumerable (...)
Пример #2
0
        } // End of ImplParseArray (...)

        protected virtual IValueUnit ImplParseUnit(
            out JsonScanner.TokenBase endToken,
            TextReader textReader)
        {
            endToken = JsonScanner.EofToken.Singleton;

            if (textReader == null)
            {
                return(null);
            }

            JsonScanner.NumericToken numericToken = null;
            String key = null;

            JsonScanner.TokenBase token  = null;
            ValueUnit             retVal = new ValueUnit();

            JsonScanner.StringToken strToken = null;
            Object value = null;
            IValueProvider <Object> valueProvider = null;

            do
            {
                token = JsonScanner.Singleton.ScanToken(textReader);

                if ((strToken = token as JsonScanner.StringToken) == null)
                {
                    return(null); // Unespected token
                }
                else
                {
                    key = strToken.Value;
                }

                token = JsonScanner.Singleton.ScanToken(textReader);

                if (token != JsonScanner.ColonToken.Singleton)
                {
                    return(null); // Unespected token
                }
                token = JsonScanner.Singleton.ScanToken(textReader);

                if ((strToken = token as JsonScanner.StringToken) != null)
                {
                    value = strToken.Value;
                }
                else if (token == JsonScanner.SquaredOpenToken.Singleton)
                {
                    value = ImplParseArray(out token, textReader);

                    if (token == JsonScanner.SquaredCloseToken.Singleton)
                    {
                        value = new ValueSequence((IEnumerable)value);
                    }
                    else
                    {
                        return(null);
                    }
                }
                else if (token == JsonScanner.CurlyOpenToken.Singleton)
                {
                    value = ImplParseUnit(out token, textReader);

                    if (token != JsonScanner.CurlyCloseToken.Singleton)
                    {
                        return(null);
                    }
                }
                else if ((numericToken = token as JsonScanner.NumericToken) != null)
                {
                    value = new NumericStringValue(numericToken.Value);
                }
                else if ((valueProvider = token as IValueProvider <Object>) != null)
                {
                    value = valueProvider.Value;
                }
                else
                {
                    return(null); // Unespected token
                }
                retVal.Add(new ValueItem(key, value));

                token = JsonScanner.Singleton.ScanToken(textReader);
            }while (token == JsonScanner.CommaToken.Singleton);

            endToken = token;

            return(retVal);
        } // End of ImplParseUnit (...)
        } // End of ImplSerializeEnumerable (...)

        protected virtual Boolean ImplSerializeEnumerable(
            out ArrayList values,
            IEnumerable objValue,
            IHierarchicalMetadataSerializationContext context)
        {
            values = new ArrayList();

            if (objValue == null)
            {
                return(false);
            }
            if (context == null)
            {
                return(false);
            }

            IValueSequence valueSeq = context.Hierarchy.Peek() as IValueSequence;

            if (valueSeq == null)
            {
                return(false);
            }

            IList ctxts = ((IValueItemBase)valueSeq).Value as IList;

            if (ctxts == null)
            {
                return(false);
            }

            IValueUnit ctxUnit    = null;
            Type       itemType   = null;
            Type       serMgrType = null;
            IValueUnit subUnit    = null;

            context.Hierarchy.Pop();

            try
            {
                if ((ctxUnit = context.Hierarchy.Peek() as IValueUnit) == null)
                {
                    return(false);
                }
                else if ((serMgrType = GetType()).IsGenericType)
                {
                    ctxUnit.Add("SerializationManager",
                                serMgrType.GetGenericTypeDefinition().AssemblyQualifiedName);

                    ArrayList typeParamGuids = new ArrayList();

                    foreach (Type par in serMgrType.GenericTypeArguments)
                    {
                        typeParamGuids.Add(par.GetTypeGuid().ToString());
                    }

                    ctxUnit.Add("SerializationManagerInstanceTypeParamGuids",
                                new ValueSequence(typeParamGuids));
                }
                else
                {
                    ctxUnit.Add("SerializationManager", serMgrType.AssemblyQualifiedName);
                }
            }
            finally { context.Hierarchy.Push(valueSeq); }

            foreach (Object item in objValue)
            {
                if (ImplIsBuiltinType(itemType = item.GetType()))
                {
                    ValueUnit vu = new ValueUnit();

                    vu.Add(new ValueItem("TypeGuid", itemType.GetTypeGuid().ToString()));
                    ctxts.Add(vu);
                    values.Add(item);
                }
                else if (ImplIsString(item.GetType()))
                {
                    ctxts.Add(null); values.Add(item);
                }
                //else if ( ImplSerializeEnumerable ( retVal, pd.Name, item as IEnumerable, context ) )
                //    continue;
                else if (ImplSerializeObject(
                             out subUnit,
                             //delegate ( IValueUnit vu ) { ctxts.Add ( vu ); },
                             delegate() {
                    ValueUnit vu = new ValueUnit();

                    ctxts.Add(vu);
                    context.Hierarchy.Push(vu);
                },
                             delegate() { context.Hierarchy.Pop(); },
                             item, //as IValueObjectContract,
                             context))
                {
                    values.Add(subUnit);
                }
            }

            return(true);
        } // End of ImplSerializeEnumerable (...)