From 9781722ae4f507c7318830b4ef2f8e3a0003063e Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Fri, 20 Dec 2019 23:32:40 +0100 Subject: [PATCH] make json writing deal with defaults easily --- MLEM.Data/NetExtensions.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/MLEM.Data/NetExtensions.cs b/MLEM.Data/NetExtensions.cs index 436a3c9..0c56562 100644 --- a/MLEM.Data/NetExtensions.cs +++ b/MLEM.Data/NetExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.IO; using System.IO.Compression; using Lidgren.Network; @@ -36,6 +37,8 @@ namespace MLEM.Data { } public static void WriteObject(this NetBuffer buffer, T obj, JsonSerializer serializer) { + if (EqualityComparer.Default.Equals(obj, default)) + buffer.Write(0); using (var memory = new MemoryStream()) { using (var gzip = new DeflateStream(memory, CompressionLevel.Fastest, true)) serializer.Serialize(new BsonDataWriter(gzip), obj, typeof(T)); @@ -47,6 +50,8 @@ namespace MLEM.Data { public static T ReadObject(this NetBuffer buffer, JsonSerializer serializer) { var length = buffer.ReadInt32(); + if (length <= 0) + return default; var arr = buffer.ReadBytes(length); using (var memory = new MemoryStream(arr)) { using (var gzip = new DeflateStream(memory, CompressionMode.Decompress, true))