<commit_before>﻿using System;
using Jarvis.NEventStoreEx.CommonDomainEx;
using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers;
using System.Collections.Generic;

namespace Jarvis.Framework.Shared.IdentitySupport.Serialization
{
    public static class MongoFlatIdSerializerHelper
    {
        internal static IIdentityConverter IdentityConverter { get; set; }

        public static T ToIdentity<T>(String id)
        {
            if (MongoFlatIdSerializerHelper.IdentityConverter == null)
                throw new Exception("Identity converter not set in MongoFlatIdSerializerHelper.IdentityConverter property");

            return (T)MongoFlatIdSerializerHelper.IdentityConverter.ToIdentity(id);
        }
    }

    public class GenericIdentityBsonSerializer : SerializerBase<IIdentity>
    {
        public override IIdentity Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            if (context.Reader.CurrentBsonType == BsonType.Null)
            {
                context.Reader.ReadNull();
                return null;
            }

            var id = context.Reader.ReadString();

            return MongoFlatIdSerializerHelper.ToIdentity<EventStoreIdentity>(id);
        }

        public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, IIdentity value)
        {
            if (value == null)
            {
                context.Writer.WriteNull();
            }
            else
            {
                context.Writer.WriteString(value.AsString());
            }
        }
    }

    public class TypedEventStoreIdentityBsonSerializer<T> : SerializerBase<T> where T : EventStoreIdentity
    {

        public override T Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            if (context.Reader.CurrentBsonType == BsonType.Null)
            {
                context.Reader.ReadNull();
                return null;
            }

            var id = context.Reader.ReadString();

            return MongoFlatIdSerializerHelper.ToIdentity<T>(id);
        }

        public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, T value)
        {
            if (value == null)
            {
                context.Writer.WriteNull();
            }
            else
            {
                context.Writer.WriteString(value);
            }
        }
    }

    public class IdentityArrayBsonSerializer<T> : SerializerBase<T[]> where T : IIdentity
    {
        public override T[] Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            if (context.Reader.CurrentBsonType == BsonType.Null)
            {
                context.Reader.ReadNull();
                return null;
            }

            List<T> retValue = new List<T>();

            context.Reader.ReadStartArray();
            
            while (context.Reader.ReadBsonType() == BsonType.String)
            {
                var id = context.Reader.ReadString();
                retValue.Add(MongoFlatIdSerializerHelper.ToIdentity<T>(id));
            }

            context.Reader.ReadEndArray();
            return retValue.ToArray();
        }

        public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, T[] value)
        {
            if (value == null)
            {
                context.Writer.WriteNull();
            }
            else
            {
                context.Writer.WriteStartArray();
                foreach (var item in value)
                {
                    context.Writer.WriteString(item.AsString());
                }
                context.Writer.WriteEndArray();
            }
        }
    }
}
<commit_msg>Little refactoring of MongoFlat initialization<commit_after>﻿using System;
using Jarvis.NEventStoreEx.CommonDomainEx;
using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers;
using System.Collections.Generic;

namespace Jarvis.Framework.Shared.IdentitySupport.Serialization
{
    public static class MongoFlatIdSerializerHelper
    {
        internal static IIdentityConverter IdentityConverter { get; set; }

        public static void Initialize(IIdentityConverter converter)
        {
            MongoFlatIdSerializerHelper.IdentityConverter = converter;
        }

        public static T ToIdentity<T>(String id)
        {
            if (MongoFlatIdSerializerHelper.IdentityConverter == null)
                throw new Exception("Identity converter not set in MongoFlatIdSerializerHelper.IdentityConverter property");

            return (T)MongoFlatIdSerializerHelper.IdentityConverter.ToIdentity(id);
        }
    }

    public class GenericIdentityBsonSerializer : SerializerBase<IIdentity>
    {
        public override IIdentity Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            if (context.Reader.CurrentBsonType == BsonType.Null)
            {
                context.Reader.ReadNull();
                return null;
            }

            var id = context.Reader.ReadString();

            return MongoFlatIdSerializerHelper.ToIdentity<EventStoreIdentity>(id);
        }

        public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, IIdentity value)
        {
            if (value == null)
            {
                context.Writer.WriteNull();
            }
            else
            {
                context.Writer.WriteString(value.AsString());
            }
        }
    }

    public class TypedEventStoreIdentityBsonSerializer<T> : SerializerBase<T> where T : EventStoreIdentity
    {

        public override T Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            if (context.Reader.CurrentBsonType == BsonType.Null)
            {
                context.Reader.ReadNull();
                return null;
            }

            var id = context.Reader.ReadString();

            return MongoFlatIdSerializerHelper.ToIdentity<T>(id);
        }

        public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, T value)
        {
            if (value == null)
            {
                context.Writer.WriteNull();
            }
            else
            {
                context.Writer.WriteString(value);
            }
        }
    }

    public class IdentityArrayBsonSerializer<T> : SerializerBase<T[]> where T : IIdentity
    {
        public override T[] Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            if (context.Reader.CurrentBsonType == BsonType.Null)
            {
                context.Reader.ReadNull();
                return null;
            }

            List<T> retValue = new List<T>();

            context.Reader.ReadStartArray();
            
            while (context.Reader.ReadBsonType() == BsonType.String)
            {
                var id = context.Reader.ReadString();
                retValue.Add(MongoFlatIdSerializerHelper.ToIdentity<T>(id));
            }

            context.Reader.ReadEndArray();
            return retValue.ToArray();
        }

        public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, T[] value)
        {
            if (value == null)
            {
                context.Writer.WriteNull();
            }
            else
            {
                context.Writer.WriteStartArray();
                foreach (var item in value)
                {
                    context.Writer.WriteString(item.AsString());
                }
                context.Writer.WriteEndArray();
            }
        }
    }
}

{'fraction_non_alphanumeric': 0.07676297747306562, 'fraction_numerical': 0.0, 'mean_word_length': 1.6801181102362204, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '1120833', 'n_tokens_mistral': 2044, 'n_tokens_neox': 1933, 'n_words': 369}