<commit_before>﻿using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;

namespace Glimpse
{
    public class DefaultMessageConverter : IMessageConverter
    {
        private readonly JsonSerializer _jsonSerializer;
        private readonly IContextData<MessageContext> _contextData;

        public DefaultMessageConverter(JsonSerializer jsonSerializer, IContextData<MessageContext> contextData)
        {
            jsonSerializer.ContractResolver = new CamelCasePropertyNamesContractResolver();

            _jsonSerializer = jsonSerializer;
            _contextData = contextData;
        }

        public IMessage ConvertMessage(object payload)
        { 
            var message = new Message();
            message.Id = Guid.NewGuid();
            message.Type = payload.GetType().FullName;
            message.Payload = _jsonSerializer.Serialize(payload);
            message.Context = _contextData.Value;

            ProcessIndices(payload, message);
            ProcessTags(payload, message);

            return message;
        } 

        private void ProcessIndices(object payload, Message message)
        {
            var indicesPayload = payload as IMessageIndices;
            if (indicesPayload?.Indices != null)
            {
                message.Indices = indicesPayload.Indices;
                // TODO: longer term these props need to be not seralized at all
                indicesPayload.Indices = null;  
            } 
        }

        private void ProcessTags(object payload, Message message)
        {<commit_msg>Nulling out the props needs to happen pre serialization<commit_after>﻿using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;

namespace Glimpse
{
    public class DefaultMessageConverter : IMessageConverter
    {
        private readonly JsonSerializer _jsonSerializer;
        private readonly IContextData<MessageContext> _contextData;

        public DefaultMessageConverter(JsonSerializer jsonSerializer, IContextData<MessageContext> contextData)
        {
            jsonSerializer.ContractResolver = new CamelCasePropertyNamesContractResolver();

            _jsonSerializer = jsonSerializer;
            _contextData = contextData;
        }

        public IMessage ConvertMessage(object payload)
        { 
            var message = new Message();

            ProcessIndices(payload, message);
            ProcessTags(payload, message);

            message.Id = Guid.NewGuid();
            message.Type = payload.GetType().FullName;
            message.Payload = _jsonSerializer.Serialize(payload);
            message.Context = _contextData.Value;

            return message;
        } 

        private void ProcessIndices(object payload, Message message)
        {
            var indicesPayload = payload as IMessageIndices;
            if (indicesPayload?.Indices != null)
            {
                message.Indices = indicesPayload.Indices;
                // TODO: longer term these props need to be not seralized at all
                indicesPayload.Indices = null;  
            } 
        }

        private void ProcessTags(object payload, Message message)
        {
{'fraction_non_alphanumeric': 0.0640904806786051, 'fraction_numerical': 0.0, 'mean_word_length': 2.2423625254582484, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '4664819', 'n_tokens_mistral': 720, 'n_tokens_neox': 710, 'n_words': 188}