Serialized data larger than expected
I am serializing a class that has a lot of nested dictionaries and lists. When serializing using ZeroFormatter the resulting byte[] is a lot larger than I expected, can someone help me to understand why and if it's possible to reduce to size. At the moment it means using ZeroFormatter is impossible as the extra time it takes to send more data over the network is higher than the extra time taken by JSON.NET
As a comparison I serialized the same class to JSON using JSON.NET and the resulting json is much smaller
JSON.NET 14281 bytes
ZeroFormatter 553992 bytes
Here is the slightly sanitized json to give an idea of the structure of the data
{
"$type": "Aggregates.EnvironmentAggregate",
"Version": 19,
"Etag": "17",
"Id": "env0",
"Name": null,
"Description": null,
"ManagementHostUri": null,
"TelemetryHostUri": null,
"GatewayHostUri": null,
"SymetricKey": null,
"State": 1,
"Regions": {
"$type": "System.Collections.Generic.Dictionary`2[[System.String, System.Private.CoreLib],[Interfaces.IRegion]], System.Private.CoreLib",
"az-eu-amsterdam": {
"$type": "Models.Region",
"Provider": null,
"Location": null,
"Name": "az-eu-amsterdam",
"Services": {
"$type": "System.Collections.Generic.Dictionary`2[[System.String, System.Private.CoreLib],[Interfaces.IService]], System.Private.CoreLib",
"service5": {
"$type": "Models.Service",
"Instances": {
"$type": "System.Collections.Generic.Dictionary`2[[System.String, System.Private.CoreLib],[Interfaces.IServiceInstance]], System.Private.CoreLib",
"invszfsbkquzp84lgxgb": {
"$type": "Models.ServiceInstance",
"InstanceId": "inVszFSbkquZP84LgxGB",
"State": 1,
"HealthMessage": null,
"Role": null,
"PublicAddress": null,
"PublicIpAddress": null,
"PrivateIpAddress": null,
"Key": 1
}
},
"Name": "service5",
"State": 0,
"Scale": 0,
"Tags": null,
"Metadata": null,
"Key": 1
},
"service3": {
"$type": "Models.Service",
"Instances": {
"$type": "System.Collections.Generic.Dictionary`2[[System.String, System.Private.CoreLib],[Interfaces.IServiceInstance]], System.Private.CoreLib",
"service3-service4-to-service4-general-6f85f66585-4kmd7": {
"$type": "Models.ServiceInstance",
"InstanceId": "service3-service4-to-service4-general-6f85f66585-4kmd7",
"State": 1,
"HealthMessage": "",
"Role": "service4 -> service4 (General)",
"PublicAddress": null,
"PublicIpAddress": null,
"PrivateIpAddress": null,
"Key": 1
},
"service3-service1-shovel-56665bb84b-9lktd": {
"$type": "Models.ServiceInstance",
"InstanceId": "service3-service1-shovel-56665bb84b-9lktd",
"State": 1,
"HealthMessage": "",
"Role": "service1 Shovel",
"PublicAddress": null,
"PublicIpAddress": null,
"PrivateIpAddress": null,
"Key": 1
},
"service3-service4-management-to-service4-6978bff6d7-wlrst": {
"$type": "Models.ServiceInstance",
"Key": 1,
"InstanceId": "service3-service4-management-to-service4-6978bff6d7-wlrst",
"State": 1,
"HealthMessage": "",
"Role": "service4 Monitoring -> service4",
"PublicAddress": null,
"PublicIpAddress": null,
"PrivateIpAddress": null
}
},
"Name": "service3",
"State": 0,
"Scale": 0,
"Tags": null,
"Metadata": null,
"Key": 1
},
"service4": {
"$type": "Models.Service",
"Instances": {
"$type": "System.Collections.Generic.Dictionary`2[[System.String, System.Private.CoreLib],[Interfaces.IServiceInstance]], System.Private.CoreLib",
"service4-0": {
"$type": "Models.ServiceInstance",
"InstanceId": "service4-0",
"State": 1,
"HealthMessage": "Unknown",
"Role": null,
"PublicAddress": null,
"PublicIpAddress": null,
"PrivateIpAddress": null,
"Key": 1
}
},
"Name": "service4",
"State": 0,
"Scale": 0,
"Tags": null,
"Metadata": null,
"Key": 1
},
"service1": {
"$type": "Models.Service",
"Instances": {
"$type": "System.Collections.Generic.Dictionary`2[[System.String, System.Private.CoreLib],[Interfaces.IServiceInstance]], System.Private.CoreLib",
"service1-2": {
"$type": "Models.ServiceInstance",
"InstanceId": "service1-2",
"State": 1,
"HealthMessage": "Unknown",
"Role": null,
"PublicAddress": null,
"PublicIpAddress": null,
"PrivateIpAddress": null,
"Key": 1
},
"service1-1": {
"$type": "Models.ServiceInstance",
"InstanceId": "service1-1",
"State": 1,
"HealthMessage": "Unknown",
"Role": null,
"PublicAddress": null,
"PublicIpAddress": null,
"PrivateIpAddress": null,
"Key": 1
}
},
"Name": "service1",
"State": 0,
"Scale": 0,
"Tags": null,
"Metadata": null,
"Key": 1
}
},
"Applications": null,
"State": 0,
"Tags": null,
"Metadata": null,
"PingServiceUri": null,
"Key": 0
}
},
"Services": {
"$type": "System.Collections.Generic.Dictionary`2[[System.String, System.Private.CoreLib],[Interfaces.IService]], System.Private.CoreLib",
"service3": {
"$type": "Models.Service",
"Instances": {
"$type": "System.Collections.Generic.Dictionary`2[[System.String, System.Private.CoreLib],[Interfaces.IServiceInstance]], System.Private.CoreLib",
"service3-service4-to-datadog-6bcf764cb7-zqbmd": {
"$type": "Models.ServiceInstance",
"InstanceId": "service3-service4-to-datadog-6bcf764cb7-zqbmd",
"State": 1,
"HealthMessage": "",
"Role": "service4 -> DataDog",
"PublicAddress": null,
"PublicIpAddress": null,
"PrivateIpAddress": null,
"Key": 1
},
"service3-service4-to-csv-64bcf94476-fc7ml": {
"$type": "Models.ServiceInstance",
"InstanceId": "service3-service4-to-csv-64bcf94476-fc7ml",
"State": 1,
"HealthMessage": "",
"Role": "service4 -> CSV",
"PublicAddress": null,
"PublicIpAddress": null,
"PrivateIpAddress": null,
"Key": 1
},
"service3-service4-to-service4-general-7c5bb845f8-sdf9d": {
"$type": "Models.ServiceInstance",
"InstanceId": "service3-service4-to-service4-general-7c5bb845f8-sdf9d",
"State": 1,
"HealthMessage": "",
"Role": "service4 -> service4 (General)",
"PublicAddress": null,
"PublicIpAddress": null,
"PrivateIpAddress": null,
"Key": 1
},
"service3-service1-to-service4-b8d9ddcd4-z94tq": {
"$type": "Models.ServiceInstance",
"InstanceId": "service3-service1-to-service4-b8d9ddcd4-z94tq",
"State": 1,
"HealthMessage": "",
"Role": "service1 -> service4",
"PublicAddress": null,
"PublicIpAddress": null,
"PrivateIpAddress": null,
"Key": 1
}
},
"Name": "service3",
"State": 0,
"Scale": 0,
"Tags": null,
"Metadata": null,
"Key": 1
},
"service2": {
"$type": "Models.Service",
"Instances": {
"$type": "System.Collections.Generic.Dictionary`2[[System.String, System.Private.CoreLib],[Interfaces.IServiceInstance]], System.Private.CoreLib",
"dc5110f1-e4e0-4cda-b847-87c343833c5f": {
"$type": "Models.ServiceInstance",
"InstanceId": "dc5110f1-e4e0-4cda-b847-87c343833c5f",
"State": 1,
"HealthMessage": null,
"Role": null,
"PublicAddress": null,
"PublicIpAddress": null,
"PrivateIpAddress": null,
"Key": 1
}
},
"Name": "service2",
"State": 0,
"Scale": 0,
"Tags": null,
"Metadata": null,
"Key": 1
},
"service1": {
"$type": "Models.Service",
"Instances": {
"$type": "System.Collections.Generic.Dictionary`2[[System.String, System.Private.CoreLib],[Interfaces.IServiceInstance]], System.Private.CoreLib",
"service1-0": {
"$type": "Models.ServiceInstance",
"InstanceId": "service1-0",
"State": 1,
"HealthMessage": "Unknown",
"Role": "central",
"PublicAddress": null,
"PublicIpAddress": null,
"PrivateIpAddress": null,
"Key": 1
},
"service1-1": {
"$type": "Models.ServiceInstance",
"InstanceId": "service1-1",
"State": 1,
"HealthMessage": "Unknown",
"Role": "central",
"PublicAddress": null,
"PublicIpAddress": null,
"PrivateIpAddress": null,
"Key": 1
},
"service1-2": {
"$type": "Models.ServiceInstance",
"InstanceId": "service1-2",
"State": 1,
"HealthMessage": "Unknown",
"Role": "central",
"PublicAddress": null,
"PublicIpAddress": null,
"PrivateIpAddress": null,
"Key": 1
}
},
"Name": "service1",
"State": 0,
"Scale": 0,
"Tags": null,
"Metadata": null,
"Key": 1
}
},
"Tags": null,
"Applications": null
}
I have the exact same issue on my server, I even serialize all of the fields separately and the total comes out to be what is expected, but the overall serialize balloons to 10x > greater than the size I am expecting.