imay commented on a change in pull request #2343: [Tag System]Add classes related to "tag". URL: https://github.com/apache/incubator-doris/pull/2343#discussion_r357516725
########## File path: fe/src/main/java/org/apache/doris/common/util/GsonUtils.java ########## @@ -0,0 +1,257 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF 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 org.apache.doris.common.util; + +import com.google.common.base.Preconditions; +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.HashBasedTable; +import com.google.common.collect.HashMultimap; +import com.google.common.collect.LinkedHashMultimap; +import com.google.common.collect.LinkedListMultimap; +import com.google.common.collect.Multimap; +import com.google.common.collect.Table; +import com.google.common.reflect.TypeToken; +import com.google.gson.ExclusionStrategy; +import com.google.gson.FieldAttributes; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonArray; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; +import com.google.gson.annotations.SerializedName; + +import java.lang.reflect.Method; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +/* + * Some utilities about Gson. + * User should get GSON instance inside this class to do the serialization. + * + * GsonUtils.GSON.toJson(...) + * GsonUtils.GSON.fromJson(...) + * + * And developers may need to add other serialization adapters for custom complex java classes. + */ +public class GsonUtils { + + // the builder of GSON instance. + // Add any other adapters if necessary. + private static final GsonBuilder GSON_BUILDER = new GsonBuilder() + .addSerializationExclusionStrategy(new HiddenAnnotationExclusionStrategy()) + .enableComplexMapKeySerialization() + .registerTypeHierarchyAdapter(Table.class, new GuavaTableAdapter()) + .registerTypeHierarchyAdapter(Multimap.class, new GuavaMultimapAdapter()); + + // this instance is thread-safe. + public static final Gson GSON = GSON_BUILDER.create(); + + /* + * The exclusion strategy of gson serialization. + * Any fields without "@SerializedName" annotation with be ignore with serializing and deserializing. + */ + private static class HiddenAnnotationExclusionStrategy implements ExclusionStrategy { + public boolean shouldSkipField(FieldAttributes f) { + return f.getAnnotation(SerializedName.class) == null; + } + + @Override + public boolean shouldSkipClass(Class<?> clazz) { + return false; + } + } + + /* + * The json adapter for Guava Table. + * Current support: + * 1. HashBasedTable + * + * The RowKey, ColumnKey and Value classes in Table should also be serializable. + */ + private static class GuavaTableAdapter<R, C, V> + implements JsonSerializer<Table<R, C, V>>, JsonDeserializer<Table<R, C, V>> { + /* + * serialize Table<R, C, V> as: Review comment: Why we serialize table like this? To minimize size of serialized message? Better to comment the reason of it. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
