Comments (9)
So update, you're right, that with that parameter object[] it serializes empty lines. Originally I had the serialization logic outside that method.
Also after changing it to await using var csvWriter = CsvDataWriter.Create(outPath);
it works 🎉
I'm testing it on my second laptop with real data that I cannot share here.
So it looks like it works when using the API properly, the only question is why there is no warning in VS that I should be using await using
with that type? Usually VS is quite vocal when I miss using for a disposable type somewhere.
from sylvan.
This is almost certainly due to not flushing/closing one of the streams properly. Show me some code the reproduces the issue.
from sylvan.
Method I used for the serialization:
async Task WriteCsvToFile(string outPath, object[] o)
{
await using DbDataReader recordReader = o.AsDataReader();
var csvWriter = CsvDataWriter.Create(outPath);
await csvWriter.WriteAsync(recordReader);
}
The record structure I serialized had only two columns, one stringId, second string with object serialized as json, so the CsvDataWriter had to do lots of escapings.
Edit, fixed typo.
from sylvan.
This code doesn't compile. object[].AsDataReader() wouldn't write any records either, because the type (T) would be object, which has no properties. So, I'm guessing your real code is more complicated than this example.
from sylvan.
It compiles, targeting .net 6.0 the project uses Sylvan.Data.Csv 1.1.16 and Sylvan.Data 0.2.3.
I'm using the method for serializing array of anonymous types.
from sylvan.
It compiles
"recordREader" vs "recordReader"
I don't believe you.
from sylvan.
Ok, fixed the typo :)
from sylvan.
Try changing:
var csvWriter = CsvDataWriter.Create(outPath);
to
using var csvWriter = CsvDataWriter.Create(outPath);
from sylvan.
why there is no warning in VS
That's a good question, and I thought that the code analysis packages would provide this now, but apparently not.
If you want to have a helper method like this, it needs a generic type parameter (T
) to allow the AsDataReader to discover the properties:
async Task WriteAsync<T>(string filename, IEnumerable<T> data) where T : class
{
using var r = data.AsDataReader();
await using var w = CsvDataWriter.Create(filename);
await w.WriteAsync(r);
}
Glad to hear your problem is solved. If you find these libraries useful, please give the repository a star.
from sylvan.
Related Issues (20)
- As is. The Object to CSV Writer example doesn't compile HOT 1
- Clone a CsvDataReader HOT 1
- CsvDataWriter to append to a file HOT 1
- Configuration adjustments for CSV parsing HOT 1
- Records Count and Max Length of each Column HOT 1
- A 19kb record throws CsvRecordTooLargeException HOT 3
- Fields ending in escaped character return the wrong character HOT 1
- Minor unescape issue? HOT 2
- CompiledDataBinder: "Body of catch must have the same type as body of try" exception HOT 7
- Bulk Load CSV data into Postgres? HOT 1
- DbDataReader reads all rows as empty from IEnumerable<object> HOT 5
- The ObjectDataReader should use DataMember attribute to allow column name mapping HOT 1
- Encoding problem HOT 3
- DataTable Columns and Rows HOT 3
- Reading many csv files, checking the length of fields and merging them all into one file HOT 3
- CsvWriter to output to a .gz file HOT 1
- Invalid Examples for CSV Data Binding HOT 2
- CSV: RowNumber and line breaks inside fields HOT 1
- nullables issue, how to get the property names, and type info HOT 1
- Query: Nullable boolean CsvDataWriterOptions FalseString HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sylvan.