JMSSerializerBundle icon indicating copy to clipboard operation
JMSSerializerBundle copied to clipboard

CSV Support

Open cojack opened this issue 12 years ago • 5 comments

There will be a support for CSV?

Regards.

cojack avatar Apr 24 '13 08:04 cojack

Not known until, now. But i could imagine, its a lot of work... But it is a different thing, cause a CSV is no key-value-pair.

I could imagine arrays or collections to be (de)serialized, maybe with an order property.

<?php
/**
 * User: maximilian
 * Date: 10/30/13
 * Time: 9:59 PM
 * 
 */

namespace JMS\Serializer\Tests\Fixtures;

/**
 * @CsvDef("text-seperator='"';field-seperator=';';charset='...'");
 * Class CsvArrayWrapper
 */
class CsvArrayWrapper {

    /**
     * Should store all values in a simple array
     * @Type("array<'string'>")
     * @var array
     */
    public $result;
} 

or for a collection of Objects

<?php
/**
 * User: maximilian
 * Date: 10/30/13
 * Time: 10:05 PM
 * 
 */

namespace JMS\Serializer\Tests\Fixtures;


/**
 * @CsvDef("text-seperator='"';field-seperator=';';charset='...'");
 * Class CsvArrayWrapper
 */

class CsvCollectionWrapper {


    /**
     * Should store all values in (for example) ArrayCollection of Objects
     * @Type("ArrayCollection<JMS\Serializer\Tests\Fixtures\CsvLineObject>")
     * @var array
     */
    public $result;
}

with:

<?php
/**
 * User: maximilian
 * Date: 10/30/13
 * Time: 10:08 PM
 * 
 */

namespace JMS\Serializer\Tests\Fixtures;

/**
 * @AccessorOrder("custom", custom = {"property3", "property2",...})
 * Class CsvLineObject
 */
class CsvLineObject {

    /**
     * @Type("string")
     */
    public $property1;

    /**
     * @Type("DateTime")
     */
    public $property2;

    /**
     * @Type("DateInterval")
     */
    public $property3;

    /**
     * @Type("integer")
     */
    public $property4;


} 

Problem will be: charakter encoding, which causes many problems with the field-/text-seperators

except of the charset thing, this should be easier to implement, cause it is one dimension less :-)

some more ideas? wanted?

ElectricMaxxx avatar Oct 30 '13 21:10 ElectricMaxxx

+1

vvh-empora avatar Sep 30 '14 09:09 vvh-empora

:+1:

ElectricMaxxx avatar Sep 30 '14 10:09 ElectricMaxxx

i have done it by this simple workaround: CSV -> ModelObject

at first, just convert the CSV to array, there are many solutions (i have use this lib https://github.com/jwage/easy-csv). after this, just convert it to json ...

$reader = new \EasyCSV\Reader($filename);
$modelList = $serializer->deserialize(json_encode($reader->getAll()), sprintf('array<%s>', $modelClass), 'json');

vvh-empora avatar Oct 01 '14 07:10 vvh-empora

There is a PR related to this over at the Serializer repo.

Potherca avatar Nov 26 '14 14:11 Potherca