Protobuf序列化对象到json

时间:2020-02-26 20:21:14

标签: c# protobuf-net

我有一些标准对象,我使用protobuf进行序列化并存储在数据库中。是否可以将对象反序列化为通用标记(例如json或xml),然后返回序列化的对象类型protobuf,而无需使用该类的具体实现?

[ProtoContract]
[Serializable]
internal class SomeDTO
{
    /// <summary>
    /// Gets or sets the Id.
    /// </summary>
    [ProtoMember(1)]
    public string MyKey { get; set; }

    [ProtoMember(2)]
    public string Name { get; set; }

    [ProtoMember(3)]
    public int SomeId { get; set; }

    [ProtoMember(4)]
    public DateTime LastModifiedDate { get; set; }
}

该对象将序列化为:

{
    "1": "asdklfhkajd",
    "2": "Tim",
    "3": 12345,
    "4": "05/29/2015 5:50"
}

或者如果我的描述符出现但不是必需的:

{
    "MyKey": "asdklfhkajd",
    "Name": "Tim",
    "SomeId": 12345,
    "LastModifiedDate": "05/29/2015 5:50"
}

我正在考虑可能使用ProtoReader编写某些东西吗?

2 个答案:

答案 0 :(得分:1)

元数据对象:

public class collectionMetadata
{
    public string Field { get; set; }
    public string FieldType { get; set; }
}

反序列化器:

    public static JObject ProtoReaderDeserilizer(List<CollectionFieldType> collectionMetadata, ProtoReader reader)
    {
        JObject obj = new JObject();

        while (reader.ReadFieldHeader() > 0)
        {
            var field = collectionMetadata[reader.FieldNumber - 1].Field;
            switch (reader.WireType)
            {
                case WireType.Variant:
                    obj[field] = reader.ReadInt64();
                    break;
                case WireType.String:
                    var fieldType = collectionMetadata[reader.FieldNumber - 1].FieldType;

                    switch (fieldType.ToLowerInvariant())
                    {
                        case "date":
                            var tok1 = ProtoReader.StartSubItem(reader);
                            reader.ReadFieldHeader();

                            switch (reader.WireType)
                            {
                                case WireType.Variant:
                                    obj[field] = reader.ReadInt64();
                                    break;
                                default:
                                    reader.ReadFieldHeader();
                                    break;
                            }

                            ProtoReader.EndSubItem(tok1, reader);
                            break;

                        case "datetime":
                            obj[field] = BclHelpers.ReadDateTime(reader);
                            break;

                        case "decimal":
                            obj[field] = BclHelpers.ReadDecimal(reader);
                            break;

                        case "guid":
                            obj[field] = BclHelpers.ReadGuid(reader);
                            break;

                        case "string":
                        default:
                            if (!fieldType.StartsWith("["))
                                obj[field] = reader.ReadString();
                            else
                            {
                                var tok2 = ProtoReader.StartSubItem(reader);
                                obj[field] = ProtoReaderDeserilizer(JsonConvert.DeserializeObject<List<CollectionFieldType>>(fieldType), reader);
                                ProtoReader.EndSubItem(tok2, reader);
                            }
                            break;
                    }

                    break;
                case WireType.Fixed32:
                    obj[field] = reader.ReadSingle();
                    break;
                case WireType.Fixed64:
                    obj[field] = reader.ReadDouble();
                    break;
                case WireType.StartGroup:
                    // one of 2 sub-object formats
                    var tok = ProtoReader.StartSubItem(reader);
                    obj[field] = ProtoReaderDeserilizer(JsonConvert.DeserializeObject<List<CollectionFieldType>>(collectionMetadata[reader.FieldNumber - 1].FieldType), reader);
                    ProtoReader.EndSubItem(tok, reader);
                    break;
                default:
                    reader.SkipField();
                    break;
            }
        }

        return obj;
    }

序列化器:

    public static void ProtoSerializer(ProtoWriter writer, JObject obj, List<CollectionFieldType> collectionMetadata, string name)
    {
        int i = 1;

        if (obj == null)
        {
            throw new FormatException($"Collection {name} has invalid object defined in its field types. Ensure your collection schema and field times schema match.");
        }

        foreach (var field in collectionMetadata)
        {
            string exType = string.Empty;

            if (obj.TryGetValue(field.Field, out var fieldToken))
                exType = SerializeField(writer, name, i, field, exType, fieldToken);

            if (!string.IsNullOrEmpty(exType))
                throw new FormatException($"Collection: {name}, Field: {field.Field} invalid {exType} value: {fieldToken.ToString()}");

            i++;
        }
    }

    private static void SerializeDefaultValue(ProtoWriter writer, int i, CollectionFieldType field)
    {
        switch (field.FieldType.ToLowerInvariant())
        {
            case "bool":
                ProtoWriter.WriteFieldHeader(i, WireType.Variant, writer);
                ProtoWriter.WriteBoolean(default, writer);
                break;
            case "byte":
                ProtoWriter.WriteFieldHeader(i, WireType.Variant, writer);
                ProtoWriter.WriteByte(default, writer);
                break;
            case "sbyte":
                ProtoWriter.WriteFieldHeader(i, WireType.Variant, writer);
                ProtoWriter.WriteSByte(default, writer);
                break;
            case "decimal":
                ProtoWriter.WriteFieldHeader(i, WireType.Fixed64, writer);
                BclHelpers.WriteDecimal(default, writer);
                break;
            case "double":
                ProtoWriter.WriteFieldHeader(i, WireType.Fixed64, writer);
                ProtoWriter.WriteDouble(default, writer);
                break;
            case "float":
                ProtoWriter.WriteFieldHeader(i, WireType.Fixed64, writer);
                ProtoWriter.WriteDouble(default, writer);
                break;
            case "int":
                ProtoWriter.WriteFieldHeader(i, WireType.Variant, writer);
                ProtoWriter.WriteInt32(default, writer);
                break;
            case "enum":
                ProtoWriter.WriteFieldHeader(i, WireType.Variant, writer);
                ProtoWriter.WriteInt32(default, writer);
                break;
            case "long":
                ProtoWriter.WriteFieldHeader(i, WireType.Fixed64, writer);
                ProtoWriter.WriteInt64(default, writer);
                break;
            case "short":
                ProtoWriter.WriteFieldHeader(i, WireType.Fixed32, writer);
                ProtoWriter.WriteInt16(default, writer);
                break;
            case "date":
                ProtoWriter.WriteFieldHeader(i, WireType.String, writer);
                var dateToken = ProtoWriter.StartSubItem(field, writer);

                ProtoWriter.WriteFieldHeader(i, WireType.Variant, writer);
                ProtoWriter.WriteInt32(default, writer);

                ProtoWriter.EndSubItem(dateToken, writer);
                break;
            case "datetime":
                ProtoWriter.WriteFieldHeader(i, WireType.String, writer);
                BclHelpers.WriteDateTime(default, writer);
                break;

            case "guid":
                ProtoWriter.WriteFieldHeader(i, WireType.String, writer);
                BclHelpers.WriteGuid(default, writer);
                break;
            case "char":
            case "string":
            default:
                ProtoWriter.WriteFieldHeader(i, WireType.String, writer);
                ProtoWriter.WriteString(string.Empty, writer);
                break;
        }
    }

    private static string SerializeField(ProtoWriter writer, string name, int i, CollectionFieldType field, string exType, JToken fieldToken)
    {
        switch (field.FieldType.ToLowerInvariant())
        {
            case "bool":
                if (bool.TryParse(fieldToken.ToString(), out bool boolVal))
                {
                    ProtoWriter.WriteFieldHeader(i, WireType.Variant, writer);
                    ProtoWriter.WriteBoolean(boolVal, writer);
                }
                else
                {
                    exType = "bool";
                }
                break;
            case "byte":
                if (byte.TryParse(fieldToken.ToString(), out byte byteVal))
                {
                    ProtoWriter.WriteFieldHeader(i, WireType.Variant, writer);
                    ProtoWriter.WriteByte(byteVal, writer);
                }
                else
                {
                    exType = "byte";
                }
                break;
            case "sbyte":
                if (sbyte.TryParse(fieldToken.ToString(), out sbyte sbyteVal))
                {
                    ProtoWriter.WriteFieldHeader(i, WireType.Variant, writer);
                    ProtoWriter.WriteSByte(sbyteVal, writer);
                }
                else
                {
                    exType = "sbyte";
                }
                break;
            case "decimal":
                if (decimal.TryParse(fieldToken.ToString(), out decimal decimalVal))
                {
                    ProtoWriter.WriteFieldHeader(i, WireType.Fixed64, writer);
                    BclHelpers.WriteDecimal(decimalVal, writer);
                }
                else
                {
                    exType = "decimal";
                }
                break;
            case "double":
                if (double.TryParse(fieldToken.ToString(), out double doubleVal))
                {
                    ProtoWriter.WriteFieldHeader(i, WireType.Fixed64, writer);
                    ProtoWriter.WriteDouble(doubleVal, writer);
                }
                else
                {
                    exType = "double";
                }
                break;
            case "float":
                if (float.TryParse(fieldToken.ToString(), out float floatVal))
                {
                    ProtoWriter.WriteFieldHeader(i, WireType.Fixed64, writer);
                    ProtoWriter.WriteDouble(floatVal, writer);
                }
                else
                {
                    exType = "float";
                }
                break;
            case "enum":
            case "int":
                if (int.TryParse(fieldToken.ToString(), out int intVal))
                {
                    ProtoWriter.WriteFieldHeader(i, WireType.Variant, writer);
                    ProtoWriter.WriteInt32(intVal, writer);
                }
                else
                {
                    exType = "int";
                }
                break;
            case "long":
                if (long.TryParse(fieldToken.ToString(), out long longVal))
                {
                    ProtoWriter.WriteFieldHeader(i, WireType.Fixed64, writer);
                    ProtoWriter.WriteInt64(longVal, writer);
                }
                else
                {
                    exType = "long";
                }
                break;
            case "short":
                if (short.TryParse(fieldToken.ToString(), out short shortVal))
                {
                    ProtoWriter.WriteFieldHeader(i, WireType.Fixed32, writer);
                    ProtoWriter.WriteInt16(shortVal, writer);
                }
                else
                {
                    exType = "short";
                }
                break;
            case "date":
                ProtoWriter.WriteFieldHeader(i, WireType.String, writer);
                var dateToken = ProtoWriter.StartSubItem(fieldToken, writer);

                if (int.TryParse(fieldToken.ToString(), out int dateVal))
                {
                    ProtoWriter.WriteFieldHeader(i, WireType.Variant, writer);
                    ProtoWriter.WriteInt32(dateVal, writer);
                }
                else
                {
                    exType = "date";
                }

                ProtoWriter.EndSubItem(dateToken, writer);
                break;
            case "datetime":
                if (fieldToken.Type == JTokenType.Date)
                {
                    ProtoWriter.WriteFieldHeader(i, WireType.String, writer);
                    BclHelpers.WriteDateTime((DateTime)fieldToken, writer);
                }
                else
                {
                    exType = "DateTime";
                }
                break;

            case "guid":
                if (Guid.TryParse(fieldToken.ToString(), out Guid guidVal))
                {
                    ProtoWriter.WriteFieldHeader(i, WireType.String, writer);
                    BclHelpers.WriteGuid(guidVal, writer);
                }
                else
                {
                    exType = "guid";
                }
                break;
            case "char":
            case "string":
            default:
                if (field.FieldType.StartsWith("["))
                {
                    ProtoWriter.WriteFieldHeader(i, WireType.String, writer);
                    var token = ProtoWriter.StartSubItem(fieldToken, writer);
                    ProtoSerializer(writer, fieldToken as JObject, JsonConvert.DeserializeObject<List<CollectionFieldType>>(field.FieldType), name);
                    ProtoWriter.EndSubItem(token, writer);
                }
                else
                {
                    ProtoWriter.WriteFieldHeader(i, WireType.String, writer);
                    ProtoWriter.WriteString(fieldToken.ToString(), writer);
                }
                break;
        }

        return exType;
    }

答案 1 :(得分:0)

不,基本上。因为protobuf(二进制格式)是模棱两可的,并且没有关于如何解释字段(它是从类型和其他元数据获取的)的附加上下文,所以不可能可靠地反序列化数据。甚至像字符串和整数之类的简单事物也容易与多种类型混淆。另外,在protobuf级别,没有日期/时间作为原始图元的概念。

要了解我的意思:从上面的内容中获取二进制有效负载,然后将其丢到https://protogen.marcgravell.com/decode上-由于存在多种可能性,它必须为您提供大多数类型的多种解释。而且它提供给您的选项集并不详尽。

您可以使用return <OverlayTrigger trigger="hover" placement="auto" delay={{show : 700, hide : 0}} overlay={overlay}> <span><MyFancyButton ... /></span> </OverlayTrigger > 反序列化,然后使用您选择的任何JSON序列化器将其序列化为JSON,但是:您将需要具体的类型。我已经考虑过添加选项以使用.proto模式和阅读器来执行此操作,但这并没有真正改变您的需求-它只是改变了它的 shape


如果您知道布局但没有类型,则有 个选项,但此时创建类型会更容易。