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

io.github.emm035.openapi.immutables.v3.content.ContentDeserializer Maven / Gradle / Ivy

package io.github.emm035.openapi.immutables.v3.content;

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.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

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

class ContentDeserializer extends StdDeserializer {

  ContentDeserializer() {
    super(Content.class);
  }

  @Override
  public Content deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    Content.Builder builder = Content.builder();

    ObjectMapper mapper = (ObjectMapper) p.getCodec();
    JsonNode node = mapper.readTree(p);
    for (Iterator iter = node.fieldNames(); iter.hasNext();) {
      String fieldName = iter.next();
      if (fieldName.startsWith("x-")) {
        builder.putExtensions(fieldName, mapper.treeToValue(node.get(fieldName), Object.class));
      } else {
        builder.putMediaTypes(fieldName, mapper.treeToValue(node.get(fieldName), MediaType.class));
      }
    }

    return builder.build();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy