internal ReadOnlyMemory <byte> Serialize() { byte[] buffer = CreateSerializationBuffer(); var writer = new FixedSizedBufferWriter(buffer); var json = new Utf8JsonWriter(writer); json.WriteStartObject(); WriteProperties(ref json); json.WriteEndObject(); return(buffer.AsMemory(0, (int)json.BytesWritten)); }
public bool TrySerializeConfigurationSetting(ConfigurationSetting setting, byte[] buffer, out int written) { try { var writer = new FixedSizedBufferWriter(buffer); Utf8JsonWriter json = new Utf8JsonWriter(writer); SerializeSetting(ref json, setting); json.Flush(); written = (int)json.BytesWritten; return(true); } catch (ArgumentException) { written = 0; return(false); } }
public static byte[] Serialize <T>(T t, SerializerFunc <T> serializerFunc) { // 2048 get a buffer large enough for all test objects var buffer = ArrayPool <byte> .Shared.Rent(2048); try { var writer = new FixedSizedBufferWriter(buffer); var json = new Utf8JsonWriter(writer); serializerFunc(ref json, t); return(buffer.AsMemory().Slice(0, (int)json.BytesWritten).ToArray()); } finally { ArrayPool <byte> .Shared.Return(buffer); } }