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

com.arangodb.internal.serde.InternalSerializers Maven / Gradle / Ivy

There is a newer version: 7.13.0
Show newest version
package com.arangodb.internal.serde;

import com.arangodb.entity.CollectionType;
import com.arangodb.entity.arangosearch.CollectionLink;
import com.arangodb.entity.arangosearch.FieldLink;
import com.arangodb.internal.velocystream.internal.AuthenticationRequest;
import com.arangodb.internal.velocystream.internal.JwtAuthenticationRequest;
import com.arangodb.util.RawBytes;
import com.arangodb.util.RawJson;
import com.arangodb.internal.InternalRequest;
import com.arangodb.shaded.fasterxml.jackson.core.JsonGenerator;
import com.arangodb.shaded.fasterxml.jackson.core.JsonParser;
import com.arangodb.shaded.fasterxml.jackson.databind.JsonSerializer;
import com.arangodb.shaded.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public final class InternalSerializers {

    static final JsonSerializer RAW_JSON_SERIALIZER = new JsonSerializer() {
        @Override
        public void serialize(RawJson value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
            gen.writeTree(SerdeUtils.INSTANCE.parseJson(value.getValue()));
        }
    };
    static final JsonSerializer RAW_BYTES_SERIALIZER = new JsonSerializer() {
        @Override
        public void serialize(RawBytes value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
            // TODO: find a way to append raw bytes directly
            // see https://github.com/FasterXML/jackson-dataformats-binary/issues/331
            try (JsonParser parser = gen.getCodec().getFactory().createParser(value.getValue())) {
                gen.writeTree(parser.readValueAsTree());
            }
        }
    };
    static final JsonSerializer AUTHENTICATION_REQUEST =
            new JsonSerializer() {
                @Override
                public void serialize(AuthenticationRequest value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
                    gen.writeStartArray();
                    gen.writeNumber(value.getVersion());
                    gen.writeNumber(value.getType());
                    gen.writeString(value.getEncryption());
                    gen.writeString(value.getUser());
                    gen.writeString(value.getPassword());
                    gen.writeEndArray();
                }
            };
    static final JsonSerializer JWT_AUTHENTICATION_REQUEST =
            new JsonSerializer() {
                @Override
                public void serialize(JwtAuthenticationRequest value, JsonGenerator gen,
                                      SerializerProvider serializers) throws IOException {
                    gen.writeStartArray();
                    gen.writeNumber(value.getVersion());
                    gen.writeNumber(value.getType());
                    gen.writeString(value.getEncryption());
                    gen.writeString(value.getToken());
                    gen.writeEndArray();
                }
            };
    static final JsonSerializer REQUEST = new JsonSerializer() {
        @Override
        public void serialize(InternalRequest value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
            gen.writeStartArray();
            gen.writeNumber(value.getVersion());
            gen.writeNumber(value.getType());
            gen.writeString(value.getDbName().get());
            gen.writeNumber(value.getRequestType().getType());
            gen.writeString(value.getPath());
            gen.writeStartObject();
            for (final Map.Entry entry : value.getQueryParam().entrySet()) {
                gen.writeStringField(entry.getKey(), entry.getValue());
            }
            gen.writeEndObject();
            gen.writeStartObject();
            for (final Map.Entry entry : value.getHeaderParam().entrySet()) {
                gen.writeStringField(entry.getKey(), entry.getValue());
            }
            gen.writeEndObject();
            gen.writeEndArray();
        }
    };
    static final JsonSerializer COLLECTION_TYPE = new JsonSerializer() {
        @Override
        public void serialize(CollectionType value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
            gen.writeNumber(value.getType());
        }
    };

    private InternalSerializers() {
    }

    public static class CollectionSchemaRuleSerializer extends JsonSerializer {
        @Override
        public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
            gen.writeTree(SerdeUtils.INSTANCE.parseJson(value));
        }
    }

    public static class FieldLinksSerializer extends JsonSerializer> {
        @Override
        public void serialize(Collection value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
            Map mapLikeValue = new HashMap<>();
            for (FieldLink fl : value) {
                mapLikeValue.put(fl.getName(), fl);
            }
            gen.writeObject(mapLikeValue);
        }
    }

    public static class CollectionLinksSerializer extends JsonSerializer> {
        @Override
        public void serialize(Collection value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
            Map mapLikeValue = new HashMap<>();
            for (CollectionLink cl : value) {
                mapLikeValue.put(cl.getName(), cl);
            }
            gen.writeObject(mapLikeValue);
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy