New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to CsvEncoder to omit the header #27447

Open
tamasd opened this Issue May 31, 2018 · 6 comments

Comments

Projects
None yet
3 participants
@tamasd

tamasd commented May 31, 2018

In the project I am working on the application on the other end expects the CSV without the headers. It would be nice if I could have an option to omit the header.

@Aerendir

This comment has been minimized.

Aerendir commented Jun 3, 2018

Can you simply remove it with unset($array[0]) before sending the payload to the application on the other end?

@tamasd

This comment has been minimized.

tamasd commented Jun 4, 2018

Unfortunately not. I send the CSV data itself.

The serializer constructs the headers based on the array keys.

@Aerendir

This comment has been minimized.

Aerendir commented Jun 4, 2018

@tamasd , I think I'm not understanding: you send the CSV file as is or you send the CSV data after having processed the CSV file?

In the first case you should operate on the "other-app" side; if you send data contained in the CSV file, instead, you should remove the header before sending such data.

What is stopping you from removing the first line before processing the data in the CSV (either on your "client" side or on your "other-app" side): I'm missing this point...

@tamasd

This comment has been minimized.

tamasd commented Jun 4, 2018

To simplify the problem, I send the CSV content in the request body. Ideally, this encoder could encode my simple 2D array into CSV, and I can just send it as is. The "other app" is an external web API, I have no control over that. Basically, I store data in CSV format, and retrieve it (to display to the user), but the data might be changed by the "other app", and they don't need headers, since the format of the CSV is simple.

I can remove the first line of the resulting CSV, and add it back when I receive data, but it is a hacky solution. It would be much simplier if I could do something like $encoder->setHeaders(false);.

Currently I copied relevant parts from this class, but since we use a lot of Symfony components, it would be better if I could reuse this.

@Aerendir

This comment has been minimized.

Aerendir commented Jun 4, 2018

Ok, understood the problem.

Anyway, I think this is not a hacky way: you simply have to process the data for two different purposes:

  1. To send them to the API (without headers)
  2. To store/show data

I think this is not a useful option to be included in the symfony component.

I understand your point, but I think this is not a Serializer's purpose to manipulate data this way: it is your responsibility and you should use the Serializer component as is and then modify data to be sent to the API.

I don't understand why, to only strip out the header, you are copying the class just for this small feature.

In my opinion is much more correct to

  1. Get the array data from the serializer
  2. Save the header in a variable ($header = $data[0])
  3. Remove the header from the array (unset($data[0]))
  4. Send data
  5. Add the header back to the response array (array_unshift($responseArray, $header))
  6. Save

And, more, there is another thing I don't understand: in the database, you save the headers (at least I understand you do from your words): so this means that also if you will remove them with the Serializer, you anyway need to have them somewhere in your code...

@tamasd

This comment has been minimized.

tamasd commented Jun 4, 2018

It is the other way around. I already have my 2D string array, I convert it to CSV, so I get numbers as headers, basically an extra line of data at the beginning of the string I get from the serializer. I need to remove that line, and add it back when I try to read the data, to be able to use this serializer.

A simple illustration: let's say I have a very simple piece data: [["asdf"]]. If I encode this, I should get asdf only. Instead, I get this:

0
asdf

This happens the other way around as well. Let's say the "other app" has a piece of data with just asdf. If I try to decode this, I get an empty array as a result.

The problem here is that without manipulating the CSV string itself (which is something I am very uneasy to do, since it is the job of the encoder), I cause either data corruption or data loss.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment