public override void write(object obj, IProtocolFormatter formatter) { if (obj is IDictionary) { ITypeWriter typeWriter = MessageWriter.getWriter(typeof(IDictionary), formatter, false); typeWriter.write(obj, formatter); return; } IEnumerator en = (IEnumerator)obj; List <Object> arrayList = new List <Object>(); while (en.MoveNext()) { arrayList.Add(en.Current); } object[] array = arrayList.ToArray(); ITypeWriter writer = MessageWriter.getWriter(array.GetType(), formatter, false); writer.write(array, formatter); }
/* * public static Object writeObject( object obj ) * { * ITypeWriter writer = getWriter( obj ); * return writer.write( obj ); * } */ public static void writeObject(object obj, IProtocolFormatter formatter) { ITypeWriter typeWriter = getWriter(obj, formatter); typeWriter.write(obj, formatter); }
public override void write(object obj, IProtocolFormatter writer) { //Log.log( ORBConstants.INFO, "ObjectWriter.write.begin: " + writer.BaseStream.Length ); #if (FULL_BUILD) if (obj is IRemote) { remoteReferenceWriter.write(RemoteReferenceObject.createReference(obj), writer); return; } SerializationConfigHandler serializationConfig = (SerializationConfigHandler)ORBConfig.GetInstance().GetConfig("weborb/serialization"); if (!configured) { //serializePrivate = ThreadContext.getORBConfig().serializePrivateFields; if (serializationConfig != null) { serializePrivate = serializationConfig.SerializePrivateFields; } configured = true; } #endif Type objectClass = obj.GetType(); /* * if( obj is IAutoUpdate ) * { * //if( Weborb.Util.License.LicenseManager.GetInstance().IsStandardLicense() && !NetUtils.RequestIsLocal( ThreadContext.currentRequest() ) ) * // throw new Exception( "auto-update is disabled, this feature is available in WebORB Professional Edition" ); * * string id = Guid.NewGuid().ToString(); * objectFields[ "_orbid_" ] = id; * ThreadContext.currentHttpContext().Cache.Insert( id, new AutoUpdateObjectWrapper( ((IAutoUpdate) obj).GetUpdateHandler(), obj ) ); * } */ ClassDefinition classDef; cachedClassDefs.TryGetValue(objectClass, out classDef); Dictionary <String, Object> objectFields = new Dictionary <String, Object>(); if (classDef == null) { string className = objectClass.IsGenericType && objectClass.FullName != null ? objectClass.FullName.Substring(0, objectClass.FullName.IndexOf("`")) : objectClass.FullName; string clientSideMapping = GetClientClass(className); if (clientSideMapping != null) { // if( Log.isLogging( LoggingConstants.DEBUG ) ) // Log.log( LoggingConstants.DEBUG, "serializing a named object with client side mapping " + clientSideMapping ); className = clientSideMapping; } // Commented out this code in 4.0.0.4. This code does not make sense anymore // For one these mappings exist between ServiceNames (or Destination names) and // classes. A service class should not be serialized back to the client. // If it is serialized, user should use class mapping for that. //else //{ // className = ServiceRegistry.GetReverseMapping( className ); // if( Log.isLogging( LoggingConstants.DEBUG ) ) // Log.log( LoggingConstants.DEBUG, "serializing object " + className ); //} classDef = getClassDefinition(className, obj); lock (_sync) { cachedClassDefs[objectClass] = classDef; } // TODO: remove this try/catch //try //{ //cachedClassDefs[ objectClass ] = classDef; // } //catch ( Exception e ) // { //if ( Log.isLogging( LoggingConstants.ERROR ) ) // Log.log( LoggingConstants.ERROR, e ); //System.Diagnostics.Debugger.Launch(); //} } else { if (Log.isLogging(LoggingConstants.DEBUG)) { Log.log(LoggingConstants.DEBUG, "serializing using cached Class Def " + classDef.ClassName); } } Dictionary <string, MemberInfo> members = classDef.Members; foreach (KeyValuePair <string, MemberInfo> member in members) { Object val = null; if (member.Value is PropertyInfo) { try { val = ((PropertyInfo)member.Value).GetValue(obj, null); } catch (Exception exception) { if (Log.isLogging(LoggingConstants.ERROR)) { Log.log(LoggingConstants.ERROR, "Unable to retrieve property/field value from an instance of " + classDef.ClassName + ". Value will be set to null. Property name is " + member.Value.Name); } if (Log.isLogging(LoggingConstants.EXCEPTION)) { Log.log(LoggingConstants.EXCEPTION, exception); } } } else { val = ((FieldInfo)member.Value).GetValue(obj); } String memberName = member.Key; #if (FULL_BUILD) if (serializationConfig != null && serializationConfig.Keywords.Contains(memberName)) { memberName = serializationConfig.PrefixForKeywords + memberName; } #endif objectFields[memberName] = val; } onWriteObject(obj, classDef.ClassName, objectFields, writer); }
public void WriteObject(string className, IDictionary objectFields, IProtocolFormatter writer) { IEnumerator en = objectFields.Keys.GetEnumerator(); V3ReferenceCache cache = (V3ReferenceCache)writer.GetReferenceCache(); String traitsClassId = className; List <String> toRemove = new List <String>(); while (en.MoveNext()) { String fieldName = en.Current.ToString(); object obj = objectFields[fieldName]; if (obj != null && obj is ICollection && ((ICollection)obj).Count == 0) { toRemove.Add(fieldName); continue; } if (obj != null && obj.GetType().IsArray&& ((Object[])obj).Length == 0) { toRemove.Add(fieldName); continue; } } foreach (Object key in toRemove) { objectFields.Remove(key); } en = objectFields.Keys.GetEnumerator(); if (traitsClassId == null) { StringBuilder sb = new StringBuilder(); while (en.MoveNext()) { sb.Append(en.Current.ToString()); sb.Append("-"); } traitsClassId = sb.ToString(); en.Reset(); } if (cache.HasTraits(traitsClassId)) { writer.DirectWriteBytes(new byte[] { (byte)Datatypes.OBJECT_DATATYPE_V3 }); int traitId = (int)cache.GetTraitsId(traitsClassId); byte[] bytes = FlashorbBinaryWriter.GetVarIntBytes(0x1 | traitId << 2); writer.DirectWriteBytes(bytes); } else { writer.BeginWriteNamedObject(className, objectFields.Count); if (className == null) { cache.AddToTraitsCache(traitsClassId); } while (en.MoveNext()) { string fieldName = en.Current.ToString(); if (Log.isLogging(LoggingConstants.DEBUG)) { Log.log(LoggingConstants.DEBUG, "serializing property/field : " + fieldName); } writer.WriteFieldName(fieldName); } en.Reset(); } while (en.MoveNext()) { Object fieldName = en.Current; if (Log.isLogging(LoggingConstants.DEBUG)) { Log.log(LoggingConstants.DEBUG, "serializing property/field : " + fieldName); } //writer.BeginWriteFieldValue(); //try //{ //MessageWriter.writeObject( objectFields[ fieldName ], writer ); //Log.log( "REFCACHE", "WRITING FIELD " + fieldName ); object obj = objectFields[fieldName]; if (obj != null && obj is ICollection && ((ICollection)obj).Count == 0) { continue; } if (obj != null && obj.GetType().IsArray&& ((Object[])obj).Length == 0) { continue; } ITypeWriter typeWriter = MessageWriter.getWriter(obj, writer); typeWriter.write(obj, writer); //Log.log( "REFCACHE", "DONE WRITING FIELD " + fieldName ); //} //catch( Exception exception ) //{ // if( Log.isLogging( LoggingConstants.ERROR ) ) // Log.log( LoggingConstants.ERROR, "unable to serialize object's field " + fieldName, exception ); //} //finally //{ // writer.EndWriteFieldValue(); //} } writer.EndWriteNamedObject(); }
public void write(object obj, IProtocolFormatter writer) { Array arrayObj = null; if (obj is IWebORBArray) { IEnumerator en = ((IWebORBArray)obj).GetEnumerator(); arrayObj = new object[((IWebORBArray)obj).Count]; int i = 0; while (en.MoveNext()) { arrayObj.SetValue(en.Current, i++); } } else { //TODO: test out this cast! arrayObj = (Array)obj; } if (obj.GetType().IsAssignableFrom(typeof(Byte[]))) { Byte[] byteArray = new byte[arrayObj.Length]; for (int i = 0; i < arrayObj.Length; i++) { byteArray[i] = (Byte)arrayObj.GetValue(i); } writer.WriteByteArray(byteArray); return; } if (arrayObj.Rank > 1) { multiDimArrayWriter.write(arrayObj, writer); return; } writer.BeginWriteArray(arrayObj.Length); bool genericArray = arrayObj.GetType().GetElementType().BaseType == null; if (genericArray) { for (int i = 0; i < arrayObj.Length; i++) { //Log.log( "REFCACHE", "WRITING ARRAY ELEMENT " + i ); object value = arrayObj.GetValue(i); ITypeWriter typeWriter = MessageWriter.getWriter(value, writer); typeWriter.write(value, writer); //Log.log( "REFCACHE", "DONE WRITING ARRAY ELEMENT " + i ); } } else { ITypeWriter typeWriter = null; ITypeWriter contextWriter = null; for (int i = 0; i < arrayObj.Length; i++) { object value = arrayObj.GetValue(i); if (contextWriter == null) { typeWriter = MessageWriter.getWriter(value, writer); contextWriter = writer.getContextWriter(); } else { writer.setContextWriter(contextWriter); } typeWriter.write(value, writer); } } writer.EndWriteArray(); }