public Serializer(SerializerType Type, string FileName, bool LoadOnCtor = false, bool AbsolutePath = true)
        {
            switch (Type)
            {
#if SERIALIZER_XML
            case SerializerType.Xml:
                DataSerializer = new XmlGenericSerializer <DATA_TYPE>();
                break;
#endif
#if SERIALIZER_JSON
            case SerializerType.Json:
                DataSerializer = new JsonGenericSerializer <DATA_TYPE>();
                break;
#endif
#if SERIALIZER_BIN
            case SerializerType.Bin:
                DataSerializer = new BinGenericSerializer <DATA_TYPE>();
                break;
#endif
#if SERIALIZER_YAML
            case SerializerType.Yaml:
                DataSerializer = new YamlGenericSerializer <DATA_TYPE>();
                break;
#endif
#if SERIALIZER_CSV
            case SerializerType.Csv:
                DataSerializer = new CsvGenericSerializer <DATA_TYPE>();
                break;
#endif
            default:
                DataSerializer = null;
                Data           = null;
                return;
            }

            if (!AbsolutePath)
            {
                string StartPath = AppDomain.CurrentDomain.BaseDirectory;
                FilePath = $@"{StartPath}\{FileName}.{Type.ToString().ToLowerInvariant()}";
            }
            else
            {
                FilePath = FileName;
            }

            if (LoadOnCtor)
            {
                Load();
            }
            else
            {
                Data = new DATA_TYPE();
            }
        }
Exemple #2
0
        private static string GetSubProtocol(SerializerType serializerType)
        {
            switch (serializerType)
            {
            case SerializerType.Json:
                return(WampSubProtocols.JsonSubProtocol);

            case SerializerType.MsgPack:
                return(WampSubProtocols.MsgPackSubProtocol);
            }

            return(serializerType.ToString());
        }
Exemple #3
0
        public static bool DeserializeFromText <T>(string textSerialized, ref T obj, SerializerType serialType = SerializerType.ntsJson, bool visualErr = true)
        {
            MemoryStream flussoInRam;
            string       prefissoErrLog, errUte;

            errUte = "";

            if (visualErr == true)
            {
                errUte = Log.main.errUserText;
            }

            if (obj == null)
            {
                Log.main.Add(new Mess(LogType.ERR, errUte, "Ricevuto oggetto a nothing, impossibile determinarne il tipo", visualMsgBox: false));
                return(false);
            }

            prefissoErrLog = "tipoSerial:<" + serialType.ToString() + ">, tipo oggetto:<" + obj.GetType().Name + ">";

            try
            {
                switch (serialType)
                {
                case SerializerType.ntsJson:      //ATTENZIONE quando si deserializza si crea un oggetto e quindi prima usa il costruttore che ha l'attributo JsonConstructor e poi valorizza le proprietà, questo comporta il doppio...
                    //passaggio nelle property del'oggetto, va comunque usato il costruttore "principale/standard" poichè se manca una property dalla config sul DB viene cmq valorizzata prima dal costruttore
                    obj = (T)JsonConvert.DeserializeObject(textSerialized, obj.GetType(), jsonSett);
                    break;

                case SerializerType.xml:
                    flussoInRam          = new MemoryStream(Encoding.UTF8.GetBytes(textSerialized));
                    flussoInRam.Position = 0;
                    XmlSerializer xmlDeserial = new XmlSerializer(obj.GetType());
                    obj = (T)xmlDeserial.Deserialize(flussoInRam);
                    break;

                case SerializerType.json:
                    flussoInRam          = new MemoryStream(Encoding.UTF8.GetBytes(textSerialized));
                    flussoInRam.Position = 0;
                    DataContractJsonSerializer jsonDeserial = new DataContractJsonSerializer(obj.GetType());
                    obj = (T)jsonDeserial.ReadObject(flussoInRam);
                    break;
                }
            }
            catch (Exception ex)
            {
                Log.main.Add(new Mess(LogType.ERR, errUte, "Eccezione " + prefissoErrLog + "ex.mess:<" + ex.Message + ">", visualMsgBox: false));
                return(false);
            }
            return(true);
        }
Exemple #4
0
        public static string GetSubProtocol(this SerializerType serializerType)
        {
            switch (serializerType)
            {
            case SerializerType.Json:
                return(WampSubProtocols.JsonSubProtocol);

            case SerializerType.MsgPack:
                return(WampSubProtocols.MsgPackSubProtocol);

            case SerializerType.Cbor:
                return(WampSubProtocols.CborSubProtocol);
            }

            return(serializerType.ToString());
        }
Exemple #5
0
        public static JsonSerializerSettings jsonSett; //= New JsonSerializerSettings() With {.TypeNameHandling = TypeNameHandling.Auto} '.ContractResolver = New MyContractResolver(),

        //Public Sub Initialize()
        //    jsonSett = New JsonSerializerSettings() With {.TypeNameHandling = TypeNameHandling.Auto}
        //End Sub

        public static bool SerializeInText(object obj, ref string text, SerializerType serialType = SerializerType.ntsJson, bool visualErr = true)
        {
            MemoryStream flussoInRam = new MemoryStream();
            string       prefissoErrLog, errUte;

            errUte = "";

            prefissoErrLog = "tipoSerial:<" + serialType.ToString() + ">, tipo oggetto:<" + obj.GetType().Name + ">";

            if (visualErr == true)
            {
                errUte = Log.main.errUserText;
            }

            try
            {
                switch (serialType)
                {
                case SerializerType.ntsJson:
                    text = JsonConvert.SerializeObject(obj, Formatting.Indented, jsonSett);
                    break;

                case SerializerType.xml:
                    var xmlSerial = new XmlSerializer(obj.GetType());
                    xmlSerial.Serialize(flussoInRam, obj);
                    flussoInRam.Position = 0;
                    text = new StreamReader(flussoInRam).ReadToEnd();
                    break;

                case SerializerType.json:
                    var jsonSerial = new DataContractJsonSerializer(obj.GetType());
                    jsonSerial.WriteObject(flussoInRam, obj);
                    flussoInRam.Position = 0;
                    text = new StreamReader(flussoInRam).ReadToEnd();
                    break;
                }
            }
            catch (Exception ex)
            {
                Log.main.Add(new Mess(LogType.ERR, errUte, "Eccezione " + prefissoErrLog + "ex.mess:<" + ex.Message + ">", visualMsgBox: false));
                return(false);
            }
            return(true);
        }
Exemple #6
0
        public Serializer(SerializerType Type, string FileName, bool LoadOnCtor = true, bool AbsolutePath = false)
        {
            switch (Type)
            {
            case SerializerType.Xml:
                DataSerializer = new XmlGenericSerializer <DATA_TYPE>();
                break;

            case SerializerType.Json:
                DataSerializer = new JsonGenericSerializer <DATA_TYPE>();
                break;

            case SerializerType.Bin:
                DataSerializer = new BinGenericSerializer <DATA_TYPE>();
                break;

            case SerializerType.Yaml:
                DataSerializer = new YamlGenericSerializer <DATA_TYPE>();
                break;

            case SerializerType.Csv:
                DataSerializer = new CsvGenericSerializer <DATA_TYPE>();
                break;
            }

            if (!AbsolutePath)
            {
                FilePath = $@"{Variables.AppPath}\{FileName}.{Type.ToString().ToLowerInvariant()}";
            }
            else
            {
                FilePath = FileName;
            }

            if (LoadOnCtor)
            {
                Load();
            }
            else
            {
                Data = new DATA_TYPE();
            }
        }
Exemple #7
0
        public ISerializer GetSerializer(SerializerType serializerType)
        {
            switch (serializerType)
            {
            case SerializerType.XML:
                return(_xSerializer);

            case SerializerType.JSON:
                return(_jSerializer);

            case SerializerType.CSV:
                return(_csvSerializer);

            default:
                throw new ArgumentException(string.Format(
                                                "{0} is not a valid {1}.",
                                                serializerType.ToString(),
                                                serializerType.GetType().FullName));
            }
        }
 public static string Name(this SerializerType x)
 {
     return(x.ToString());
 }
Exemple #9
0
 public ISerializer Resolve(SerializerType serializeType = SerializerType.Json)
 {
     return(this.uc.Resolve <ISerializer>(serializeType.ToString()));///XmlSerializer
 }