Esempio n. 1
0
        static public JObject ProtoToJson(byte[] protodata)
        {
            Stream      stream  = new MemoryStream(protodata);
            ProtoReader s       = ProtoReader.Create(stream, stream.Length);
            Regex       regex   = new Regex("[\u0000-\u0009\u000e-\u001f]");
            JObject     jObject = new JObject();

            for (; s.Position < protodata.Length;)
            {
                int    fieldNumber = s.ReadFieldHeader();
                string name        = "field_" + fieldNumber;

                switch (s.WireType)
                {
                case WireType.Variant:
                case WireType.Fixed32:
                case WireType.Fixed64:
                case WireType.SignedVariant:
                {
                    string data = "0x" + s.ReadInt64().ToString("X");
                    jObject.AddEx(name, data);
                    break;
                }

                case WireType.String:
                {
                    byte[] b       = s.ReadStringBytes();
                    string readstr = "";
                    if (b != null)
                    {
                        readstr = Encoding.UTF8.GetString(b);
                    }

                    if (b != null && regex.IsMatch(readstr))
                    {
                        try
                        {
                            jObject.AddEx(name, ProtoToJson(b));
                        }
                        catch
                        {
                            jObject.AddEx(name, readstr);
                        }
                    }
                    else
                    {
                        jObject.AddEx(name, readstr);
                    }
                    break;
                }

                case WireType.StartGroup:
                //@string.Append('\t', depth + 1); @string.Append("Start Group\n"); break;
                case WireType.EndGroup:
                //@string.Append('\t', depth + 1); @string.Append("End Group\n"); break;
                default: break;
                }
            }
            return(jObject);
        }