2014-10-08 181 views
0

我需要在neo4j中使用Java將序列化對象作爲節點屬性(字符串類型),我能夠應用一些轉換器/序列化程序嗎?neo4j節點中的序列化屬性

我知道,.NET新客戶如果使用SDN有類似JsonConverter

+1

你能告訴我們關於你的環境的更多信息嗎?例如你使用Spring嗎?你只是想找一個像Jackson這樣的圖書館(https://github.com/FasterXML/jackson) – JohnMark13 2014-10-08 11:34:39

+0

我正在使用與Neo4J的彈簧集成 – Fagoter 2014-10-08 15:25:51

回答

2

(和代理我想Spring框架的其他部分),那麼這對你來說很容易。首先,你應該爲你的財產的轉換器,將數據轉換爲字符串(見Spring documentation第7.5節):

import org.springframework.core.convert.converter.Converter 

public class YourPropertyTypeToStringConverter 
    implements Converter<YourPropertyType, String> { 

    @Override 
    public String convert(final YourPropertyType source) { 
     //some code that takes your object and returns a String 
    } 
} 

和另一回再次轉換:

public class StringToYourPropertyTypeConverter 
    implements Converter<String, YourPropertyType> { 

    @Override 
    public YourPropertyType convert(final String source) { 
     //some code that takes a String and returns an object of your type 
    } 
} 

然後你應該通過增加一些配置使Spring框架轉換:

<bean id="conversionService" 
     class="org.springframework.context.support.ConversionServiceFactoryBean"> 
    <property name="converters"> 
     <set> 
      <bean class="your.package.YourPropertyTypeToStringConverter"/> 
      <bean class="your.package.StringToYourPropertyTypeConverter"/> 
     </set> 
    </property> 
</bean> 

將在@NodeEntity註解類,你可以添加一個@GraphProperty annota重刑的領域,需要在表格轉換:

@GraphProperty(propertyType = String.class) 

現在你的轉換器可以爲所欲爲,只要你可以在兩個方向轉換,但你對JSON使用傑克遜的特別要求,所以一個簡單的例子。

包括傑克遜在您的項目:

<dependency> 
    <groupId>com.fasterxml.jackson.core</groupId> 
    <artifactId>jackson-databind</artifactId> 
    <version>2.3.3</version> 
</dependency> 

從類轉換爲字符串:

ObjectMapper mapper = new ObjectMapper(); 
String json = mapper.writeValueAsString(yourObject); 

,然後再返回:

YourPropertyType deserialised = mapper.readValue(json, YourPropertyType.class); 

顯然很多,你可以配置,有一些關於GitHub的文檔。

相關問題