提问人:Factor Three 提问时间:11/16/2023 更新时间:11/20/2023 访问量:30
生成的 YAML 文件未按所需格式化
Generated YAML file not being formatted as desired
问:
我正在尝试使用 Jackson YAML 库生成 YAML 文件。我正在寻求以特定格式创建该文件,但未能这样做。
我保存的数据是数据测试对象的列表。对象本身定义如下:
@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonTypeName("User")
@Builder
public class DataTest
{
@JsonProperty("ID")
private int userID;
@JsonProperty("Name")
private String userName;
@JsonProperty("Address")
private String streetAddress;
@JsonProperty("City")
private String theCity;
}
请注意,除了 Jackson 注解外,此类还使用 Lombok 来提供代码简洁性。
用于包含 DataTest 对象列表的对象如下所示:
@JsonRootName(value = "ListOfUsers")
public class DataList extends ArrayList<DataTest>
{
private static final long serialVersionUID = 7112953850878936075L;
public DataList()
{
}
// Other constructors
}
有一些示例数据将被放入 DataTest 对象中:
public class SomeData
{
public static String[] theNames = {
"Jimmy Dean",
"John Sheridan",
"John Chriton",
"James Kirk",
"River Tan"
};
public static String[] theAddresses = {
"12 Riverview Drive",
"233X Blue Sector",
"2nd Room",
"Captain's Quarters",
"3rd Berth"
};
public static String[] theCities = {
"Greenview",
"Babylon 5",
"Moya the Ship",
"USS Enterprise",
"Serenity"
};
}
下面的 ServiceIO 类是利用 Jackson API 创建 YAML 文件的单一实例:
public class ServiceIO
{
private static ServiceIO mySelf = null;
private final ObjectMapper theMapper;
private final File theFile;
private ServiceIO()
{
theFile = new File("./files/users.yml");
theMapper = new ObjectMapper(new YAMLFactory().disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER))
.enable(SerializationFeature.WRAP_ROOT_VALUE)
.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
}
public void saveServices(DataList theInfo)
{
try
{
theMapper.writeValue(theFile,theInfo);
}
catch(Exception exe)
{
exe.printStackTrace();
}
}
public static ServiceIO getInstance()
{
if(mySelf == null)
mySelf = new ServiceIO();
return(mySelf);
}
}
实际生成 YAML 文件的类很简单。它生成一组 DataTest 对象,并将它们放入 DataList 中。然后,它将 DataList 写入 YAML 文件。
public class YamlTest
{
public static void main(String[] args)
{
DataList list = new DataList();
for(int inc = 0; inc < 5; inc++)
{
DataTest test = DataTest.builder().userID(inc+1).userName(SomeData.theNames[inc])
.streetAddress(SomeData.theAddresses[inc]).theCity(SomeData.theCities[inc])
.build();
list.add(test);
}
ServiceIO service = ServiceIO.getInstance();
service.saveServices(list);
}
}
当我运行测试应用程序时,我想生成一个 YAML 文件,其内容类似于以下内容:
ListOfUsers:
- User:
ID: 1
Name: "Jimmy Dean"
Address: "12 Riverview Drive"
City: "Greenview"
- User:
ID: 2
Name: "John Sheridan"
Address: "233X Blue Sector"
City: "Babylon 5"
- User:
ID: 3
Name: "John Chriton"
Address: "2nd Room"
City: "Moya the Ship"
- User:
ID: 4
Name: "James Kirk"
Address: "Captain's Quarters"
City: "USS Enterprise"
- User:
ID: 5
Name: "River Tan"
Address: "3rd Berth"
City: "Serenity"
它不必是DataTest的精确,但最终我想用“User”linne分隔每个DataTest。
相反,我得到以下输出:
ListOfUsers:
- ID: 1
Name: "Jimmy Dean"
Address: "12 Riverview Drive"
City: "Greenview"
- ID: 2
Name: "John Sheridan"
Address: "233X Blue Sector"
City: "Babylon 5"
- ID: 3
Name: "John Chriton"
Address: "2nd Room"
City: "Moya the Ship"
- ID: 4
Name: "James Kirk"
Address: "Captain's Quarters"
City: "USS Enterprise"
- ID: 5
Name: "River Tan"
Address: "3rd Berth"
City: "Serenity"
有人可以告诉我我做错了什么(如果有的话)吗?我怎样才能得到与我所寻求的输出相似的输出?
答:
0赞
Paul Marcelin Bejan
11/19/2023
#1
除了你做了什么, 您需要禁用:
YAMLGenerator.Feature.USE_NATIVE_TYPE_ID
在 DataTest 上,您需要添加以下注解:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT)
通过这些更改,yaml 将如下所示:
ListOfUsers:
- User:
ID: 1
Name: "Jimmy Dean"
Address: "12 Riverview Drive"
City: "Greenview"
- User:
ID: 2
Name: "John Sheridan"
Address: "233X Blue Sector"
City: "Babylon 5"
- User:
ID: 3
Name: "John Chriton"
Address: "2nd Room"
City: "Moya the Ship"
- User:
ID: 4
Name: "James Kirk"
Address: "Captain's Quarters"
City: "USS Enterprise"
- User:
ID: 5
Name: "River Tan"
Address: "3rd Berth"
City: "Serenity"
对于数组的缩进,您可以启用以下配置之一:
YAMLGenerator.Feature.INDENT_ARRAYS
YAMLGenerator.Feature.INDENT_ARRAYS_WITH_INDICATOR
评论