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

com.github.tonivade.zeromock.api.Deserializers Maven / Gradle / Ivy

/*
 * Copyright (c) 2018-2024, Antonio Gabriel Muñoz Conejo 
 * Distributed under the terms of the MIT License
 */
package com.github.tonivade.zeromock.api;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.lang.reflect.Type;

import com.github.tonivade.purefun.core.Function1;
import com.github.tonivade.purefun.type.Option;
import com.github.tonivade.purefun.type.Try;
import com.github.tonivade.purejson.JsonNode;
import com.github.tonivade.purejson.PureJson;

import jakarta.xml.bind.DataBindingException;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Unmarshaller;

public final class Deserializers {

  private Deserializers() {}

  public static Function1> json() {
    return plain().andThen(asJson());
  }

  @SafeVarargs
  public static  Function1> xmlToObject(T...reified) {
    return xmlToObject(getClassOf(reified));
  }

  public static  Function1> xmlToObject(Class clazz) {
    return bytes -> Try.of(() -> Deserializers.fromXml(bytes, clazz));
  }

  public static  Function1> jsonToObject(Function1 deserializer) {
    return toObject(deserializer.liftTry());
  }

  @SafeVarargs
  public static  Function1>> jsonToObject(T... reified) {
    return jsonToObject(getClassOf(reified));
  }

  public static  Function1>> jsonToObject(Class clazz) {
    return toObject(fromJson(clazz));
  }

  public static  Function1>> jsonTo(Type type) {
    return toObject(fromJson(type));
  }

  public static Function1 plain() {
    return Bytes::asString;
  }

  private static Function1> asJson() {
    return PureJson::parse;
  }

  private static  Function1>> fromJson(Type type) {
    return json -> fromJson(json, type);
  }

  public static  Function1> toObject(Function1> deserializer) {
    return plain().andThen(deserializer);
  }

  @SuppressWarnings("unchecked")
  private static  T fromXml(Bytes bytes, Class clazz) {
    try (InputStream input = new ByteArrayInputStream(bytes.toArray())) {
      JAXBContext context = JAXBContext.newInstance(clazz);
      Unmarshaller unmarshaller = context.createUnmarshaller();
      return (T) unmarshaller.unmarshal(input);
    } catch (IOException e) {
      throw new UncheckedIOException(e);
    } catch (JAXBException e) {
      throw new DataBindingException(e);
    }
  }

  private static  Try> fromJson(String json, Type type) {
    return new PureJson(type).fromJson(json);
  }

  @SuppressWarnings("unchecked")
  private static  Class getClassOf(T... reified) {
    if (reified.length > 0) {
      throw new IllegalArgumentException("do not pass arguments to this function, it's just a trick to get refied types");
    }
    return (Class) reified.getClass().getComponentType();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy