Code Monkey home page Code Monkey logo

Comments (9)

martinsuchan avatar martinsuchan commented on June 16, 2024 1

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.

MarkPflug avatar MarkPflug commented on June 16, 2024

This is almost certainly due to not flushing/closing one of the streams properly. Show me some code the reproduces the issue.

from sylvan.

martinsuchan avatar martinsuchan commented on June 16, 2024

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.

MarkPflug avatar MarkPflug commented on June 16, 2024

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.

martinsuchan avatar martinsuchan commented on June 16, 2024

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.

MarkPflug avatar MarkPflug commented on June 16, 2024

It compiles

"recordREader" vs "recordReader"
I don't believe you.

from sylvan.

martinsuchan avatar martinsuchan commented on June 16, 2024

Ok, fixed the typo :)

from sylvan.

MarkPflug avatar MarkPflug commented on June 16, 2024

Try changing:
var csvWriter = CsvDataWriter.Create(outPath);
to
using var csvWriter = CsvDataWriter.Create(outPath);

from sylvan.

MarkPflug avatar MarkPflug commented on June 16, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.