graph TD
A[序列化协议] --> B[二进制协议]
A --> C[文本协议]
B --> B1[Protobuf]
B --> B2[Avro]
B --> B3[MessagePack]
B --> B4[Thrift]
C --> C1[JSON]
C --> C2[XML]
C --> C3[YAML]
三、Protobuf深入解析
3.1 Protobuf定义
public class ProtobufDefinitionService
{
public string GenerateProtoFile(string messageName, List fields)
{
var sb = new StringBuilder();
sb.AppendLine("syntax = \"proto3\";");
sb.AppendLine();
sb.AppendLine($"message {messageName} {{");
foreach (var field in fields)
{
sb.AppendLine($" {field.Type} {field.Name} = {field.Number};");
}
sb.AppendLine("}");
return sb.ToString();
}
public void GenerateCode(string protoFile, string outputDir)
{
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "protoc",
Arguments = $"--csharp_out={outputDir} {protoFile}",
UseShellExecute = true,
CreateNoWindow = true
}
};
process.Start();
process.WaitForExit();
}
}
public class ProtoField
{
public int Number { get; set; }
public string Name { get; set; }
public string Type { get; set; }
}
3.2 Protobuf序列化与反序列化
public class ProtobufSerializer
{
public byte[] Serialize(T obj) where T : IMessage, new()
{
using var stream = new MemoryStream();
obj.WriteTo(stream);
return stream.ToArray();
}
public T Deserialize(byte[] data) where T : IMessage, new()
{
var obj = new T();
obj.MergeFrom(data);
return obj;
}
public async Task SerializeAsync(T obj) where T : IMessage, new()
{
using var stream = new MemoryStream();
await obj.WriteToAsync(stream);
return stream.ToArray();
}
public async Task DeserializeAsync(byte[] data) where T : IMessage, new()
{
var obj = new T();
await obj.MergeFromAsync(data);
return obj;
}
public void SerializeToStream(T obj, Stream stream) where T : IMessage, new()
{
obj.WriteTo(stream);
}
public T DeserializeFromStream(Stream stream) where T : IMessage, new()
{
var obj = new T();
obj.MergeFrom(stream);
return obj;
}
}
3.3 Protobuf性能优化
public class ProtobufOptimizer
{
public void ConfigureSerializer(SerializerOptions options)
{
options.WriteRawMessage = true;
options.IgnoreUnknownFields = true;
}
public byte[] SerializeWithCompression(T obj) where T : IMessage, new()
{
var serialized = Serialize(obj);
using var compressedStream = new MemoryStream();
using var gzipStream = new GZipStream(compressedStream, CompressionLevel.Optimal);
gzipStream.Write(serialized, 0, serialized.Length);
gzipStream.Flush();
return compressedStream.ToArray();
}
public T DeserializeWithDecompression(byte[] compressedData) where T : IMessage, new()
{
using var compressedStream = new MemoryStream(compressedData);
using var gzipStream = new GZipStream(compressedStream, CompressionMode.Decompress);
using var decompressedStream = new MemoryStream();
gzipStream.CopyTo(decompressedStream);
return Deserialize(decompressedStream.ToArray());
}
}
四、Avro深入解析
4.1 Avro Schema定义
public class AvroSchemaService
{
public string GenerateSchema(string recordName, List fields)
{
var schema = new
{
type = "record",
name = recordName,
fields = fields.Select(f => new
{
name = f.Name,
type = f.Type,
default = f.Default
}).ToList()
};
return JsonSerializer.Serialize(schema);
}
public async Task ValidateSchemaAsync(string schema)
{
var parser = new Schema.Parser();
var avroSchema = parser.Parse(schema);
if (avroSchema == null)
{
throw new InvalidOperationException("Invalid Avro schema");
}
}
}
public class AvroField
{
public string Name { get; set; }
public string Type { get; set; }
public object Default { get; set; }
}
4.2 Avro序列化与反序列化
public class AvroSerializer
{
public byte[] Serialize(object obj, string schema)
{
var parser = new Schema.Parser();
var avroSchema = parser.Parse(schema);
using var stream = new MemoryStream();
using var writer = new BinaryWriter(stream);
var encoder = new BinaryEncoder(writer);
var datumWriter = new GenericDatumWriter
五、MessagePack深入解析
5.1 MessagePack序列化与反序列化
public class MessagePackSerializer
{
public byte[] Serialize(object obj)
{
return MessagePackSerializer.Serialize(obj);
}
public T Deserialize(byte[] data)
{
return MessagePackSerializer.Deserialize(data);
}
public async Task SerializeAsync(object obj)
{
return await MessagePackSerializer.SerializeAsync(obj);
}
public async Task DeserializeAsync(byte[] data)
{
return await MessagePackSerializer.DeserializeAsync(data);
}
public void SerializeToStream(object obj, Stream stream)
{
MessagePackSerializer.Serialize(stream, obj);
}
public T DeserializeFromStream(Stream stream)
{
return MessagePackSerializer.Deserialize(stream);
}
public byte[] SerializeWithCompression(object obj)
{
var options = MessagePackSerializerOptions.Standard.WithCompression(MessagePackCompression.Lz4BlockArray);
return MessagePackSerializer.Serialize(obj, options);
}
}
5.2 MessagePack配置
public class MessagePackConfigurationService
{
public MessagePackSerializerOptions ConfigureOptions()
{
return MessagePackSerializerOptions.Standard
.WithResolver(ContractlessStandardResolver.Instance)
.WithCompression(MessagePackCompression.None)
.WithSecurity(MessagePackSecurity.UntrustedData);
}
public MessagePackSerializerOptions ConfigureOptionsWithCompression()
{
return MessagePackSerializerOptions.Standard
.WithResolver(ContractlessStandardResolver.Instance)
.WithCompression(MessagePackCompression.Lz4BlockArray)
.WithSecurity(MessagePackSecurity.UntrustedData);
}
}
六、JSON序列化优化
6.1 JSON序列化对比
JSON库
性能
特性
适用场景
System.Text.Json
高
内置、高性能
.NET Core
Newtonsoft.Json
中等
功能丰富
兼容性需求
Utf8Json
极高
极致性能
高性能场景
6.2 JSON序列化优化
public class JsonSerializerOptimizer
{
private readonly JsonSerializerOptions _options;
public JsonSerializerOptimizer()
{
_options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
PropertyNameCaseInsensitive = true,
WriteIndented = false,
IgnoreNullValues = true
};
}
public string Serialize(object obj)
{
return JsonSerializer.Serialize(obj, _options);
}
public T Deserialize(string json)
{
return JsonSerializer.Deserialize(json, _options);
}
public byte[] SerializeToUtf8Bytes(object obj)
{
return JsonSerializer.SerializeToUtf8Bytes(obj, _options);
}
public T DeserializeFromUtf8Bytes(byte[] utf8Json)
{
return JsonSerializer.Deserialize(utf8Json, _options);
}
public async Task SerializeAsync(object obj)
{
using var stream = new MemoryStream();
await JsonSerializer.SerializeAsync(stream, obj, _options);
return Encoding.UTF8.GetString(stream.ToArray());
}
}
七、序列化协议选型策略
7.1 选型决策树
graph TD
A[选择序列化协议] --> B{性能需求?}
B -->|极致性能| C[Protobuf/MessagePack]
B -->|一般性能| D[JSON]
C --> C1{跨语言?}
C1 -->|是| C2[Protobuf]
C1 -->|否| C3[MessagePack]
D --> D1{大数据?}
D1 -->|是| D2[Avro]
D1 -->|否| D3[JSON]
7.2 选型考虑因素
考虑因素
说明
推荐协议
性能
极致性能需求
Protobuf/MessagePack
跨语言
多语言系统
Protobuf
大数据
流式处理
Avro
可读性
调试需求
JSON
API
REST API
JSON
八、数据交换格式
8.1 数据交换格式
public class DataExchangeService
{
public async Task ConvertAsync(string data, string sourceFormat, string targetFormat)
{
return (sourceFormat, targetFormat) switch
{
("json", "protobuf") => await ConvertJsonToProtobufAsync(data),
("protobuf", "json") => await ConvertProtobufToJsonAsync(data),
("json", "avro") => await ConvertJsonToAvroAsync(data),
("avro", "json") => await ConvertAvroToJsonAsync(data),
("json", "messagepack") => await ConvertJsonToMessagePackAsync(data),
("messagepack", "json") => await ConvertMessagePackToJsonAsync(data),
_ => throw new ArgumentException("Unsupported format conversion")
};
}
private async Task ConvertJsonToProtobufAsync(string json)
{
var obj = JsonSerializer.Deserialize(json);
return Convert.ToBase64String(_protobufSerializer.Serialize(obj));
}
private async Task ConvertProtobufToJsonAsync(string protobufBase64)
{
var data = Convert.FromBase64String(protobufBase64);
var obj = _protobufSerializer.Deserialize(data);
return JsonSerializer.Serialize(obj);
}
}
8.2 数据交换协议
public class DataExchangeProtocol
{
public async Task ExchangeAsync(ExchangeRequest request)
{
var convertedData = await _dataExchangeService.ConvertAsync(
request.Data,
request.SourceFormat,
request.TargetFormat);
return new ExchangeResult
{
Data = convertedData,
Format = request.TargetFormat,
OriginalSize = request.Data.Length,
ConvertedSize = convertedData.Length
};
}
public async Task ValidateAsync(string data, string format)
{
try
{
switch (format)
{
case "json":
JsonSerializer.Deserialize(data);
return new ValidationResult { Valid = true };
case "protobuf":
var protobufData = Convert.FromBase64String(data);
_protobufSerializer.Deserialize(protobufData);
return new ValidationResult { Valid = true };
default:
return new ValidationResult { Valid = false, Message = "Unknown format" };
}
}
catch (Exception ex)
{
return new ValidationResult { Valid = false, Message = ex.Message };
}
}
}
public class ExchangeRequest
{
public string Data { get; set; }
public string SourceFormat { get; set; }
public string TargetFormat { get; set; }
}
public class ExchangeResult
{
public string Data { get; set; }
public string Format { get; set; }
public int OriginalSize { get; set; }
public int ConvertedSize { get; set; }
}
九、序列化最佳实践
9.1 选择合适的序列化协议
高性能场景选择Protobuf或MessagePack
大数据场景选择Avro
API接口选择JSON
多语言系统选择Protobuf
9.2 序列化优化技巧
public class SerializationBestPractices
{
public byte[] SerializeEfficiently(object obj, SerializationFormat format)
{
return format switch
{
SerializationFormat.Protobuf => _protobufSerializer.Serialize(obj),
SerializationFormat.MessagePack => _messagePackSerializer.Serialize(obj),
SerializationFormat.Json => Encoding.UTF8.GetBytes(_jsonSerializer.Serialize(obj)),
_ => throw new ArgumentException("Unknown format")
};
}
public void AvoidUnnecessarySerialization(object obj)
{
if (obj is string)
{
return;
}
}
public void UseCompressionForLargeData(object obj)
{
var serialized = _protobufSerializer.Serialize(obj);
if (serialized.Length > 1024 * 1024)
{
var compressed = Compress(serialized);
}
}
}
public enum SerializationFormat { Protobuf, MessagePack, Json, Avro }
9.3 版本兼容性
public class VersionCompatibilityService
{
public void HandleVersionMismatch(object data, int sourceVersion, int targetVersion)
{
if (sourceVersion < targetVersion)
{
data = UpgradeData(data, sourceVersion, targetVersion);
}
else if (sourceVersion > targetVersion)
{
data = DowngradeData(data, sourceVersion, targetVersion);
}
}
private object UpgradeData(object data, int fromVersion, int toVersion)
{
for (int i = fromVersion; i < toVersion; i++)
{
data = ApplyUpgrade(data, i);
}
return data;
}
private object DowngradeData(object data, int fromVersion, int toVersion)
{
for (int i = fromVersion; i > toVersion; i--)
{
data = ApplyDowngrade(data, i);
}
return data;
}
}