Comments (5)
I am not sure I understand your question.
Can you show me an example of the class your generating schema for, how the generated schema looks like, and what you want it to look like.
from mbknor-jackson-jsonschema.
Sure, The idea is to have a JSON structure that uses BCP47 language codes as the keys from the java.util.Locale object and text in the corresponding language.
Domain Model
@JsonSerialize(using = LanguageMapSerializer.class)
public class LanguageMap {
/**
* A map that contains text in the language that is supported by the corresponding locale.
*/
Map<Locale, String> localizedText;
public Map<Locale, String> getLocalizedText() {
return localizedText;
}
/**
* Set text in a specific language
* @param locale The locale that matches the text.
* @param text The text to be captured.
*/
public void setText(Locale locale, String text) {
if (locale != null) {
localizedText.put(locale, text);
}
}
/**
* Fetch the text for a given locale.
* @param locale A locale that is to be used for fetching the text.
* @return A text for the given locale.
*/
public String getText(Locale locale) {
return localizedText.get(locale);
}
/**
* Fetch the text for the default locale.
* @return A text for the default locale.
*/
public String getText() {
return localizedText.get(Locale.getDefault());
}
/**
* Set text in the default locale language
* @param text The text to be captured.
*/
public void setText(String text) {
setText(Locale.getDefault(), text);
}
}
Serializer:
public class LanguageMapSerializer extends JsonSerializer<LanguageMap> {
@Override
public void serialize(LanguageMap value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
Map<Locale, String> localizedText = value.getLocalizedText();
jgen.writeStartObject();
localizedText.entrySet().forEach(entry -> {
try {
jgen.writeStringField(entry.getKey().getLanguage(), entry.getValue());
} catch (IOException e) {
// do something.
}
});
jgen.writeEndObject();
}
@Override
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint) throws JsonMappingException {
TypeFactory typeFactory = TypeFactory.defaultInstance();
visitor.expectObjectFormat(typeFactory.constructType(LanguageMap.class));
}
}
Sample JSON:
{
"ja": "忍者",
"en": "Ninja",
"cs": "Nindža"
}
Sample Schema:
"LanguageMap": {
"type": "object",
"additionalProperties": false,
"patternProperties": {
"^[a-zA-Z0-9]+": {
"type": "string"
}
}
}
from mbknor-jackson-jsonschema.
I've been thinking about adding a new annotation called @JsonSchemaInject
which could be used to include "whatever" json into the generated schema.
With this annotation, you should be able to inject the following fragment in the schema all places it encounters your LanguageMap-class if it is annotated with @JsonSchemaInject:
{
"patternProperties": {
"^[a-zA-Z0-9]+": {
"type": "string"
}
}
}
Would this work for you?
I guess it would be better to have "nativ" support for 'patternProperties', but I see that I first should try to add something generic that could work as a workaround for all such cases.
What do you think?
from mbknor-jackson-jsonschema.
I think that would work. Thanks for looking into this.
from mbknor-jackson-jsonschema.
Version 1.0.16 has been released
from mbknor-jackson-jsonschema.
Related Issues (20)
- Any way to ignore @Deprecated fields when generating schema?
- @JsonProperty(defaultValue="...") does not allow float/double values HOT 1
- Support generating multiple files instead of nesting in definitions
- Rewrite in Java, drop Scala dependencies HOT 2
- anyOf not working with array and nullableJsonSchemaDraft4
- CVE dependancies security breaches (Kotlin, jackson databind)
- Support for generating a file per class
- Proper handling of Polymorphic types when JsonTypeInfo.Id is DEDUCTION
- Support for restrictions in local definitions instead of in-lined HOT 1
- JaxbAnnotationModule and PropertyNamingStrategy conflicts HOT 1
- Generation throws java.lang.NoClassDefFoundError: javax/validation/constraints/NotBlank HOT 1
- How to document type properties used for polymorphism?
- A way to use jakarta.validation instead of javax.validation
- Security Vulnerability: Latest 1.0.39 release depends on version of Scala with known vulernability HOT 1
- Security Vulnerability: Latest 1.0.39 release depends on version of Jackson with known vulnerabilites
- Security Vulnerability: Latest 1.0.39 release depends on version of Kotlin with known vulnerabilities
- Question: Custom Type Mapping with Java
- Calling `JsonSchemaConfig.builder()` scans the entire classpath
- support Scala 3
- Schema generation for JsonSchemaExamples on double valued field throws an exception HOT 1
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 mbknor-jackson-jsonschema.