Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.github.oceanc.mybatis3.generator.plugin;
 
 
Created by chengyang
public class JacksonToJsonPlugin extends PluginAdapter {
    @Override
    public boolean validate(List<Stringwarnings) {
        return true;
    }
    @Override
    public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClassIntrospectedTable introspectedTable) {
        Method method = new Method();
        method.setName("toJson");
        method.setVisibility(.);
        method.setReturnType(FullyQualifiedJavaType.getStringInstance());
        method.addException(new FullyQualifiedJavaType("java.io.IOException"));
//        method.addBodyLine("StringWriter writer = new StringWriter();");
//        method.addBodyLine("JsonGenerator generator = new JsonFactory().createGenerator(writer);");
        method.addBodyLine("ObjectMapper mapper = new ObjectMapper();");
        method.addBodyLine("mapper.setTimeZone(TimeZone.getDefault());");
        method.addBodyLine("return mapper.writeValueAsString(this);");
//        method.addBodyLine("generator.setCodec(mapper);");
//        method.addBodyLine("generator.writeObject(this);");
//        method.addBodyLine("generator.close();");
//        method.addBodyLine("writer.close();");
//        method.addBodyLine("return writer.toString();");
        this.addDoc(methodintrospectedTable.getFullyQualifiedTableNameAtRuntime());
        topLevelClass.addImportedType(new FullyQualifiedJavaType("java.io.IOException"));
//        topLevelClass.addImportedType(new FullyQualifiedJavaType("java.io.StringWriter"));
//        topLevelClass.addImportedType(new FullyQualifiedJavaType("com.fasterxml.jackson.core.JsonFactory"));
//        topLevelClass.addImportedType(new FullyQualifiedJavaType("com.fasterxml.jackson.core.JsonGenerator"));
        topLevelClass.addImportedType(new FullyQualifiedJavaType("com.fasterxml.jackson.databind.ObjectMapper"));
        topLevelClass.addImportedType(new FullyQualifiedJavaType("java.util.TimeZone"));
        topLevelClass.addMethod(method);
        .debug("{} add method=toJson implement by Jackson2."topLevelClass.getType().getShortName());
        return true;
    }
    private void addDoc(JavaElement elementString tableName) {
        String suppressAllComments = this.getContext().getCommentGeneratorConfiguration().getProperty("suppressAllComments");
        if (!"true".equals(suppressAllComments)) {
            String type = element.getClass() == Field.class ? "field" : "method";
            element.addJavaDocLine("/**");
            element.addJavaDocLine("* This " + type + " was generated by MyBatis Generator.");
            element.addJavaDocLine("* This " + type + " corresponds to the database table " + tableName);
            element.addJavaDocLine("*");
            element.addJavaDocLine("* @mbggenerated " + .format(new Date()));
            element.addJavaDocLine("*/");
        }
    }
    private Logger log = LoggerFactory.getLogger(JacksonToJsonPlugin.class);
    private final SimpleDateFormat df = new SimpleDateFormat("EEE MMM ww HH:mm:ss z yyyy".);
New to GrepCode? Check out our FAQ X