Giter Club home page Giter Club logo

Comments (5)

mbknor avatar mbknor commented on August 12, 2024

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.

awanczowski avatar awanczowski commented on August 12, 2024

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.

mbknor avatar mbknor commented on August 12, 2024

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.

awanczowski avatar awanczowski commented on August 12, 2024

I think that would work. Thanks for looking into this.

from mbknor-jackson-jsonschema.

mbknor avatar mbknor commented on August 12, 2024

Version 1.0.16 has been released

from mbknor-jackson-jsonschema.

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.