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

co.elastic.clients.json.JsonEnums Maven / Gradle / Ivy

There is a newer version: 9.0.0-beta1
Show newest version
/*
 * Licensed to Elasticsearch B.V. under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch B.V. licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package co.elastic.clients.json;

import jakarta.json.stream.JsonGenerator;
import jakarta.json.stream.JsonParser;
import jakarta.json.stream.JsonParsingException;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;

public class JsonEnums {

    public static class Deserializer extends JsonpDeserializerBase {

        private static final EnumSet acceptedEvents =
            EnumSet.of(JsonParser.Event.VALUE_STRING, JsonParser.Event.KEY_NAME);
        private static final EnumSet nativeEvents = EnumSet.of(JsonParser.Event.VALUE_STRING);

        private final Map lookupTable;

        public Deserializer(T[] values) {
            this(values, acceptedEvents);
        }

        protected Deserializer(T[] values, EnumSet acceptedEvents) {
            super(acceptedEvents, nativeEvents);

            // Use the same size calculation as in java.lang.Enum.enumConstantDirectory
            this.lookupTable = new HashMap<>((int)(values.length / 0.75f) + 1);
            for (T member : values) {
                String jsonValue = member.jsonValue();
                if (jsonValue != null) { // _Custom enum members have a null jsonValue
                    this.lookupTable.put(jsonValue, member);
                }
                String[] aliases = member.aliases();
                if (aliases != null) {
                    for (String alias: aliases) {
                        this.lookupTable.put(alias, member);
                    }
                }
            }
        }

        @Override
        public T deserialize(JsonParser parser, JsonpMapper mapper, JsonParser.Event event) {
            String value = parser.getString();
            return deserialize(value, parser);
        }

        /**
         * Get the enum member for a JSON string value
         *
         * @param value the JSON value
         * @param parser parsing context
         * @return the enum member
         * @throws JsonParsingException if no matching enum was found
         */
        public T deserialize(String value, JsonParser parser) {
            T result = this.lookupTable.get(value);
            if (result == null) {
                throw new JsonpMappingException("Invalid enum '" + value + "'", parser.getLocation());
            }
            return result;
        }

        /**
         * Get the enum member for a JSON string value
         *
         * @param value the JSON value
         * @return the enum member
         * @throws IllegalArgumentException if no matching enum was found
         */
        public T parse(String value) {
            T result = this.lookupTable.get(value);
            if (result == null) {
                throw new NoSuchElementException("Invalid enum '" + value + "'");
            }
            return result;
        }

        /**
         * An enum deserializer that also accepts boolean values. Used for a few properties that started as two-state booleans
         * and evolved into enums over time.
         */
        public static class AllowingBooleans extends JsonEnum.Deserializer {

            private static final EnumSet acceptedEventsAndBoolean = EnumSet.of(
                JsonParser.Event.VALUE_STRING, JsonParser.Event.KEY_NAME, JsonParser.Event.VALUE_TRUE, JsonParser.Event.VALUE_FALSE
            );

            public AllowingBooleans(T[] values) {
                super(values, acceptedEventsAndBoolean);
            }

            @Override
            public T deserialize(JsonParser parser, JsonpMapper mapper, JsonParser.Event event) {
                String value;
                if (event == JsonParser.Event.VALUE_TRUE) {
                    value = "true";
                } else if (event == JsonParser.Event.VALUE_FALSE) {
                    value = "false";
                } else {
                    value = parser.getString();
                }

                return deserialize(value, parser);
            }
        }
    }

    static class PipeSeparatedDeserializer extends JsonpDeserializerBase> {
        private final JsonEnum.Deserializer enumDeserializer;

        protected PipeSeparatedDeserializer(JsonEnum.Deserializer enumDeserializer) {
            super(EnumSet.of(JsonParser.Event.VALUE_STRING));
            this.enumDeserializer = enumDeserializer;
        }

        @Override
        public List deserialize(JsonParser parser, JsonpMapper mapper, JsonParser.Event event) {
            String[] values = parser.getString().split("\\|"); // There's a fast path for this kind of regex

            List result = new ArrayList<>(values.length);
            for (String v : values) {
                result.add(enumDeserializer.deserialize(v.trim(), parser));
            }

            return result;
        }
    }

    public static  void serializePipeSeparated(List values, JsonGenerator generator) {
        if (values.size() == 1) {
            generator.write(values.get(0).jsonValue());
        } else {
            generator.write(values.stream().map(JsonEnum::jsonValue).collect(Collectors.joining("|")));
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy