/* * Copyright (c) 2018 THL A29 Limited, a Tencent company. All Rights Reserved. * * Licensed 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. */ using Newtonsoft.Json; using System; using System.Collections.Generic; namespace TencentCloud.Common { public abstract class AbstractModel { internal abstract void ToMap(Dictionary map, string prefix); protected void SetParamSimple(Dictionary map, String key, V value) { if ((value!= null) && !value.Equals(default(V)) ) { key = key.Substring(0, 1).ToUpper() + key.Substring(1); key = key.Replace("_", "."); map.Add(key, value.ToString()); } } protected void SetParamArraySimple(Dictionary map, string prefix, V[] array) { if (array != null) { for (int i = 0; i < array.Length; i++) { this.SetParamSimple(map, prefix + i, array[i]); } } } protected void SetParamObj(Dictionary map, String prefix, V obj) where V: AbstractModel { if (obj != null) { obj.ToMap(map, prefix); } } protected void SetParamArrayObj(Dictionary map, String prefix, V[] array) where V : AbstractModel { if (array != null) { for (int i = 0; i < array.Length; i++) { this.SetParamObj(map, prefix + i + ".", array[i]); } } } /// /// Serialize an obect of AbstractModel into JSON string. /// /// A class inherited from AbstrctModel. /// An object of the class. /// JSON formatted string. public static string ToJsonString(V obj) where V: AbstractModel { return JsonConvert.SerializeObject(obj); } /// /// Deserialize JSON formatted string into an object of a class inherited from AbstractModel. /// /// A class inherited from AbstrctModel. /// JSON formatted string. /// An object of the class. public static V FromJsonString(string json) { return JsonConvert.DeserializeObject(json); } } }