"json",
StringBuilder.class,
Stmt.newObject(StringBuilder.class)
)
);
final ContextualStatementBuilder csb = Stmt.loadVariable("json");
marshallEnum(csb, Stmt.loadVariable("a0"), toMap);
builder.append(csb.invoke("toString").returnValue());
return;
}
builder.append(Stmt.declareFinalVariable("ref", boolean.class,
Stmt.loadVariable("a1").invoke("hasObject", Refs.get("a0"))));
builder.append(
Stmt.declareFinalVariable(
"json",
StringBuilder.class,
Stmt.newObject(StringBuilder.class,
"{" + keyValue(SerializationParts.ENCODED_TYPE, string(toType.getFullyQualifiedName())) + ",\"" +
SerializationParts.OBJECT_ID + "\"")
)
);
builder.append(Stmt.loadVariable("json")
.invoke("append", ":\"")
.invoke("append", loadVariable("a1").invoke("getObject", Stmt.loadVariable("a0")))
.invoke("append", "\"")
);
builder.append(
If.cond(loadVariable("ref"))
.append(Stmt.loadVariable("json").invoke("append", "}").invoke("toString").returnValue())
.finish());
boolean hasEncoded = false;
ContextualStatementBuilder appendChain = null;
int i = 0;
for (final MemberMapping mapping : definition.getMemberMappings()) {
if (!mapping.canRead()) {
continue;
}
BlockBuilder<?> lazyInitMethod = (needsLazyInit(mapping.getType())) ? initMethod : null;
MarshallingGenUtil.ensureMarshallerFieldCreated(classStructureBuilder, toMap, mapping.getType()
.asBoxed(), lazyInitMethod);
if (!hasEncoded) {
appendChain = Stmt.loadVariable("json").invoke("append", ",");
hasEncoded = true;
}
else if (i > 0) {
appendChain.invoke("append", ",");
}
final MetaClass targetType = GenUtil.getPrimitiveWrapper(mapping.getType());
final MetaClass compType =
targetType.isArray() ? targetType.getOuterComponentType().asBoxed() : targetType.asBoxed();
if (!(compType.isAbstract() || compType.isInterface() || compType.isEnum())
&& !context.canMarshal(compType.getFullyQualifiedName())) {
throw new NoAvailableMarshallerException(compType.getFullyQualifiedName());
}
Statement valueStatement = valueAccessorFor(mapping.getReadingMember(), classStructureBuilder);
if (targetType.isArray()) {
valueStatement = context.getArrayMarshallerCallback().marshal(targetType, valueStatement);
}
appendChain.invoke("append", "\"" + mapping.getKey() + "\":");
if (targetType.isEnum()) {
marshallEnum(appendChain, valueStatement, targetType);
}
else {
appendChain.invoke("append",
loadVariable(MarshallingGenUtil.getVarName(targetType))
.invoke("marshall", valueStatement, loadVariable("a1")));
}
i++;
}
if (i == 0) {
if (appendChain == null) {
appendChain = Stmt.loadVariable("json");
}
appendChain.invoke("append", ",\"" + SerializationParts.INSTANTIATE_ONLY + "\":true");
}
builder.append(appendChain.invoke("append", "}").invoke("toString").returnValue());
}