use of com.abubusoft.kripton.processor.bind.model.BindProperty in project kripton by xcesco.
the class MapBindTransformation method generateParseOnJacksonInternal.
public void generateParseOnJacksonInternal(BindTypeContext context, Builder methodBuilder, String parserName, TypeName beanClass, String beanName, BindProperty property, boolean onString) {
// define key and value type
ParameterizedTypeName mapTypeName = (ParameterizedTypeName) property.getPropertyType().getTypeName();
TypeName keyTypeName = mapTypeName.typeArguments.get(0);
TypeName valueTypeName = mapTypeName.typeArguments.get(1);
// @formatter:off
methodBuilder.beginControlFlow("if ($L.currentToken()==$T.START_ARRAY)", parserName, JsonToken.class);
methodBuilder.addStatement("$T<$T, $T> collection=new $T<>()", defineMapClass(mapTypeName), keyTypeName, valueTypeName, defineMapClass(mapTypeName));
BindTransform transformKey = BindTransformer.lookup(keyTypeName);
BindProperty elementKeyProperty = BindProperty.builder(keyTypeName, property).inCollection(false).xmlType(property.xmlInfo.mapEntryType.toXmlType()).elementName(property.mapKeyName).nullable(false).build();
BindTransform transformValue = BindTransformer.lookup(valueTypeName);
BindProperty elementValueProperty = BindProperty.builder(valueTypeName, property).inCollection(false).xmlType(property.xmlInfo.mapEntryType.toXmlType()).elementName(property.mapValueName).nullable(true).build();
methodBuilder.addStatement("$T key=$L", elementKeyProperty.getPropertyType().getTypeName(), DEFAULT_VALUE);
methodBuilder.addStatement("$T value=$L", elementValueProperty.getPropertyType().getTypeName(), DEFAULT_VALUE);
if (onString) {
methodBuilder.addStatement("$T current", JsonToken.class);
methodBuilder.addStatement("String tempValue=null");
}
methodBuilder.beginControlFlow("while ($L.nextToken() != $T.END_ARRAY)", parserName, JsonToken.class);
if (onString) {
// on string
methodBuilder.addStatement("current=$L.currentToken()", parserName);
methodBuilder.beginControlFlow("for (int i=0; i<2 ;i++)");
methodBuilder.beginControlFlow("while (current != $T.FIELD_NAME)", JsonToken.class);
methodBuilder.addStatement("current=$L.nextToken()", parserName);
methodBuilder.endControlFlow();
methodBuilder.addStatement("$L.nextValue()", parserName);
//
methodBuilder.addCode("switch(jacksonParser.getCurrentName()) {\n");
methodBuilder.addCode("case $S:$>\n", property.mapKeyName);
transformKey.generateParseOnJacksonAsString(context, methodBuilder, parserName, null, "key", elementKeyProperty);
methodBuilder.addStatement("$<break");
methodBuilder.addCode("case $S:$>\n", property.mapValueName);
methodBuilder.addStatement("tempValue=$L.getValueAsString()", parserName);
methodBuilder.beginControlFlow("if ($L.currentToken()==$T.VALUE_STRING && \"null\".equals(tempValue))", parserName, JsonToken.class);
methodBuilder.addStatement("value=$L", DEFAULT_VALUE);
methodBuilder.nextControlFlow("else");
transformValue.generateParseOnJacksonAsString(context, methodBuilder, parserName, null, "value", elementValueProperty);
methodBuilder.endControlFlow();
methodBuilder.addStatement("$<break");
methodBuilder.addCode("}\n");
//
methodBuilder.endControlFlow();
} else {
// key
methodBuilder.addStatement("$L.nextValue()", parserName);
transformKey.generateParseOnJackson(context, methodBuilder, parserName, null, "key", elementKeyProperty);
// value
methodBuilder.addStatement("$L.nextValue()", parserName);
transformValue.generateParseOnJackson(context, methodBuilder, parserName, null, "value", elementValueProperty);
}
methodBuilder.addStatement("collection.put(key, value)");
methodBuilder.addStatement("key=$L", DEFAULT_VALUE);
methodBuilder.addStatement("value=$L", DEFAULT_VALUE);
methodBuilder.addStatement("$L.nextToken()", parserName);
methodBuilder.endControlFlow();
methodBuilder.addStatement(setter(beanClass, beanName, property, "collection"));
methodBuilder.endControlFlow();
// @formatter:on
}
use of com.abubusoft.kripton.processor.bind.model.BindProperty in project kripton by xcesco.
the class MapBindTransformation method generateSerializeOnXml.
@Override
public void generateSerializeOnXml(BindTypeContext context, MethodSpec.Builder methodBuilder, String serializerName, TypeName beanClass, String beanName, BindProperty property) {
// define key and value type
ParameterizedTypeName mapTypeName = (ParameterizedTypeName) property.getPropertyType().getTypeName();
TypeName keyTypeName = mapTypeName.typeArguments.get(0);
TypeName valueTypeName = mapTypeName.typeArguments.get(1);
// @formatter:off
methodBuilder.beginControlFlow("if ($L!=null) ", getter(beanName, beanClass, property));
if (property.xmlInfo.isWrappedCollection()) {
methodBuilder.addCode("// write wrapper tag\n");
methodBuilder.addStatement("$L.writeStartElement($S)", serializerName, property.label);
}
BindTransform transformKey = BindTransformer.lookup(keyTypeName);
// key can not be null
// not in collection, it's included in an element
BindProperty elementKeyProperty = BindProperty.builder(keyTypeName, property).inCollection(false).nullable(false).xmlType(property.xmlInfo.mapEntryType.toXmlType()).elementName(property.mapKeyName).build();
BindTransform transformValue = BindTransformer.lookup(valueTypeName);
// not in collection, it's included in an element
BindProperty elementValueProperty = BindProperty.builder(valueTypeName, property).inCollection(false).xmlType(property.xmlInfo.mapEntryType.toXmlType()).elementName(property.mapValueName).build();
methodBuilder.beginControlFlow("for ($T<$T, $T> item: $L.entrySet())", Entry.class, keyTypeName, valueTypeName, getter(beanName, beanClass, property));
methodBuilder.addStatement("$L.writeStartElement($S)$>", serializerName, property.xmlInfo.labelItem);
transformKey.generateSerializeOnXml(context, methodBuilder, serializerName, null, "item.getKey()", elementKeyProperty);
if (elementValueProperty.isNullable()) {
methodBuilder.beginControlFlow("if (item.getValue()==null)");
methodBuilder.addStatement("$L.writeEmptyElement($S)", serializerName, property.mapValueName);
methodBuilder.nextControlFlow("else");
transformValue.generateSerializeOnXml(context, methodBuilder, serializerName, null, "item.getValue()", elementValueProperty);
methodBuilder.endControlFlow();
} else {
transformValue.generateSerializeOnXml(context, methodBuilder, serializerName, null, "item.getValue()", elementValueProperty);
}
methodBuilder.addStatement("$<$L.writeEndElement()", serializerName);
methodBuilder.endControlFlow();
if (property.xmlInfo.isWrappedCollection()) {
methodBuilder.addStatement("$L.writeEndElement()", serializerName);
}
methodBuilder.endControlFlow();
// @formatter:on
}
Aggregations