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

com.github.nagyesta.lowkeyvault.model.json.util.Base64Serializer Maven / Gradle / Ivy

There is a newer version: 2.5.81
Show newest version
package com.github.nagyesta.lowkeyvault.model.json.util;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.util.Base64;
import java.util.Optional;

public class Base64Serializer extends JsonSerializer {

    private static final Base64.Encoder ENCODER = Base64.getUrlEncoder().withoutPadding();
    private final Base64.Encoder encoder;

    public Base64Serializer() {
        this(ENCODER);
    }

    protected Base64Serializer(final Base64.Encoder encoder) {
        this.encoder = encoder;
    }

    @Override
    public void serialize(final byte[] value, final JsonGenerator generator, final SerializerProvider provider) throws IOException {
        final String text = base64Encode(value);
        if (text != null) {
            generator.writeString(text);
        } else {
            generator.writeNull();
        }
    }

    protected String base64Encode(final byte[] value) {
        final Optional optional = Optional.ofNullable(value);
        return optional.filter(v -> v.length > 0)
                .map(encoder::encodeToString)
                .orElse(null);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy