2014-06-06 5 views
0
내가 모든 빈 문자열 ("") null로 변환하는 변환기를 만들

모든 빈 문자열로 변환합니다. 나는 변환기를 만들었지 만 나는 그것을 등록 할 때 NullPointerException이 발생합니다.XStream을 - 직렬화

버전 1.4.7

누군가 나를 도울 수 있습니까?

변환기 :

public class EmptyConverter extends StringConverter { 

    @Override 
    public String toString(Object obj) { 
     String str = (String) obj; 
     if(str != null && str.isEmpty()) { 
      return null; 
     } 
     return super.toString(obj); 
    } 

} 

스택 트레이스 :

java.lang.NullPointerException 
    at com.thoughtworks.xstream.io. 
xml.PrettyPrintWriter.writeText(PrettyPrintWriter.java:235) 
    at com.thoughtworks.xstream.io.xml.PrettyPrintWriter.writeText(PrettyPrintWriter.java:231) 
    at com.thoughtworks.xstream.io.xml.PrettyPrintWriter.setValue(PrettyPrintWriter.java:214) 
    at com.thoughtworks.xstream.converters.SingleValueConverterWrapper.marshal(SingleValueConverterWrapper.java:45) 
    at com.thoughtworks.xstream.core.TreeMarshaller.convert(TreeMarshaller.java:70) 
    at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:58) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshallField(AbstractReflectionConverter.java:250) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.writeField(AbstractReflectionConverter.java:226) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.<init>(AbstractReflectionConverter.java:189) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doMarshal(AbstractReflectionConverter.java:135) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshal(AbstractReflectionConverter.java:83) 
    at com.thoughtworks.xstream.core.TreeMarshaller.convert(TreeMarshaller.java:70) 
    at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:58) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshallField(AbstractReflectionConverter.java:250) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.writeField(AbstractReflectionConverter.java:226) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.<init>(AbstractReflectionConverter.java:189) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doMarshal(AbstractReflectionConverter.java:135) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshal(AbstractReflectionConverter.java:83) 
    at com.thoughtworks.xstream.core.TreeMarshaller.convert(TreeMarshaller.java:70) 
    at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:58) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshallField(AbstractReflectionConverter.java:250) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.writeField(AbstractReflectionConverter.java:226) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.<init>(AbstractReflectionConverter.java:189) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doMarshal(AbstractReflectionConverter.java:135) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshal(AbstractReflectionConverter.java:83) 
    at com.thoughtworks.xstream.core.TreeMarshaller.convert(TreeMarshaller.java:70) 
    at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:58) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshallField(AbstractReflectionConverter.java:250) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.writeField(AbstractReflectionConverter.java:226) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.<init>(AbstractReflectionConverter.java:189) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doMarshal(AbstractReflectionConverter.java:135) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshal(AbstractReflectionConverter.java:83) 
    at com.thoughtworks.xstream.core.TreeMarshaller.convert(TreeMarshaller.java:70) 
    at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:58) 
    at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:43) 
    at com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter.writeItem(AbstractCollectionConverter.java:64) 
    at com.thoughtworks.xstream.converters.collections.CollectionConverter.marshal(CollectionConverter.java:74) 
    at com.thoughtworks.xstream.core.TreeMarshaller.convert(TreeMarshaller.java:70) 
    at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:58) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshallField(AbstractReflectionConverter.java:250) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.writeField(AbstractReflectionConverter.java:226) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.<init>(AbstractReflectionConverter.java:189) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doMarshal(AbstractReflectionConverter.java:135) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshal(AbstractReflectionConverter.java:83) 
    at com.thoughtworks.xstream.core.TreeMarshaller.convert(TreeMarshaller.java:70) 
    at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:58) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshallField(AbstractReflectionConverter.java:250) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.writeField(AbstractReflectionConverter.java:226) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.<init>(AbstractReflectionConverter.java:189) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doMarshal(AbstractReflectionConverter.java:135) 
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshal(AbstractReflectionConverter.java:83) 
    at com.thoughtworks.xstream.core.TreeMarshaller.convert(TreeMarshaller.java:70) 
    at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:58) 
    at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:43) 
    at com.thoughtworks.xstream.core.TreeMarshaller.start(TreeMarshaller.java:82) 
    at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.marshal(AbstractTreeMarshallingStrategy.java:37) 
    at com.thoughtworks.xstream.XStream.marshal(XStream.java:1022) 
    at com.thoughtworks.xstream.XStream.marshal(XStream.java:1011) 
    at com.thoughtworks.xstream.XStream.toXML(XStream.java:984) 
    at com.thoughtworks.xstream.XStream.toXML(XStream.java:971) 
    at br.com.flexait.nfse.builder.Nfse.asXML(Nfse.java:43) 
    at br.com.flexait.nfse.builder.NfseTest.shouldSetNullIfStringEmpty(NfseTest.java:187) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309) 
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 

답변

1

당신이 그것에 대해 writeReplace을 사용할 수 있도록 XStream을이, 자바의 자신의 serilization 메커니즘을 존중합니다. 이 방법은 객체 직렬화 얻을 전에 를 호출 얻을. 객체는 이전에 객체를 대체 할 수 있습니다. 는 XML이 매우 복잡하기 때문에

<org.example.Foo> 
     <bar>hello</bar> 
    </org.example.Foo> 

대신

<org.example.Foo> 
     <bar>hello</bar> 
     <baz></baz> 
    </org.example.Foo> 
+1

이 솔루션 밖으로

public class NullString { public static void main(String[] args) { final Foo foo = new Foo(); foo.bar = "hello"; foo.baz = ""; final XStream xstream = new XStream(); xstream.toXML(foo, System.out); } } class Foo { String bar; String baz; private Object writeReplace() throws ObjectStreamException { final Foo replacement = new Foo(); //@formatter:off replacement.bar = (this.bar == null || this.bar.isEmpty()) ? null : this.bar; replacement.baz = (this.baz == null || this.baz.isEmpty()) ? null : this.baz; //@formatter:on return replacement; } } 

인쇄는, 나를 위해 너무 복잡하다. 같은 특성을 가진 많은 문자열입니다. – dtelaroli