我正在尝试使用RestyGWT的JsonEncoderDecoder来编码/解码JSON对象。从他们的文档我能够做到:
public interface PersonCodec extends JsonEncoderDecoder<PersonCodec>> {}
并使用编码/解码功能。但是,当我这样做时:
public interface PersonListCodec extends JsonEncoderDecoder<List<PersonCodec>> {}
它给了我编译错误:
java.lang.NullPointerException
at org.fusesource.restygwt.rebind.BaseSourceCreator.<init>(BaseSourceCreator.java:76)
at org.fusesource.restygwt.rebind.JsonEncoderDecoderClassCreator.<init>(JsonEncoderDecoderClassCreator.java:79)
at org.fusesource.restygwt.rebind.ExtendedJsonEncoderDecoderClassCreator.createComposerFactory(ExtendedJsonEncoderDecoderClassCreator.java:46)
at org.fusesource.restygwt.rebind.BaseSourceCreator.create(BaseSourceCreator.java:210)
at org.fusesource.restygwt.rebind.JsonEncoderDecoderGenerator.generate(JsonEncoderDecoderGenerator.java:38)
at com.google.gwt.core.ext.IncrementalGenerator.generateNonIncrementally(IncrementalGenerator.java:40)
at com.google.gwt.dev.javac.StandardGeneratorContext.runGeneratorIncrementally(StandardGeneratorContext.java:657)
at com.google.gwt.dev.cfg.RuleGenerateWith.realize(RuleGenerateWith.java:41)
at com.google.gwt.dev.shell.StandardRebindOracle$Rebinder.rebind(StandardRebindOracle.java:79)
at com.google.gwt.dev.shell.StandardRebindOracle.rebind(StandardRebindOracle.java:276)
at com.google.gwt.dev.shell.StandardRebindOracle.rebind(StandardRebindOracle.java:265)
at com.google.gwt.dev.DistillerRebindPermutationOracle.getAllPossibleRebindAnswers(DistillerRebindPermutationOracle.java:91)
at com.google.gwt.dev.jjs.impl.UnifyAst$UnifyVisitor.handleGwtCreate(UnifyAst.java:355)
at com.google.gwt.dev.jjs.impl.UnifyAst$UnifyVisitor.handleMagicMethodCall(UnifyAst.java:433)
at com.google.gwt.dev.jjs.impl.UnifyAst$UnifyVisitor.endVisit(UnifyAst.java:237)
at com.google.gwt.dev.jjs.ast.JMethodCall.traverse(JMethodCall.java:243)
at com.google.gwt.dev.jjs.ast.JModVisitor.traverse(JModVisitor.java:361)
...
有关如何使这项工作的任何想法?或者其他建议将json编码/解码为java对象?
谢谢!
答案 0 :(得分:3)
我能够使用以下代码对我的列表进行编码:
JSONArray batch = new JSONArray();
int idx=0;
for (Pojo i : buffer) {
batch.set(idx++, CODEC.encode(i));
}
答案 1 :(得分:0)
我将List包装在一个Object中,因为我控制了输出。
@XmlRootElement
public class WrapperResult {
protected List<Person> result;
}
然后我可以毫无问题地对列表进行编码和解码。