Beispiel #1
0
        public static T Deserialize <T>(string serializedData, string format)
        {
            if (string.IsNullOrWhiteSpace(format) == true)
            {
                format = "json";
            }

            switch (format.Trim().ToLowerInvariant())
            {
            case "json":
                return(JsonSerializer.DeserializeFromString <T>(serializedData));

            case "csv":
                System.IO.MemoryStream ms = new System.IO.MemoryStream(Encoding.UTF8.GetBytes(serializedData));

                return(CsvSerializer.DeserializeFromStream <T>(ms));

            case "jsv":
                return(TypeSerializer.DeserializeFromString <T>(serializedData));

            case "xml":
                return(XmlSerializer.DeserializeFromString <T>(serializedData));
            }

            throw new Exception("No format could be found for the data");
        }
        public object SerializeAndDeserialize <T>(T data)
        {
            var csv = CsvSerializer.SerializeToString(data);

            csv.Print();

            var dto = CsvSerializer.DeserializeFromString <T>(csv);

            AssertEqual(dto, data);

            using (var reader = new StringReader(csv))
            {
                dto = CsvSerializer.DeserializeFromReader <T>(reader);
                AssertEqual(dto, data);
            }

            using (var ms = new MemoryStream(csv.ToUtf8Bytes()))
            {
                dto = CsvSerializer.DeserializeFromStream <T>(ms);
                AssertEqual(dto, data);
            }

            using (var ms = new MemoryStream(csv.ToUtf8Bytes()))
            {
                dto = (T)CsvSerializer.DeserializeFromStream(typeof(T), ms);
                AssertEqual(dto, data);
            }

            return(dto);
        }
Beispiel #3
0
 private void ImportMatchesToContext()
 {
     using (var fileStream = new FileStream(filename, FileMode.Open))
     {
         CsvSerializer.UseEncoding     = Encoding.UTF8;
         CsvConfig.ItemSeperatorString = ";";
         var rkiMatches = CsvSerializer.DeserializeFromStream <List <RkiMatchImportRecord> >(fileStream);
         foreach (var rkiMatch in rkiMatches.Where(r => r.klhi_nr.HasValue))
         {
             var dbMatch = context.Sendings.SingleOrDefault(i => i.Isolate.StemNumber == rkiMatch.klhi_nr);
             if (dbMatch == null && rkiMatch.Jahr.HasValue && rkiMatch.LaufendeNummer.HasValue)
             {
                 dbMatch = context.Sendings.SingleOrDefault(s =>
                                                            s.Isolate.YearlySequentialIsolateNumber == rkiMatch.LaufendeNummer &&
                                                            s.Isolate.Year == rkiMatch.Jahr);
             }
             if (dbMatch != null && dbMatch.ReceivingDate >= MinImportDate && rkiMatch.InterneRef.HasValue)
             {
                 if (dbMatch.RkiMatchRecord == null)
                 {
                     dbMatch.RkiMatchRecord = new RkiMatchRecord {
                         RkiStatus = (RkiStatus)(-1)
                     };
                 }
                 var status = EnumSerializer.DeserializeEnumStrict <RkiStatus>(rkiMatch.StatusName);
                 if (dbMatch.RkiMatchRecord.RkiStatus <= status)
                 {
                     dbMatch.RkiMatchRecord.RkiReferenceId     = rkiMatch.InterneRef.Value;
                     dbMatch.RkiMatchRecord.RkiStatus          = status;
                     dbMatch.RkiMatchRecord.RkiReferenceNumber = rkiMatch.Aktenzeichen;
                 }
             }
         }
     }
 }
Beispiel #4
0
 public static List <KNode> GetKlines()
 {
     return
         (CsvSerializer.DeserializeFromStream <List <KNode> >(
              File.OpenRead(@"C:\Users\dy45\Documents\Visual Studio 2012\Projects\Dhyana\Dhyana\600015.csv"))
          .Where(item => item.Low > 0 && item.High > 0)
          .OrderBy(item => item.InDate)
          .ToList());
 }
Beispiel #5
0
        protected RouteModel GetRouteFromRouteId(OpenArchive gtfsArchive, string routeId)
        {
            if (routeList == null)
            {
                using (Stream routeStream = gtfsArchive.zip.GetEntry("google_transit/routes.csv").Open())
                {
                    routeList = CsvSerializer.DeserializeFromStream <List <RouteModel> >(routeStream);
                }
            }

            RouteModel routeDetails = routeList.FirstOrDefault(o => o.route_id == routeId);

            return(routeDetails);
        }
Beispiel #6
0
        protected TripModel GetTripFromTripId(OpenArchive gtfsArchive, int tripId)
        {
            if (tripList == null)
            {
                using (Stream tripStream = gtfsArchive.zip.GetEntry("google_transit/trips.csv").Open())
                {
                    tripList = CsvSerializer.DeserializeFromStream <List <TripModel> >(tripStream);
                }
            }

            TripModel tripDetails = tripList.FirstOrDefault(o => o.trip_id == tripId);

            return(tripDetails);
        }
Beispiel #7
0
        protected StopModel GetStopFromStopRef(OpenArchive gtfsArchive, string stopRef)
        {
            if (stopsList == null)
            {
                using (Stream stopsStream = gtfsArchive.zip.GetEntry("google_transit/stops.csv").Open())
                {
                    this.stopsList = CsvSerializer.DeserializeFromStream <List <StopModel> >(stopsStream);
                }
            }

            StopModel stopObj = stopsList.FirstOrDefault(o => o.stop_code == stopRef);

            return(stopObj);
        }
Beispiel #8
0
        /// <inheritdoc />
        public sealed override object DeepCopyObject(object source)
        {
            if (source == null)
            {
                return(null);
            }

            var type = source.GetType();

            using (var ms = MemoryStreamManager.GetStream())
            {
                CsvSerializer.SerializeToStream(source, ms);
                ms.Seek(0, System.IO.SeekOrigin.Begin);
                return(CsvSerializer.DeserializeFromStream(type, ms));
            }
        }
Beispiel #9
0
        /// <inheritdoc />
        public sealed override T ReadFromStream <T>(Stream readStream, Encoding effectiveEncoding)
        {
            if (null == readStream)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.readStream);
            }

            // 不是 Stream 都会实现 Position、Length 这两个属性
            //if (readStream.Position == readStream.Length) { return default(T); }

            try
            {
                return(CsvSerializer.DeserializeFromStream <T>(readStream));
            }
            catch (Exception ex)
            {
                s_logger.LogError(ex.ToString());
                return(default);
Beispiel #10
0
        /// <inheritdoc />
        public sealed override object ReadFromStream(Type type, Stream readStream, Encoding effectiveEncoding)
        {
            if (null == readStream)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.readStream);
            }

            // 不是 Stream 都会实现 Position、Length 这两个属性
            //if (readStream.Position == readStream.Length) { return GetDefaultValueForType(type); }

            try
            {
                return(CsvSerializer.DeserializeFromStream(type, readStream));
            }
            catch (Exception ex)
            {
                s_logger.LogError(ex.ToString());
                return(GetDefaultValueForType(type));
            }
        }
Beispiel #11
0
        async public Task <bool> IsUpdateAvaliable()
        {
            try
            {
                //check if its in the buffer
                OpenArchive gtfsArchive = GetArchiveFromBuffer(this.archiveFileName);
                if (gtfsArchive == null)
                {
                    string     completePath = this.fileMgr.FilePathRoot + this.archiveFileName;
                    ZipArchive zipFile      = this.fileMgr.GetZipFile(this.archiveFileName);
                    gtfsArchive = new OpenArchive(completePath, zipFile);
                    this.bufferedArchives.Add(gtfsArchive);
                }

                //done by checking if the feed version number is different
                List <FeedInfo> feedInfos = null;
                using (Stream infoStream = gtfsArchive.zip.GetEntry("google_transit/feed_info.csv").Open())
                {
                    feedInfos = CsvSerializer.DeserializeFromStream <List <FeedInfo> >(infoStream);
                }

                FeedInfo feedInfo     = feedInfos.First();
                int      localVersion = feedInfo.feed_version;

                //compare this local version with the online version
                byte[] feedFile = await DownloadFile(this.FeedInfoUrl);

                string          feedString      = Encoding.UTF8.GetString(feedFile);
                List <FeedInfo> onlineFeedInfos = CsvSerializer.DeserializeFromString <List <FeedInfo> >(feedString);
                FeedInfo        onlineFeedInfo  = onlineFeedInfos.First();
                int             onlineVersion   = onlineFeedInfo.feed_version;

                return(onlineVersion != localVersion);
            }
            catch
            {
                //failed somewhere with reading local file
                //return true so caller will update local version
                return(true);
            }
        }
Beispiel #12
0
 public override T DeserializeFromStream <T>(Stream stream)
 {
     return(CsvSerializer.DeserializeFromStream <T>(stream));
 }
Beispiel #13
0
 private void button5_Click(object sender, EventArgs e)
 {
     var csv    = CsvSerializer.SerializeToCsv(collection);
     var stream = new MemoryStream(Encoding.UTF8.GetBytes(csv ?? ""));
     var coll   = CsvSerializer.DeserializeFromStream(typeof(DynamicMessage), stream);
 }