2017-04-24 127 views
0

我們試圖將YAML文件轉換爲對象並使用它們。當試圖用特殊字符分析字符串時,YAML Factory拆分字符串。yaml jackson使用YAML工廠解析

如果你注意到輸出,你會發現字符串正在被分割。 你能幫我們嗎?我們嘗試使用YAML功能,但仍然無法成功。

回答

0

實施例:

We are trying to debug an issue. 

Following is my Object class: 



import com.fasterxml.jackson.annotation.JsonCreator; 
import com.fasterxml.jackson.annotation.JsonProperty; 

public class HelloWorld 
{ 

public String name; 
public String place; 
public String company; 

@JsonCreator 
public HelloWorld(@JsonProperty("name") String name, @JsonProperty("place") String place, @JsonProperty("company") String company) 
{ 
super(); 
this.name = name; 
this.place = place; 
this.company = company; 
} 

public String getName() 
{ 
return name; 
} 

public String getPlace() 
{ 
return place; 
} 

public String getCompany() 
{ 
return company; 
} 
} 

Main Class :+1: 

import com.fasterxml.jackson.annotation.JsonInclude.Include; 
import com.fasterxml.jackson.databind.ObjectMapper; 
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; 

public class SampleHellp 
{ 

public static void main(String[] args) 
{ 

    String sample = "$(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) \"_\" + $(parent_RFSEVCService_evcID)"; 
    System.out.println(sample); 
    HelloWorld world = new HelloWorld("name", sample,"company"); 
    ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); 
    try 
    { 
    mapper.setSerializationInclusion(Include.NON_NULL); 
    // mapper.writeValue(new File("C:\\D\\Template.yaml"), template); 
    String writeValueAsString = mapper.writeValueAsString(world); 
    System.out.println(writeValueAsString); 
    // System.out.println(ReflectionToStringBuilder.toString(template, ToStringStyle.MULTI_LINE_STYLE)); 
    } 
    catch (Exception e) 
    { 
    throw new RuntimeException("Exception while converting Object to YAML ", e); 
    } 
} 
} 

Output : 

$(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) "_" + $(parent_RFSEVCService_evcID) 

name: "name" 
place: "$(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) "_" 
\ + $(parent_RFSEVCService_evcID)" 
company: "company" 
+0

你應該把你的答案壓縮成一個。用第二個信息編輯第一個,然後刪除第二個。 –

0

解決:

用於YAML發電機的以下特點:SplitLINES

YAMLFactory yf = new YAMLFactory(); 
    yf.disable(YAMLGenerator.Feature.SPLIT_LINES); 
    ObjectMapper mapper = new ObjectMapper(yf); 

這是在2.6特徵可用OD同時傑克遜-YAML的數據綁定