net.hamnaberg.json.codec.Encoders Maven / Gradle / Ivy
The newest version!
package net.hamnaberg.json.codec;
import net.hamnaberg.arities.*;
import net.hamnaberg.json.Json;
import java.net.URI;
import java.net.URL;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.function.Function;
import java.util.stream.Collectors;
public abstract class Encoders {
private Encoders(){}
public static final EncodeJson EIdentity = j -> j;
public static final EncodeJson EString = Json::jString;
public static final EncodeJson ENumber = Json::jNumber;
public static final EncodeJson ELong = Json::jNumber;
public static final EncodeJson EInt = Json::jNumber;
public static final EncodeJson EDouble = Json::jNumber;
public static final EncodeJson EBoolean = Json::jBoolean;
public static final EncodeJson EURI = EString.contramap(URI::toString);
public static final EncodeJson EURL = EString.contramap(URL::toExternalForm);
public static final EncodeJson EUUID = EString.contramap(UUID::toString);
public static final EncodeJson EISODateTimeUTC = zonedDateTimeEncoder(DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneOffset.UTC));
public static final EncodeJson EISOInstantUTC = instantEncoder(DateTimeFormatter.ISO_INSTANT.withZone(ZoneOffset.UTC));
public static EncodeJson zonedDateTimeEncoder(DateTimeFormatter formatter) {
return EString.contramap(formatter::format);
}
public static EncodeJson instantEncoder(DateTimeFormatter formatter) {
return EString.contramap(formatter::format);
}
public static EncodeJson> listEncoder(EncodeJson encoder) {
return value -> Json.jArray(value.stream().map(encoder::toJson).collect(Collectors.toUnmodifiableList()));
}
public static EncodeJson> setEncoder(EncodeJson encoder) {
return listEncoder(encoder).contramap(List::copyOf);
}
public static EncodeJson> optionalencoder(EncodeJson encoder) {
return value -> value.map(encoder::toJson).orElse(Json.jNull());
}
public static EncodeJson objectEncoder(Function encoder) {
return a -> encoder.apply(a).asJValue();
}
public static EncodeJson> encode(FieldEncoder e1, FieldEncoder e2) {
return tuple -> Json.jObject(
Json.tuple(e1.name, e1.toJson(tuple._1())),
Json.tuple(e2.name, e2.toJson(tuple._2()))
);
}
public static EncodeJson encode(FieldEncoder e1, FieldEncoder e2, Function> f) {
return type -> encode(e1, e2).toJson(f.apply(type));
}
public static EncodeJson> encode(FieldEncoder e1, FieldEncoder e2, FieldEncoder e3) {
return tuple -> Json.jObject(
Json.tuple(e1.name, e1.toJson(tuple._1())),
Json.tuple(e2.name, e2.toJson(tuple._2())),
Json.tuple(e3.name, e3.toJson(tuple._3()))
);
}
public static EncodeJson encode(FieldEncoder e1, FieldEncoder e2, FieldEncoder e3, Function> f) {
return type -> encode(e1, e2, e3).toJson(f.apply(type));
}
public static EncodeJson> encode(FieldEncoder e1, FieldEncoder e2, FieldEncoder e3, FieldEncoder e4) {
return tuple -> Json.jObject(
Json.tuple(e1.name, e1.toJson(tuple._1())),
Json.tuple(e2.name, e2.toJson(tuple._2())),
Json.tuple(e3.name, e3.toJson(tuple._3())),
Json.tuple(e4.name, e4.toJson(tuple._4()))
);
}
public static EncodeJson encode(FieldEncoder e1, FieldEncoder e2, FieldEncoder e3, FieldEncoder e4, Function> f) {
return type -> encode(e1, e2, e3, e4).toJson(f.apply(type));
}
public static EncodeJson> encode(FieldEncoder e1, FieldEncoder e2, FieldEncoder e3, FieldEncoder e4, FieldEncoder e5) {
return tuple -> Json.jObject(
Json.tuple(e1.name, e1.toJson(tuple._1())),
Json.tuple(e2.name, e2.toJson(tuple._2())),
Json.tuple(e3.name, e3.toJson(tuple._3())),
Json.tuple(e4.name, e4.toJson(tuple._4())),
Json.tuple(e5.name, e5.toJson(tuple._5()))
);
}
public static EncodeJson encode(FieldEncoder e1, FieldEncoder e2, FieldEncoder e3, FieldEncoder e4, FieldEncoder e5, Function> f) {
return type -> encode(e1, e2, e3, e4, e5).toJson(f.apply(type));
}
public static EncodeJson> encode(FieldEncoder e1, FieldEncoder e2, FieldEncoder e3, FieldEncoder e4, FieldEncoder e5, FieldEncoder e6) {
return tuple -> Json.jObject(
Json.tuple(e1.name, e1.toJson(tuple._1())),
Json.tuple(e2.name, e2.toJson(tuple._2())),
Json.tuple(e3.name, e3.toJson(tuple._3())),
Json.tuple(e4.name, e4.toJson(tuple._4())),
Json.tuple(e5.name, e5.toJson(tuple._5())),
Json.tuple(e6.name, e6.toJson(tuple._6()))
);
}
public static EncodeJson encode(FieldEncoder e1, FieldEncoder e2, FieldEncoder e3, FieldEncoder e4, FieldEncoder e5, FieldEncoder e6, Function> f) {
return type -> encode(e1, e2, e3, e4, e5, e6).toJson(f.apply(type));
}
public static EncodeJson> encode(FieldEncoder e1, FieldEncoder e2, FieldEncoder e3, FieldEncoder e4, FieldEncoder e5, FieldEncoder e6, FieldEncoder e7) {
return tuple -> Json.jObject(
Json.tuple(e1.name, e1.toJson(tuple._1())),
Json.tuple(e2.name, e2.toJson(tuple._2())),
Json.tuple(e3.name, e3.toJson(tuple._3())),
Json.tuple(e4.name, e4.toJson(tuple._4())),
Json.tuple(e5.name, e5.toJson(tuple._5())),
Json.tuple(e6.name, e6.toJson(tuple._6())),
Json.tuple(e7.name, e7.toJson(tuple._7()))
);
}
public static EncodeJson encode(FieldEncoder e1, FieldEncoder e2, FieldEncoder e3, FieldEncoder e4, FieldEncoder e5, FieldEncoder e6, FieldEncoder e7, Function> f) {
return type -> encode(e1, e2, e3, e4, e5, e6, e7).toJson(f.apply(type));
}
public static EncodeJson> encode(FieldEncoder e1, FieldEncoder e2, FieldEncoder e3, FieldEncoder e4, FieldEncoder e5, FieldEncoder e6, FieldEncoder e7, FieldEncoder e8) {
return tuple -> Json.jObject(
Json.tuple(e1.name, e1.toJson(tuple._1())),
Json.tuple(e2.name, e2.toJson(tuple._2())),
Json.tuple(e3.name, e3.toJson(tuple._3())),
Json.tuple(e4.name, e4.toJson(tuple._4())),
Json.tuple(e5.name, e5.toJson(tuple._5())),
Json.tuple(e6.name, e6.toJson(tuple._6())),
Json.tuple(e7.name, e7.toJson(tuple._7())),
Json.tuple(e8.name, e8.toJson(tuple._8()))
);
}
public static EncodeJson encode(FieldEncoder e1, FieldEncoder e2, FieldEncoder e3, FieldEncoder e4, FieldEncoder e5, FieldEncoder e6, FieldEncoder e7, FieldEncoder e8, Function> f) {
return type -> encode(e1, e2, e3, e4, e5, e6, e7, e8).toJson(f.apply(type));
}
public static EncodeJson> encode(FieldEncoder e1, FieldEncoder e2, FieldEncoder e3, FieldEncoder e4, FieldEncoder e5, FieldEncoder e6, FieldEncoder e7, FieldEncoder e8, FieldEncoder e9) {
return tuple -> Json.jObject(
Json.tuple(e1.name, e1.toJson(tuple._1())),
Json.tuple(e2.name, e2.toJson(tuple._2())),
Json.tuple(e3.name, e3.toJson(tuple._3())),
Json.tuple(e4.name, e4.toJson(tuple._4())),
Json.tuple(e5.name, e5.toJson(tuple._5())),
Json.tuple(e6.name, e6.toJson(tuple._6())),
Json.tuple(e7.name, e7.toJson(tuple._7())),
Json.tuple(e8.name, e8.toJson(tuple._8())),
Json.tuple(e9.name, e9.toJson(tuple._9()))
);
}
public static EncodeJson encode(FieldEncoder e1, FieldEncoder e2, FieldEncoder e3, FieldEncoder e4, FieldEncoder e5, FieldEncoder e6, FieldEncoder e7, FieldEncoder e8, FieldEncoder e9, Function> f) {
return type -> encode(e1, e2, e3, e4, e5, e6, e7, e8, e9).toJson(f.apply(type));
}
public static EncodeJson> encode(FieldEncoder e1, FieldEncoder e2, FieldEncoder e3, FieldEncoder e4, FieldEncoder e5, FieldEncoder e6, FieldEncoder e7, FieldEncoder e8, FieldEncoder e9, FieldEncoder e10) {
return tuple -> Json.jObject(
Json.tuple(e1.name, e1.toJson(tuple._1())),
Json.tuple(e2.name, e2.toJson(tuple._2())),
Json.tuple(e3.name, e3.toJson(tuple._3())),
Json.tuple(e4.name, e4.toJson(tuple._4())),
Json.tuple(e5.name, e5.toJson(tuple._5())),
Json.tuple(e6.name, e6.toJson(tuple._6())),
Json.tuple(e7.name, e7.toJson(tuple._7())),
Json.tuple(e8.name, e8.toJson(tuple._8())),
Json.tuple(e9.name, e9.toJson(tuple._9())),
Json.tuple(e10.name, e10.toJson(tuple._10()))
);
}
public static EncodeJson encode(FieldEncoder e1, FieldEncoder e2, FieldEncoder e3, FieldEncoder e4, FieldEncoder e5, FieldEncoder e6, FieldEncoder e7, FieldEncoder e8, FieldEncoder e9, FieldEncoder e10, Function> f) {
return type -> encode(e1, e2, e3, e4, e5, e6, e7, e8, e9, e10).toJson(f.apply(type));
}
public static EncodeJson> encode(FieldEncoder e1, FieldEncoder e2, FieldEncoder e3, FieldEncoder e4, FieldEncoder e5, FieldEncoder e6, FieldEncoder e7, FieldEncoder e8, FieldEncoder e9, FieldEncoder e10, FieldEncoder e11) {
return tuple -> Json.jObject(
Json.tuple(e1.name, e1.toJson(tuple._1())),
Json.tuple(e2.name, e2.toJson(tuple._2())),
Json.tuple(e3.name, e3.toJson(tuple._3())),
Json.tuple(e4.name, e4.toJson(tuple._4())),
Json.tuple(e5.name, e5.toJson(tuple._5())),
Json.tuple(e6.name, e6.toJson(tuple._6())),
Json.tuple(e7.name, e7.toJson(tuple._7())),
Json.tuple(e8.name, e8.toJson(tuple._8())),
Json.tuple(e9.name, e9.toJson(tuple._9())),
Json.tuple(e10.name, e10.toJson(tuple._10())),
Json.tuple(e11.name, e11.toJson(tuple._11()))
);
}
public static EncodeJson encode(FieldEncoder e1, FieldEncoder e2, FieldEncoder e3, FieldEncoder e4, FieldEncoder e5, FieldEncoder e6, FieldEncoder e7, FieldEncoder e8, FieldEncoder e9, FieldEncoder e10, FieldEncoder e11, Function> f) {
return type -> encode(e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11).toJson(f.apply(type));
}
public static EncodeJson> encode(FieldEncoder e1, FieldEncoder e2, FieldEncoder e3, FieldEncoder e4, FieldEncoder e5, FieldEncoder e6, FieldEncoder e7, FieldEncoder e8, FieldEncoder e9, FieldEncoder e10, FieldEncoder e11, FieldEncoder e12) {
return tuple -> Json.jObject(
Json.tuple(e1.name, e1.toJson(tuple._1())),
Json.tuple(e2.name, e2.toJson(tuple._2())),
Json.tuple(e3.name, e3.toJson(tuple._3())),
Json.tuple(e4.name, e4.toJson(tuple._4())),
Json.tuple(e5.name, e5.toJson(tuple._5())),
Json.tuple(e6.name, e6.toJson(tuple._6())),
Json.tuple(e7.name, e7.toJson(tuple._7())),
Json.tuple(e8.name, e8.toJson(tuple._8())),
Json.tuple(e9.name, e9.toJson(tuple._9())),
Json.tuple(e10.name, e10.toJson(tuple._10())),
Json.tuple(e11.name, e11.toJson(tuple._11())),
Json.tuple(e12.name, e12.toJson(tuple._12()))
);
}
public static EncodeJson encode(FieldEncoder e1, FieldEncoder e2, FieldEncoder e3, FieldEncoder e4, FieldEncoder e5, FieldEncoder e6, FieldEncoder e7, FieldEncoder e8, FieldEncoder e9, FieldEncoder e10, FieldEncoder e11, FieldEncoder e12, Function> f) {
return type -> encode(e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12).toJson(f.apply(type));
}
public static EncodeJson> encode(FieldEncoder e1, FieldEncoder e2, FieldEncoder e3, FieldEncoder e4, FieldEncoder e5, FieldEncoder e6, FieldEncoder e7, FieldEncoder e8, FieldEncoder e9, FieldEncoder e10, FieldEncoder e11, FieldEncoder e12, FieldEncoder e13) {
return tuple -> Json.jObject(
Json.tuple(e1.name, e1.toJson(tuple._1())),
Json.tuple(e2.name, e2.toJson(tuple._2())),
Json.tuple(e3.name, e3.toJson(tuple._3())),
Json.tuple(e4.name, e4.toJson(tuple._4())),
Json.tuple(e5.name, e5.toJson(tuple._5())),
Json.tuple(e6.name, e6.toJson(tuple._6())),
Json.tuple(e7.name, e7.toJson(tuple._7())),
Json.tuple(e8.name, e8.toJson(tuple._8())),
Json.tuple(e9.name, e9.toJson(tuple._9())),
Json.tuple(e10.name, e10.toJson(tuple._10())),
Json.tuple(e11.name, e11.toJson(tuple._11())),
Json.tuple(e12.name, e12.toJson(tuple._12())),
Json.tuple(e13.name, e13.toJson(tuple._13()))
);
}
public static EncodeJson encode(FieldEncoder e1, FieldEncoder e2, FieldEncoder e3, FieldEncoder