Coder Social home page Coder Social logo

Comments (3)

joerg-d-schneider-db avatar joerg-d-schneider-db commented on June 5, 2024
package com.db.dice.rules;

import org.jsonschema2pojo.Annotator;
import org.jsonschema2pojo.GenerationConfig;
import org.jsonschema2pojo.SchemaStore;
import org.jsonschema2pojo.rules.Rule;
import org.jsonschema2pojo.rules.RuleFactory;

import com.sun.codemodel.JClassContainer;
import com.sun.codemodel.JType;

/**
 * @author sf9100
 * @since Jun 2023
 */
public class EnumMapperRuleFactory extends RuleFactory {
	public EnumMapperRuleFactory(GenerationConfig generationConfig, Annotator annotator, SchemaStore schemaStore) {
		super(generationConfig, annotator, schemaStore);
	}

	public EnumMapperRuleFactory() {
		super();
	}

	@Override
	public Rule<JClassContainer, JType> getEnumRule() {
		return new EnumMapperRule(this);
	}
}

from jsonschema2pojo.

joerg-d-schneider-db avatar joerg-d-schneider-db commented on June 5, 2024
package com.db.dice.rules;

import javax.json.bind.adapter.JsonbAdapter;

import org.jsonschema2pojo.AnnotationStyle;
import org.jsonschema2pojo.model.EnumDefinition;
import org.jsonschema2pojo.rules.EnumRule;
import org.jsonschema2pojo.rules.RuleFactory;

import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JMod;

public class EnumMapperRule extends EnumRule {

	private final RuleFactory ruleFactory;
	public static final String SUFFIX = "Mapper";

	protected EnumMapperRule(RuleFactory ruleFactory) {
		super(ruleFactory);
		this.ruleFactory = ruleFactory;
	}

	@Override
	protected void applyCustomizations(EnumDefinition enumDefinition, JDefinedClass _enum) {
		if (this.ruleFactory.getGenerationConfig().getAnnotationStyle().equals(AnnotationStyle.JSONB1)
				|| this.ruleFactory.getGenerationConfig().getAnnotationStyle().equals(AnnotationStyle.JSONB1)) {
			
			try {
				// create an public static inner class inside the enum
				JDefinedClass c = _enum._class(JMod.PUBLIC | JMod.STATIC,
						enumDefinition.getNodeName() + EnumMapperRule.SUFFIX);
				

				if (c != null) {
					// implement the JsonbAdapter interface
				    c._implements(_enum.owner().ref(JsonbAdapter.class).narrow(_enum).narrow(String.class));
				    
					// create the adaptToJson method
					JMethod to = c.method(JMod.PUBLIC, String.class, "adaptToJson");
					to.annotate(Override.class);
					to.param(_enum, "obj");
					to.body().directStatement("return obj.value();");

					// create the adaptFromJson method
					JMethod from = c.method(JMod.PUBLIC, _enum, "adaptFromJson");
					from.annotate(Override.class);
					from.param(String.class, "obj");
					from.body().directStatement("return " + enumDefinition.getNodeName() + ".fromValue(obj);");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

}

from jsonschema2pojo.

joerg-d-schneider-db avatar joerg-d-schneider-db commented on June 5, 2024
package com.db.dice.util;

import javax.json.bind.annotation.JsonbTypeAdapter;

import org.jsonschema2pojo.AbstractAnnotator;

import com.db.dice.rules.EnumMapperRule;
import com.fasterxml.jackson.databind.JsonNode;
import com.sun.codemodel.JClass;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JFieldVar;
import com.sun.codemodel.JType;

public class EnumJsonbAdapterAnnotator extends AbstractAnnotator {
	@Override
	public void propertyField(JFieldVar field, JDefinedClass clazz, String propertyName, JsonNode propertyNode) {
		super.propertyField(field, clazz, propertyName, propertyNode);
		if (propertyNode.has("enum")) {

			try {
				JClass temp1 = clazz.owner()
						.directClass(field.type().fullName() + "." + field.type().name() + EnumMapperRule.SUFFIX);
				
				field.annotate(JsonbTypeAdapter.class).param("value", (JType) temp1);

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}

from jsonschema2pojo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.