protected object MapHashtable(IEnumerator <Node> iter, Type valType, MappingStack mappingStack,
                                      MappingAction mappingAction, out Type mappedType)
        {
            mappedType = null;
            XmlRpcStruct retObj = new XmlRpcStruct();

            mappingStack.Push("struct mapped to XmlRpcStruct");
            try
            {
                while (iter.MoveNext() && iter.Current is StructMember)
                {
                    string rpcName = (iter.Current as StructMember).Value;
                    if (retObj.ContainsKey(rpcName) &&
                        !IgnoreDuplicateMembers)
                    {
                        throw new XmlRpcInvalidXmlRpcException(mappingStack.MappingType
                                                               + " contains struct value with duplicate member "
                                                               + rpcName
                                                               + " " + StackDump(mappingStack));
                    }
                    iter.MoveNext();

                    object value = OnStack(String.Format("member {0}", rpcName),
                                           mappingStack, delegate()
                    {
                        return(MapValueNode(iter, null, mappingStack, mappingAction));
                    });
                    if (!retObj.ContainsKey(rpcName))
                    {
                        retObj[rpcName] = value;
                    }
                }
            }
            finally
            {
                mappingStack.Pop();
            }
            return(retObj);
        }
Beispiel #2
0
 //#endif
 void Serialize(
     XmlWriter xtw,
     Object o,
     MappingActions mappingActions,
     List <object> nestedObjs)
 {
     if (nestedObjs.Contains(o))
     {
         throw new XmlRpcUnsupportedTypeException(nestedObjs[0].GetType(),
                                                  "Cannot serialize recursive data structure");
     }
     nestedObjs.Add(o);
     try
     {
         xtw.WriteStartElement("", "value", "");
         XmlRpcType xType = XmlRpcTypeInfo.GetXmlRpcType(o);
         if (xType == XmlRpcType.tArray)
         {
             xtw.WriteStartElement("", "array", "");
             xtw.WriteStartElement("", "data", "");
             Array a = (Array)o;
             foreach (Object aobj in a)
             {
                 //if (aobj == null)
                 //  throw new XmlRpcMappingSerializeException(String.Format(
                 //    "Items in array cannot be null ({0}[]).",
                 //o.GetType().GetElementType()));
                 Serialize(xtw, aobj, mappingActions, nestedObjs);
             }
             WriteFullEndElement(xtw);
             WriteFullEndElement(xtw);
         }
         else if (xType == XmlRpcType.tMultiDimArray)
         {
             Array mda     = (Array)o;
             int[] indices = new int[mda.Rank];
             BuildArrayXml(xtw, mda, 0, indices, mappingActions, nestedObjs);
         }
         else if (xType == XmlRpcType.tBase64)
         {
             byte[] buf = (byte[])o;
             xtw.WriteStartElement("", "base64", "");
             xtw.WriteBase64(buf, 0, buf.Length);
             WriteFullEndElement(xtw);
         }
         else if (xType == XmlRpcType.tBoolean)
         {
             bool boolVal = (bool)o;
             if (boolVal)
             {
                 WriteFullElementString(xtw, "boolean", "1");
             }
             else
             {
                 WriteFullElementString(xtw, "boolean", "0");
             }
         }
         else if (xType == XmlRpcType.tDateTime)
         {
             DateTime dt  = (DateTime)o;
             string   sdt = dt.ToString("yyyyMMdd'T'HH':'mm':'ss",
                                        DateTimeFormatInfo.InvariantInfo);
             WriteFullElementString(xtw, "dateTime.iso8601", sdt);
         }
         else if (xType == XmlRpcType.tDouble)
         {
             double doubleVal = (double)o;
             WriteFullElementString(xtw, "double", doubleVal.ToString(null,
                                                                      CultureInfo.InvariantCulture));
         }
         else if (xType == XmlRpcType.tHashtable)
         {
             xtw.WriteStartElement("", "struct", "");
             XmlRpcStruct xrs = o as XmlRpcStruct;
             foreach (object obj in xrs.Keys)
             {
                 string skey = obj as string;
                 xtw.WriteStartElement("", "member", "");
                 WriteFullElementString(xtw, "name", skey);
                 Serialize(xtw, xrs[skey], mappingActions, nestedObjs);
                 WriteFullEndElement(xtw);
             }
             WriteFullEndElement(xtw);
         }
         else if (xType == XmlRpcType.tInt32)
         {
             o = SerializeInt32(xtw, o, mappingActions);
         }
         else if (xType == XmlRpcType.tInt64)
         {
             o = SerializeInt64(xtw, o, mappingActions);
         }
         else if (xType == XmlRpcType.tString)
         {
             SerializeString(xtw, o);
         }
         else if (xType == XmlRpcType.tStruct)
         {
             MappingActions structActions
                 = GetMappingActions(o.GetType().GetTypeInfo(), mappingActions);
             xtw.WriteStartElement("", "struct", "");
             MemberInfo[] mis = o.GetType().GetMembers();
             foreach (MemberInfo mi in mis)
             {
                 if (mi.GetCustomAttribute <IgnoreDataMemberAttribute>() != null)
                 {
                     continue;
                 }
                 if (mi is FieldInfo)
                 {
                     FieldInfo fi      = (FieldInfo)mi;
                     string    member  = fi.Name;
                     Attribute attrchk = fi.GetCustomAttribute <XmlRpcMemberAttribute>();
                     if (attrchk != null && attrchk is XmlRpcMemberAttribute)
                     {
                         string mmbr = ((XmlRpcMemberAttribute)attrchk).Member;
                         if (mmbr != "")
                         {
                             member = mmbr;
                         }
                     }
                     MappingActions memberActions = MemberMappingActions(o.GetType(),
                                                                         fi.Name, structActions);
                     if (fi.GetValue(o) == null)
                     {
                         if (memberActions.NullMappingAction == NullMappingAction.Ignore)
                         {
                             continue;
                         }
                         else if (memberActions.NullMappingAction == NullMappingAction.Error)
                         {
                             throw new XmlRpcMappingSerializeException(@"Member """ + member +
                                                                       @""" of struct """ + o.GetType().Name + @""" cannot be null.");
                         }
                     }
                     xtw.WriteStartElement("", "member", "");
                     WriteFullElementString(xtw, "name", member);
                     Serialize(xtw, fi.GetValue(o), memberActions, nestedObjs);
                     WriteFullEndElement(xtw);
                 }
                 else if (mi is PropertyInfo)
                 {
                     PropertyInfo pi      = (PropertyInfo)mi;
                     string       member  = pi.Name;
                     Attribute    attrchk = pi.GetCustomAttribute <XmlRpcMemberAttribute>();
                     if (attrchk != null && attrchk is XmlRpcMemberAttribute)
                     {
                         string mmbr = ((XmlRpcMemberAttribute)attrchk).Member;
                         if (mmbr != "")
                         {
                             member = mmbr;
                         }
                     }
                     MappingActions memberActions = MemberMappingActions(o.GetType(),
                                                                         pi.Name, structActions);
                     if (pi.GetValue(o, null) == null)
                     {
                         if (memberActions.NullMappingAction == NullMappingAction.Ignore)
                         {
                             continue;
                         }
                         else if (memberActions.NullMappingAction == NullMappingAction.Error)
                         {
                             throw new XmlRpcMappingSerializeException(@"Member """ + member +
                                                                       @""" of struct """ + o.GetType().Name + @""" cannot be null.");
                         }
                     }
                     xtw.WriteStartElement("", "member", "");
                     WriteFullElementString(xtw, "name", member);
                     Serialize(xtw, pi.GetValue(o, null), memberActions, nestedObjs);
                     WriteFullEndElement(xtw);
                 }
             }
             WriteFullEndElement(xtw);
         }
         else if (xType == XmlRpcType.tVoid)
         {
             WriteFullElementString(xtw, "string", "");
         }
         else if (xType == XmlRpcType.tNil)
         {
             xtw.WriteStartElement("nil");
             WriteFullEndElement(xtw);
         }
         else
         {
             throw new XmlRpcUnsupportedTypeException(o.GetType());
         }
         WriteFullEndElement(xtw);
     }
     catch (global::System.NullReferenceException)
     {
         throw new XmlRpcNullReferenceException("Attempt to serialize data "
                                                + "containing null reference");
     }
     finally
     {
         nestedObjs.RemoveAt(nestedObjs.Count - 1);
     }
 }