net.hamnaberg.json.codec.Codecs 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.ZonedDateTime;
import java.util.*;
import java.util.function.Function;
public abstract class Codecs {
private Codecs(){}
public static final JsonCodec CIdentity = new DefaultJsonCodec<>(
Decoders.DIdentity,
Encoders.EIdentity,
"Identity"
);
public static final JsonCodec CString = new DefaultJsonCodec<>(
Decoders.DString,
Encoders.EString,
"String"
);
public static final JsonCodec CNumber = new DefaultJsonCodec<>(
Decoders.DNumber,
Encoders.ENumber,
"Number"
);
public static final JsonCodec CLong = new DefaultJsonCodec<>(
Decoders.DLong,
Encoders.ELong,
"Long"
);
public static final JsonCodec CDouble = new DefaultJsonCodec<>(
Decoders.DDouble,
Encoders.EDouble,
"Double"
);
public static final JsonCodec CInt = new DefaultJsonCodec<>(
Decoders.DInt,
Encoders.EInt,
"Int"
);
public static final JsonCodec CBoolean = new DefaultJsonCodec<>(
Decoders.DBoolean,
Encoders.EBoolean,
"Boolean"
);
public static final JsonCodec CURI = new DefaultJsonCodec<>(
Decoders.DURI,
Encoders.EURI,
"URI"
);
public static final JsonCodec CURL = new DefaultJsonCodec<>(
Decoders.DURL,
Encoders.EURL,
"URL"
);
public static final JsonCodec CUUID = new DefaultJsonCodec<>(
Decoders.DUUID,
Encoders.EUUID,
"UUID"
);
public static final JsonCodec CISODateTimeUTC = new DefaultJsonCodec<>(
Decoders.DISODateTimeUTC,
Encoders.EISODateTimeUTC,
"ZonedDateTime"
);
public static final JsonCodec CISOInstantUTC = new DefaultJsonCodec<>(
Decoders.DISOInstantUTC,
Encoders.EISOInstantUTC,
"Instant"
);
public static JsonCodec nullCodec() {
return new DefaultJsonCodec<>(
ignore -> DecodeResult.ok(null),
ignore -> Json.jNull(),
"Null"
);
}
public static JsonCodec> listCodec(JsonCodec codec) {
return new DefaultJsonCodec<>(
Decoders.listDecoder(codec),
Encoders.listEncoder(codec),
String.format("List(%s)", codec.toString())
);
}
public static JsonCodec> setCodec(JsonCodec codec) {
return new DefaultJsonCodec<>(
Decoders.setDecoder(codec),
Encoders.setEncoder(codec),
String.format("Set(%s)", codec.toString())
);
}
public static JsonCodec> optionalCodec(JsonCodec codec) {
return new DefaultJsonCodec<>(
Decoders.optionalDecoder(codec),
Encoders.optionalencoder(codec),
String.format("Optional(%s)", codec.toString())
);
}
public static JsonCodec objectCodec(Function> decoder, Function encoder) {
return JsonCodec.lift(
json -> decoder.apply(json.asJsonObjectOrEmpty()),
a ->encoder.apply(a).asJValue()
);
}
public static JsonCodec of(NamedJsonCodec c1) {
return codec(c1);
}
public static JsonCodec> of(NamedJsonCodec c1, NamedJsonCodec c2) {
return codec(Iso.identity(), c1, c2);
}
public static JsonCodec> of(NamedJsonCodec c1, NamedJsonCodec c2, NamedJsonCodec c3) {
return codec(Iso.identity(), c1, c2, c3);
}
public static JsonCodec> of(NamedJsonCodec c1, NamedJsonCodec c2, NamedJsonCodec c3, NamedJsonCodec c4) {
return codec(Iso.identity(), c1, c2, c3, c4);
}
public static JsonCodec> of(NamedJsonCodec c1, NamedJsonCodec c2, NamedJsonCodec c3, NamedJsonCodec c4, NamedJsonCodec c5) {
return codec(Iso.identity(), c1, c2, c3, c4, c5);
}
public static JsonCodec> of(NamedJsonCodec c1, NamedJsonCodec c2, NamedJsonCodec c3, NamedJsonCodec c4, NamedJsonCodec c5, NamedJsonCodec c6) {
return codec(Iso.identity(), c1, c2, c3, c4, c5, c6);
}
public static JsonCodec> of(NamedJsonCodec c1, NamedJsonCodec c2, NamedJsonCodec c3, NamedJsonCodec c4, NamedJsonCodec c5, NamedJsonCodec c6, NamedJsonCodec c7) {
return codec(Iso.identity(), c1, c2, c3, c4, c5, c6, c7);
}
public static JsonCodec> of(NamedJsonCodec c1, NamedJsonCodec c2, NamedJsonCodec c3, NamedJsonCodec c4, NamedJsonCodec c5, NamedJsonCodec c6, NamedJsonCodec c7, NamedJsonCodec c8) {
return codec(Iso.identity(), c1, c2, c3, c4, c5, c6, c7, c8);
}
public static JsonCodec> of(NamedJsonCodec c1, NamedJsonCodec c2, NamedJsonCodec c3, NamedJsonCodec c4, NamedJsonCodec c5, NamedJsonCodec c6, NamedJsonCodec c7, NamedJsonCodec c8, NamedJsonCodec c9) {
return codec(Iso.identity(), c1, c2, c3, c4, c5, c6, c7, c8, c9);
}
public static JsonCodec> of(NamedJsonCodec c1, NamedJsonCodec c2, NamedJsonCodec c3, NamedJsonCodec c4, NamedJsonCodec c5, NamedJsonCodec c6, NamedJsonCodec c7, NamedJsonCodec c8, NamedJsonCodec c9, NamedJsonCodec c10) {
return codec(Iso.identity(), c1, c2, c3, c4, c5, c6, c7, c8, c9, c10);
}
public static JsonCodec> of(NamedJsonCodec c1, NamedJsonCodec c2, NamedJsonCodec c3, NamedJsonCodec c4, NamedJsonCodec c5, NamedJsonCodec c6, NamedJsonCodec c7, NamedJsonCodec c8, NamedJsonCodec c9, NamedJsonCodec c10, NamedJsonCodec c11) {
return codec(Iso.identity(), c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11);
}
public static JsonCodec> of(NamedJsonCodec c1, NamedJsonCodec c2, NamedJsonCodec c3, NamedJsonCodec c4, NamedJsonCodec c5, NamedJsonCodec c6, NamedJsonCodec c7, NamedJsonCodec c8, NamedJsonCodec c9, NamedJsonCodec c10, NamedJsonCodec c11, NamedJsonCodec c12) {
return codec(Iso.identity(), c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12);
}
public static JsonCodec> of(NamedJsonCodec c1, NamedJsonCodec c2, NamedJsonCodec c3, NamedJsonCodec c4, NamedJsonCodec c5, NamedJsonCodec c6, NamedJsonCodec c7, NamedJsonCodec c8, NamedJsonCodec c9, NamedJsonCodec c10, NamedJsonCodec c11, NamedJsonCodec c12, NamedJsonCodec c13) {
return codec(Iso.identity(), c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13);
}
public static JsonCodec> of(NamedJsonCodec c1, NamedJsonCodec c2, NamedJsonCodec c3, NamedJsonCodec c4, NamedJsonCodec c5, NamedJsonCodec c6, NamedJsonCodec c7, NamedJsonCodec c8, NamedJsonCodec c9, NamedJsonCodec c10, NamedJsonCodec c11, NamedJsonCodec c12, NamedJsonCodec c13, NamedJsonCodec c14) {
return codec(Iso.identity(), c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14);
}
public static JsonCodec> of(NamedJsonCodec c1, NamedJsonCodec c2, NamedJsonCodec c3, NamedJsonCodec c4, NamedJsonCodec c5, NamedJsonCodec c6, NamedJsonCodec c7, NamedJsonCodec c8, NamedJsonCodec c9, NamedJsonCodec c10, NamedJsonCodec c11, NamedJsonCodec c12, NamedJsonCodec c13, NamedJsonCodec c14, NamedJsonCodec c15) {
return codec(Iso.identity(), c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15);
}
public static JsonCodec> of(NamedJsonCodec c1, NamedJsonCodec c2, NamedJsonCodec c3, NamedJsonCodec c4, NamedJsonCodec c5, NamedJsonCodec c6, NamedJsonCodec c7, NamedJsonCodec c8, NamedJsonCodec c9, NamedJsonCodec c10, NamedJsonCodec c11, NamedJsonCodec c12, NamedJsonCodec c13, NamedJsonCodec c14, NamedJsonCodec c15, NamedJsonCodec c16) {
return codec(Iso.identity(), c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16);
}
public static JsonCodec> of(NamedJsonCodec c1, NamedJsonCodec c2, NamedJsonCodec c3, NamedJsonCodec c4, NamedJsonCodec c5, NamedJsonCodec c6, NamedJsonCodec c7, NamedJsonCodec c8, NamedJsonCodec c9, NamedJsonCodec c10, NamedJsonCodec c11, NamedJsonCodec c12, NamedJsonCodec c13, NamedJsonCodec c14, NamedJsonCodec c15, NamedJsonCodec c16, NamedJsonCodec c17) {
return codec(Iso.identity(), c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16, c17);
}
public static JsonCodec> of(NamedJsonCodec c1, NamedJsonCodec c2, NamedJsonCodec c3, NamedJsonCodec c4, NamedJsonCodec c5, NamedJsonCodec c6, NamedJsonCodec c7, NamedJsonCodec c8, NamedJsonCodec c9, NamedJsonCodec c10, NamedJsonCodec c11, NamedJsonCodec c12, NamedJsonCodec c13, NamedJsonCodec c14, NamedJsonCodec c15, NamedJsonCodec c16, NamedJsonCodec c17, NamedJsonCodec c18) {
return codec(Iso.identity(), c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16, c17, c18);
}
public static JsonCodec> of(NamedJsonCodec c1, NamedJsonCodec c2, NamedJsonCodec c3, NamedJsonCodec c4, NamedJsonCodec c5, NamedJsonCodec c6, NamedJsonCodec c7, NamedJsonCodec c8, NamedJsonCodec c9, NamedJsonCodec c10, NamedJsonCodec c11, NamedJsonCodec c12, NamedJsonCodec c13, NamedJsonCodec c14, NamedJsonCodec c15, NamedJsonCodec c16, NamedJsonCodec c17, NamedJsonCodec c18, NamedJsonCodec c19) {
return codec(Iso.identity(), c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16, c17, c18, c19);
}
public static JsonCodec