<commit_before>﻿using Newtonsoft.Json;
using SignalGo.Shared.Converters;
using SignalGo.Shared.DataTypes;
using System;
using System.Collections.Generic;

namespace SignalGo.Client
{
    public static class ClientSerializationHelper
    {
        public static string SerializeObject(this object obj, NullValueHandling nullValueHandling = NullValueHandling.Ignore, CustomDataExchangerAttribute[] customDataExchanger = null)
        {
            if (obj == null)
                return "";
            //if (serverBase != null && serverBase.InternalSetting.IsEnabledDataExchanger)
            return JsonConvert.SerializeObject(obj, Formatting.None, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore, Converters = new List<JsonConverter>() { new DataExchangeConverter(LimitExchangeType.OutgoingCall, customDataExchanger) }, Formatting = Formatting.None, NullValueHandling = nullValueHandling });
            //return JsonConvert.SerializeObject(obj, new JsonSerializerSettings() { Formatting = Formatting.None, NullValueHandling = nullValueHandling, ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        }

        public static T DeserializeObject<T>(this string json, CustomDataExchangerAttribute[] customDataExchanger = null)
        {
            return (T)DeserializeObject(json, typeof(T), customDataExchanger: customDataExchanger);
        }

        public static object DeserializeObject(this string json, CustomDataExchangerAttribute[] customDataExchanger = null)
        {
            return DeserializeObject<object>(json, customDataExchanger: customDataExchanger);
        }

        public static object DeserializeObject(this string json, Type type, NullValueHandling nullValueHandling = NullValueHandling.Ignore, CustomDataExchangerAttribute[] customDataExchanger = null)
        {
            if (string.IsNullOrEmpty(json))
                return null;
            return JsonConvert.DeserializeObject(json, type, new JsonSerializerSettings() { Formatting = Formatting.None, NullValueHandling = nullValueHandling, ReferenceLoopHandling = ReferenceLoopHandling.Ignore, Converters = new List<JsonConverter>() { new DataExchangeConverter(LimitExchangeType.IncomingCall, customDataExchanger) } });
        }

        //public static object DeserializeByValidate(this string json, Type type, NullValueHandling nullValueHandling = NullValueHandling.Ignore, CustomDataExchangerAttribute[] customDataExchanger = null)
        //{
        //    if (string.IsNullOrEmpty(json))
        //        return null;
        //    if (!IsValidJson(json))
        //        json = SerializeObject(json, nullValueHandling, customDataExchanger);
        //    if (serverBase != null && serverBase.InternalSetting.IsEnabledDataExchanger)
        //        return JsonConvert.DeserializeObject(json, type, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore, Converters = new List<JsonConverter>() { new DataExchangeConverter(LimitExchangeType.IncomingCall, customDataExchanger) { Server = serverBase, Client = client } }, Formatting = Formatting.None, NullValueHandling = nullValueHandling });
        //    return JsonConvert.DeserializeObject(json, type, new JsonSerializerSettings() { Formatting = Formatting.None, NullValueHandling = nullValueHandling, ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        //}

        public static bool IsValidJson(this string json)
        {
            json = json.Trim();
            if ((json.StartsWith("{") && json.EndsWith("}")) ||
                (json.StartsWith("[") && json.EndsWith("]")) ||
                (json.StartsWith("\"") && json.EndsWith("\"")))
            {
                return true;
            }
            return false;
        }
    }
}
<commit_msg>add setting for dataexchanger in client<commit_after>﻿using Newtonsoft.Json;
using SignalGo.Shared.Converters;
using SignalGo.Shared.DataTypes;
using System;
using System.Collections.Generic;

namespace SignalGo.Client
{
    public static class ClientSerializationHelper
    {
        /// <summary>
        /// enable refrence serializing when duplicate object detected
        /// </summary>
        public static bool IsEnabledReferenceResolver { get; set; } = true;
        /// <summary>
        /// enable refrence serializing when duplicate list of objects detected
        /// </summary>
        public static bool IsEnabledReferenceResolverForArray { get; set; } = true;
        public static string SerializeObject(this object obj, NullValueHandling nullValueHandling = NullValueHandling.Ignore, CustomDataExchangerAttribute[] customDataExchanger = null)
        {
            if (obj == null)
                return "";
            //if (serverBase != null && serverBase.InternalSetting.IsEnabledDataExchanger)
            return JsonConvert.SerializeObject(obj, Formatting.None, new JsonSerializerSettings()
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                Converters = new List<JsonConverter>()
                {
                    new DataExchangeConverter(LimitExchangeType.OutgoingCall, customDataExchanger)
                    {
                        IsEnabledReferenceResolver = IsEnabledReferenceResolver,
                        IsEnabledReferenceResolverForArray = IsEnabledReferenceResolverForArray
                    }
                },
                Formatting = Formatting.None,
                NullValueHandling = nullValueHandling
            });
            //return JsonConvert.SerializeObject(obj, new JsonSerializerSettings() { Formatting = Formatting.None, NullValueHandling = nullValueHandling, ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        }

        public static T DeserializeObject<T>(this string json, CustomDataExchangerAttribute[] customDataExchanger = null)
        {
            return (T)DeserializeObject(json, typeof(T), customDataExchanger: customDataExchanger);
        }

        public static object DeserializeObject(this string json, CustomDataExchangerAttribute[] customDataExchanger = null)
        {
            return DeserializeObject<object>(json, customDataExchanger: customDataExchanger);
        }

        public static object DeserializeObject(this string json, Type type, NullValueHandling nullValueHandling = NullValueHandling.Ignore, CustomDataExchangerAttribute[] customDataExchanger = null)
        {
            if (string.IsNullOrEmpty(json))
                return null;
            return JsonConvert.DeserializeObject(json, type, new JsonSerializerSettings()
            {
                Formatting = Formatting.None,
                NullValueHandling = nullValueHandling,
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                Converters = new List<JsonConverter>() 
                {
                    new DataExchangeConverter(LimitExchangeType.IncomingCall, customDataExchanger)
                    {
                        IsEnabledReferenceResolver = IsEnabledReferenceResolver,
                        IsEnabledReferenceResolverForArray = IsEnabledReferenceResolverForArray
                    }
                }
            });
        }

        //public static object DeserializeByValidate(this string json, Type type, NullValueHandling nullValueHandling = NullValueHandling.Ignore, CustomDataExchangerAttribute[] customDataExchanger = null)
        //{
        //    if (string.IsNullOrEmpty(json))
        //        return null;
        //    if (!IsValidJson(json))
        //        json = SerializeObject(json, nullValueHandling, customDataExchanger);
        //    if (serverBase != null && serverBase.InternalSetting.IsEnabledDataExchanger)
        //        return JsonConvert.DeserializeObject(json, type, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore, Converters = new List<JsonConverter>() { new DataExchangeConverter(LimitExchangeType.IncomingCall, customDataExchanger) { Server = serverBase, Client = client } }, Formatting = Formatting.None, NullValueHandling = nullValueHandling });
        //    return JsonConvert.DeserializeObject(json, type, new JsonSerializerSettings() { Formatting = Formatting.None, NullValueHandling = nullValueHandling, ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        //}

        public static bool IsValidJson(this string json)
        {
            json = json.Trim();
            if ((json.StartsWith("{") && json.EndsWith("}")) ||
                (json.StartsWith("[") && json.EndsWith("]")) ||
                (json.StartsWith("\"") && json.EndsWith("\"")))
            {
                return true;
            }
            return false;
        }
    }
}

{'fraction_non_alphanumeric': 0.08700616579127654, 'fraction_numerical': 0.0, 'mean_word_length': 2.8132346538963864, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '806810', 'n_tokens_mistral': 2150, 'n_tokens_neox': 2112, 'n_words': 455}