i want build a DataTable for google charts with jackson that i can easily create a json file..
我想用jackson为谷歌图表构建一个可以轻松创建json文件的数据表。
i use this jackson version:
我使用杰克逊版本:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.3</version>
</dependency>
I have the following classes (The example creates only rows at the moment):
我有以下的类(这个例子现在只创建行):
public class DataTable implements Serializable {
private static final long serialVersionUID = 1L;
@JsonProperty("rows")
private List<DataTableRow> dataTableRows;
public List<DataTableRow> getDataTableRows() {
return dataTableRows;
}
public void setDataTableRows(List<DataTableRow> dataTableRows) {
this.dataTableRows = dataTableRows;
}
}
then i have a DataTableRow Class
然后我有一个DataTableRow类
public class DataTableRow implements Serializable{
private static final long serialVersionUID = 1L;
@JsonProperty("c")
List<SingleTableCell> singleTableCell;
public List<SingleTableCell> getSingleTableCell() {
return singleTableCell;
}
public void setSingleTableCell(List<SingleTableCell> singleTableCell) {
this.singleTableCell = singleTableCell;
}
}
and a row has several SingleTableCells
一排有几个单排
public class SingleTableCell implements Serializable{
private static final long serialVersionUID = 1L;
@JsonProperty("v")
private Value value;
@JsonProperty("f")
private String formattedValue = null;
public Value getValue() {
return value;
}
public void setValue(Value value) {
this.value = value;
}
public String getFormattedValue() {
return formattedValue;
}
public void setFormattedValue(String formattedValue) {
this.formattedValue = formattedValue;
}
}
this generates me a Json String like this :
这会生成一个Json字符串,如下所示:
{
{
"rows": [
{
"c": [
{
"v": {
"value": "Hans"
},
"f": null
},
{
"v": {
"value": 3.4
},
"f": null
}
]
}
]
}
}
the Problem is my Value object this is an abstract class i want not that this class is rendering as a json object i want only a key value pair! so it should not render to this:
问题是我的值对象这是一个抽象类我不想这个类呈现为json对象我只想要一个键值对!因此,它不应该这样说:
"v": { "value": "Hans" }
"v": {"value": "Hans"}
i want only this: "v" : "Hans"
我只想要这个:"v": "汉斯"
and i want that i can define how the Value object is rendert the value object cann be for example a String,Double or Date Object... if it is a Date object the Date should transformed to a String like this: "v" : Date(2010,10,01)
我想定义Value对象是如何渲染Value对象可以是字符串,Double或Date对象。如果它是一个日期对象,那么日期应该转换为如下字符串:“v”:Date(2010、10、01)
and if it is an other Object like a String or Double i want decide how the json string looks like..
如果它是另一个对象,比如字符串或者Double,我想要决定json字符串的样子
my abstract class looks like this:
我的抽象类是这样的:
public abstract class Value {
}
public class StringValue extends Value{
String Value;
}
public class DoubleValue extends Value{
Double Value;
}
public class DateValue extends Value{
Date Value;
}
1 个解决方案
#1
1
I would make the following changes to your code in order to achieve what you want:
我将对您的代码做如下修改,以实现您的目标:
public abstract class Value {
}
public class StringValue extends Value{
@JsonValue
//do whatever else you need although I can't really see what that would be
String Value;
}
public class DoubleValue extends Value{
@JsonValue
//do whatever else you need although I can't really see what that would be
Double Value;
}
public class DateValue extends Value{
@JsonValue
@DateFormat() //insert the relevant date code here
Date Value;
}
From the code you have posted I cannot really see what the purpose of class Value
is, but I ran with here since I am guessing you are doing more with it than you have posted
从您所发布的代码中,我无法真正理解类值的目的是什么,但是我在这里使用了它,因为我猜您使用它所做的事情比您发布的要多
#1
1
I would make the following changes to your code in order to achieve what you want:
我将对您的代码做如下修改,以实现您的目标:
public abstract class Value {
}
public class StringValue extends Value{
@JsonValue
//do whatever else you need although I can't really see what that would be
String Value;
}
public class DoubleValue extends Value{
@JsonValue
//do whatever else you need although I can't really see what that would be
Double Value;
}
public class DateValue extends Value{
@JsonValue
@DateFormat() //insert the relevant date code here
Date Value;
}
From the code you have posted I cannot really see what the purpose of class Value
is, but I ran with here since I am guessing you are doing more with it than you have posted
从您所发布的代码中,我无法真正理解类值的目的是什么,但是我在这里使用了它,因为我猜您使用它所做的事情比您发布的要多