Comments (3)
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.
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.
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)
- Add Jakarta annotation supports HOT 2
- Bug with Quarkus 3
- Adding a Jakarta validation annotation. HOT 6
- Enum-generation: differences between Windows and Linux HOT 7
- [Feature request] Allow javaInterfaces to be passed as plugin configuration HOT 4
- Not able to use in gradle with correct dependencies. tried the sample gradle file but got error HOT 2
- CVE => take Jackson 2.15.*
- JSONB1 style output missing @JsonbCreator for enum HOT 17
- Support for oneOf in schema HOT 1
- Hi, I'm trying to jsonschema2pojo maven plugin to generate classes from my json schema but classes are not generated eventhough package is created HOT 3
- how to implement a typed interface using jcodemodel ? HOT 3
- javaType as Long does not work with latest version but works with 0.4.x HOT 1
- Handling class name clashes when generating POJOs from examples.
- $ref generates classes in wrong package HOT 3
- jsonschema2pojo not generating classes HOT 1
- json schema $ref request address contains separator (':') , NTFS ADS separator (':') in file name is forbidden. HOT 1
- Generated output stays empty HOT 1
- Map generation from standard JsonSchema HOT 2
- Duplicate constructors generated when all properties are required; and both includeAllPropertiesConstructor and includeRequiredPropertiesConstructor are true
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsonschema2pojo.