<commit_before>﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;

namespace Quobject.SocketIoClientDotNet.Parser
{
    public class Binary
    {
        private static readonly string KEY_PLACEHOLDER = "_placeholder";

        private static readonly string KEY_NUM = "num";

        public static DeconstructedPacket DeconstructPacket(Packet packet)
        {
            var buffers = new List<byte[]>();

            packet.Data = _deconstructPacket(packet.Data, buffers);
            packet.Attachments = buffers.Count;

            var result = new DeconstructedPacket();
            result.Packet = packet;
            result.Buffers = buffers.ToArray();
            return result;
        }

        private static JToken _deconstructPacket(object data, List<byte[]> buffers)
        {
            if (data == null) return null;

            if (data is byte[])
            {
                var byteArray = (byte[]) data;
                return AddPlaceholder(buffers, byteArray);
            }
            if (data is JArray)
            {
                var newData = new JArray();
                var _data = (JArray) data;
                int len = _data.Count;
                for (int i = 0; i < len; i ++)
                {
                    try
                    {
                        newData.Add( _deconstructPacket(_data[i], buffers));                        
                    }
                    catch (Exception)
                    {
                        return null;
                    }
                }
                return newData;
            }
            if (!(data is JToken))
            {
                throw new NotImplementedException();
            }
            var jtoken = (JToken) data;
            if (jtoken.Type == JTokenType.String)
            {
                return jtoken.Value<string>();
            }
            else if (jtoken.Type == JTokenType.Bytes)
            {
                var byteArray = jtoken.Value<byte[]>();
                return AddPlaceholder(buffers, byteArray);
            }
            else if (jtoken.Type == JTokenType.Object)
            {

                var newData2 = new JObject();
                var _data2 = (JObject)jtoken;

                foreach (var property in _data2.Properties())
                {
                    try
                    {
                        newData2[property.Name] = _deconstructPacket(property.Value, buffers);
                    }
                    catch (Exception)
                    {
                        return null;
                    }

                }
                return newData2;
            }
            throw new NotImplementedException();
        }

        private static JToken AddPlaceholder(List<byte[]> buffers, byte[] byteArray)
        {
            var placeholder = new JObject();
            try
            {
                placeholder.Add(KEY_PLACEHOLDER, true);
                placeholder.Add(KEY_NUM, buffers.Count);
            }
            catch (Exception)
            {
                return null;
            }
            buffers.Add(byteArray);
            return placeholder;
        }

        public static Packet ReconstructPacket(Packet packet, byte[][] buffers)<commit_msg>Fixed non-String and non-Bytes values not sent in JSON with binary data (#112)<commit_after>﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;

namespace Quobject.SocketIoClientDotNet.Parser
{
    public class Binary
    {
        private static readonly string KEY_PLACEHOLDER = "_placeholder";

        private static readonly string KEY_NUM = "num";

        public static DeconstructedPacket DeconstructPacket(Packet packet)
        {
            var buffers = new List<byte[]>();

            packet.Data = _deconstructPacket(packet.Data, buffers);
            packet.Attachments = buffers.Count;

            var result = new DeconstructedPacket();
            result.Packet = packet;
            result.Buffers = buffers.ToArray();
            return result;
        }

        private static JToken _deconstructPacket(object data, List<byte[]> buffers)
        {
            if (data == null)
            {
                return null;
            }

            if (data is byte[])
            {
                var byteArray = (byte[])data;
                return AddPlaceholder(buffers, byteArray);
            }

            var jToken = data as JToken;
            if (jToken == null)
            {
                throw new NotImplementedException();
            }

            switch (jToken.Type)
            {
                case JTokenType.Object:
                    var newJObject = new JObject();
                    var jObject = (JObject)jToken;
                    foreach (var property in jObject.Properties())
                    {
                        try
                        {
                            newJObject[property.Name] = _deconstructPacket(property.Value, buffers);
                        }
                        catch (Exception)
                        {
                            return null;
                        }
                    }
                    return newJObject;

                case JTokenType.Array:
                    var newJArray = new JArray();
                    var jArray = (JArray)jToken;
                    for (int i = 0; i < jArray.Count; i++)
                    {
                        try
                        {
                            newJArray.Add(_deconstructPacket(jArray[i], buffers));
                        }
                        catch (Exception)
                        {
                            return null;
                        }
                    }
                    return newJArray;

                case JTokenType.Bytes:
                    var byteArray = jToken.Value<byte[]>();
                    return AddPlaceholder(buffers, byteArray);

                case JTokenType.None:
                case JTokenType.Constructor:
                case JTokenType.Property:
                case JTokenType.Comment:
                    throw new NotImplementedException();

                default:
                    return jToken;
            }
        }

        private static JToken AddPlaceholder(List<byte[]> buffers, byte[] byteArray)
        {
            var placeholder = new JObject();
            try
            {
                placeholder.Add(KEY_PLACEHOLDER, true);
                placeholder.Add(KEY_NUM, buffers.Count);
            }
            catch (Exception)
            {
                return null;
            }
            buffers.Add(byteArray);
            return placeholder;
        }

        public static Packet ReconstructPacket(Packet packet, byte[][] buffers)
{'fraction_non_alphanumeric': 0.07685576782849643, 'fraction_numerical': 0.001458363715910748, 'mean_word_length': 1.105618667485416, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 12, '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': '3512854', 'n_tokens_mistral': 1627, 'n_tokens_neox': 1512, 'n_words': 381}