final MappingDefinition definition,
final ClassStructureBuilder classStructureBuilder,
final BlockBuilder<?> initMethod) {
if (!context.canMarshal(toType.getFullyQualifiedName())) {
throw new NoAvailableMarshallerException(toType.getName());
}
builder.append(
If.isNull(loadVariable("a0"))
.append(Stmt.load("null").returnValue()).finish()
);
final int bufSize = calcBufferSize(new ArrayList<MappingDefinition>(), definition);
if (toMap.isEnum()) {
builder.append(
Stmt.declareFinalVariable(
"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);