JsonGenerationException:CSV生成器不支持属性的Object值

时间:2016-05-31 17:04:33

标签: java csv serialization jackson jackson-modules

我正在尝试将int main() { // If you need to use this class in something other than main, // you will need to move it outside of main. class shape { public: RectangleShape rect1; }; // But in this particular case you don't even need a class, // why not just use RectangleShape? shape getShape; getShape.rect1.setSize(Vector2f(100, 100)); getShape.rect1.setFillColor(Color(0, 255, 50, 30)); RenderWindow window(sf::VideoMode(800, 600), "SFML Game"); window.setFramerateLimit(60); window.setKeyRepeatEnabled(false); bool play = true; Event event; std::vector<shape> Vec; // Put your vector here! // play is already a bool, so you don't need == true while (play) { while (window.pollEvent(event)) { if (event.type == Event::Closed) { play = false; } } window.clear(); if (Keyboard::isKeyPressed(Keyboard::Space)) { Vec.push_back(getShape); } for (int i = 0; i < Vec.size(); ++i) { window.draw(Vec[i].rect1); } window.display(); } window.close(); return 0; } 序列化为Foo。这似乎是一项非常简单的任务,但出于某种原因String似乎打破了它。

Test.java

DateTime

Foo.java

import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.joda.time.DateTime;

public class Test {

    public static void main(String args[]) {
        try {
            List<Foo> foos = new ArrayList<>();
            Foo foo = new Foo();
            foo.setID(1);
            foo.setCURRENT(new DateTime(new Timestamp(System.currentTimeMillis())));
            foos.add(foo);
            String content = serialize(foos, Foo.class, Boolean.TRUE);
            System.out.println(content);
        } catch (IOException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static final synchronized String serialize(final Object object, final Class type, final Boolean withHeaders) throws IOException {
        CsvMapper csvMapper = new CsvMapper();
        CsvSchema csvSchema;
        if (withHeaders) {
            csvSchema = csvMapper.schemaFor(type).withHeader();
        } else {
            csvSchema = csvMapper.schemaFor(type).withoutHeader();
        }
        return csvMapper.writer(csvSchema).writeValueAsString(object);
    }

}

我尝试在我的import org.joda.time.DateTime; public class Foo { private Integer ID; private DateTime CURRENT; public Foo() { } public Integer getID() { return ID; } public void setID(Integer ID) { this.ID = ID; } public DateTime getCURRENT() { return CURRENT; } public void setCURRENT(DateTime CURRENT) { this.CURRENT = CURRENT; } } 对象中使用@JsonGetter@JsonSetter但它确实没有产生任何影响。

导致此错误的原因是什么?

1 个答案:

答案 0 :(得分:1)

您是否包含Joda DateTime的数据类型模块?您需要的是jackson-datatype-joda,来自https://github.com/FasterXML/jackson-datatype-joda。如果没有,这将解释这个问题,因为值会被视为常规POJO,而CSV不适用于嵌套数据而没有某种映射到点分表示法。