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

ru.cwcode.tkach.config.jackson.module.TranslatableMessageDeserializer Maven / Gradle / Ivy

The newest version!
package ru.cwcode.tkach.config.jackson.module;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import ru.cwcode.tkach.locale.translatable.TranslatableMessage;

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

public class TranslatableMessageDeserializer extends JsonDeserializer {
  ObjectMapper mapper = new ObjectMapper();
  
  @Override
  public TranslatableMessage deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
    JsonNode node = p.getCodec().readTree(p);
    String defaultText = node.get("default").asText();
    HashMap translates = new HashMap<>();
    
    JsonNode translatesNode = node.get("translates");
    
    Iterator fieldNames = translatesNode.fieldNames();
    
    while (fieldNames.hasNext()) {
      String fieldName = fieldNames.next();
      JsonNode fieldValue = translatesNode.get(fieldName);
      translates.put(fieldName, fieldValue.asText());
    }
    
    TranslatableMessage translatableMessage = new TranslatableMessage(defaultText);
    translatableMessage.setTranslates(translates);
    
    return translatableMessage;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy