mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-26 14:38:34 +01:00
make json writing deal with defaults easily
This commit is contained in:
parent
1d56cbd8eb
commit
9781722ae4
1 changed files with 5 additions and 0 deletions
|
@ -1,4 +1,5 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.IO.Compression;
|
using System.IO.Compression;
|
||||||
using Lidgren.Network;
|
using Lidgren.Network;
|
||||||
|
@ -36,6 +37,8 @@ namespace MLEM.Data {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void WriteObject<T>(this NetBuffer buffer, T obj, JsonSerializer serializer) {
|
public static void WriteObject<T>(this NetBuffer buffer, T obj, JsonSerializer serializer) {
|
||||||
|
if (EqualityComparer<T>.Default.Equals(obj, default))
|
||||||
|
buffer.Write(0);
|
||||||
using (var memory = new MemoryStream()) {
|
using (var memory = new MemoryStream()) {
|
||||||
using (var gzip = new DeflateStream(memory, CompressionLevel.Fastest, true))
|
using (var gzip = new DeflateStream(memory, CompressionLevel.Fastest, true))
|
||||||
serializer.Serialize(new BsonDataWriter(gzip), obj, typeof(T));
|
serializer.Serialize(new BsonDataWriter(gzip), obj, typeof(T));
|
||||||
|
@ -47,6 +50,8 @@ namespace MLEM.Data {
|
||||||
|
|
||||||
public static T ReadObject<T>(this NetBuffer buffer, JsonSerializer serializer) {
|
public static T ReadObject<T>(this NetBuffer buffer, JsonSerializer serializer) {
|
||||||
var length = buffer.ReadInt32();
|
var length = buffer.ReadInt32();
|
||||||
|
if (length <= 0)
|
||||||
|
return default;
|
||||||
var arr = buffer.ReadBytes(length);
|
var arr = buffer.ReadBytes(length);
|
||||||
using (var memory = new MemoryStream(arr)) {
|
using (var memory = new MemoryStream(arr)) {
|
||||||
using (var gzip = new DeflateStream(memory, CompressionMode.Decompress, true))
|
using (var gzip = new DeflateStream(memory, CompressionMode.Decompress, true))
|
||||||
|
|
Loading…
Reference in a new issue