All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.netgrif.application.engine.petrinet.domain.I18nStringDeserializer Maven / Gradle / Ivy

Go to download

System provides workflow management functions including user, role and data management.

There is a newer version: 6.3.3
Show newest version
package com.netgrif.application.engine.petrinet.domain;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class I18nStringDeserializer extends StdDeserializer {

    public I18nStringDeserializer() {
        this(null);
    }

    public I18nStringDeserializer(Class vc) {
        super(vc);
    }

    @Override
    public I18nString deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        JsonNode node = jp.getCodec().readTree(jp);
        if (node.isTextual()) {
            return new I18nString(node.asText());
        } else {
            String defaultValue = node.get("defaultValue").asText();
            I18nString text = new I18nString(defaultValue);
            if (!node.get("key").isNull()) {
                text.setKey(node.get("key").asText());
            }
            if (!node.get("translations").isNull()) {
                Map translations = new HashMap<>();
                node.get("translations").fields().forEachRemaining(entry ->
                        translations.put(entry.getKey(), entry.getValue().asText())
                );
                text.setTranslations(translations);
            }
            return text;
        }
    }
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy