2014-10-08 95 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的文档。