/// <summary> /// Builds the string serialization of this object depending on the given format /// </summary> /// <param name="format">serialization format</param> /// <returns>string serialization of this object if no error occurred; string.Empty otherwise</returns> public string Serialize(SerialFormat format) { switch (format) { case SerialFormat.JSON: return(jsonUtils.ToJSON(ToJSONEntity())); case SerialFormat.XML: return(ToXML(false).ToString()); default: return(string.Empty); } }
//public S2CSerializer(SerialFormat format, double execTime, ErrorCodes res, BaseEntity entityToSerialize) // : this(format, execTime, res) //{ // this.m_entityToSerialize = entityToSerialize; //} //public S2CSerializer(SerialFormat format, double execTime, ErrorCodes res, object[] objectsToSerialize) // : this(format, execTime, res) //{ // this.m_objectsToSerialize = objectsToSerialize; //} //public S2CSerializer(SerialFormat format, double execTime, ErrorCodes res, BaseEntity[] entitiesToSerialize) // : this(format, execTime, res) //{ // this.m_entitiesToSerialize = entitiesToSerialize; //} //public S2CSerializer(SerialFormat format, double execTime, ErrorCodes res, object objectToSerialize) // : this(format, execTime, res) //{ // this.m_objectToSerialize = objectToSerialize; //} //public S2CSerializer(SerialFormat format, long id, object objectToSerialize) // : this(format, id) //{ // this.m_objectToSerialize = objectToSerialize; //} //public S2CSerializer(SerialFormat format, long id, object[] objectsToSerialize) // : this(format, id) //{ // this.m_objectsToSerialize = objectsToSerialize; //} //public S2CSerializer(SerialFormat format, long id, BaseEntity entityToSerialize) // : this(format, id) //{ // this.m_entityToSerialize = entityToSerialize; //} //public S2CSerializer(SerialFormat format, long id, BaseEntity[] entitiesToSerialize) // : this(format, id) //{ // this.m_entitiesToSerialize = entitiesToSerialize; //} #endregion ///////////////////////////////////////////////////////////////////////////////////////////////////// #region Serialization ///////////////////////////////////////////////////////////////////////////////////////////////////// public string SerializeBaseEntity <T>(T entityToSerialize) where T : BaseEntity { string resp = string.Empty; switch (m_format) { case SerialFormat.JSON: //set content: if (entityToSerialize == null) { return(string.Empty); } BaseEntity m_entityToSerialize = entityToSerialize.ToJSONEntity(); if (m_isClientSide) { return(jsonUtils.ToJSON(new S2CRequestBaseEntity <T>(m_id, m_entityToSerialize))); } else { return(jsonUtils.ToJSON(new S2CResBaseEntity <T>(m_execTime, m_res, m_entityToSerialize))); } case SerialFormat.XML: //set content: XElement data = new XElement(ConfigReader.S2CNS + "Data", new XAttribute("ContType", S2CContentType.S2CEntity)); if (entityToSerialize != null) { data.Add(entityToSerialize.ToXML(false)); } XDocument result = PrepareXML(data); return(result.ToString()); } return(string.Empty); }
///////////////////////////////////////////////////////////////////////////////////////////////////// #region Public API ///////////////////////////////////////////////////////////////////////////////////////////////////// public string Serialize() { string resp = string.Empty; switch (Format) { case SerialFormat.JSON: //set content: if (m_entityToSerialize != null) { Data = m_entityToSerialize.ToJSONEntity(); } else if (m_entitiesToSerialize != null) { List <BaseEntity> dataList = new List <BaseEntity>(); foreach (BaseEntity be in m_entitiesToSerialize) { dataList.Add(be.ToJSONEntity()); } Data = dataList; } return(jsonUtils.ToJSON(this)); case SerialFormat.XML: //set content: XElement data = new XElement(ConfigReader.S2CNS + "Data", new XAttribute("ContType", ContentType)); switch (ContentType) { case S2CContentType.S2CEntity: if (m_entityToSerialize != null) { data.Add(m_entityToSerialize.ToXML(false)); } break; case S2CContentType.ListS2CEntity: if (m_entitiesToSerialize != null) { foreach (BaseEntity be in m_entitiesToSerialize) { data.Add(be.ToXML(false)); } } break; case S2CContentType.Obj: if (m_objectToSerialize != null) { data.SetValue(m_objectToSerialize); } break; case S2CContentType.ListObj: if (m_objectsToSerialize != null) { foreach (object toSer in m_objectsToSerialize) { data.Add(new XElement(ConfigReader.S2CNS + "Obj", toSer)); } } break; } XDocument result = Utilities.CreateXMLDoc("S2CResponse", false); result.Root.Add( new XElement(ConfigReader.S2CNS + "Status", Status), new XElement(ConfigReader.S2CNS + "ExecTime", ExecTime), data ); return(result.ToString()); case SerialFormat.HTML: return(string.Empty); //TODO??? } return(string.Empty); }