/
JilSerializer.cs
105 lines (87 loc) · 2.89 KB
/
JilSerializer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using System.Data.Common;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Jil;
using Marten.Services;
using Marten.Util;
using Weasel.Core;
using Weasel.Postgresql;
#nullable enable
namespace Marten.Testing
{
#region sample_JilSerializer
public class JilSerializer : ISerializer
{
private readonly Options _options
= new(dateFormat: DateTimeFormat.ISO8601, includeInherited:true);
public ValueCasting ValueCasting { get; } = ValueCasting.Strict;
public string ToJson(object? document)
{
return JSON.Serialize(document, _options);
}
public T FromJson<T>(Stream stream)
{
return JSON.Deserialize<T>(stream.GetStreamReader(), _options);
}
public T FromJson<T>(DbDataReader reader, int index)
{
var stream = reader.GetStream(index);
return FromJson<T>(stream);
}
public ValueTask<T> FromJsonAsync<T>(Stream stream, CancellationToken cancellationToken = default)
{
return new(FromJson<T>(stream));
}
public ValueTask<T> FromJsonAsync<T>(DbDataReader reader, int index, CancellationToken cancellationToken = default)
{
return new (FromJson<T>(reader, index));
}
public object FromJson(Type type, Stream stream)
{
return JSON.Deserialize(stream.GetStreamReader(), type, _options);
}
public object FromJson(Type type, DbDataReader reader, int index)
{
var stream = reader.GetStream(index);
return FromJson(type, stream);
}
public ValueTask<object> FromJsonAsync(Type type, Stream stream, CancellationToken cancellationToken = default)
{
return new (FromJson(type, stream));
}
public ValueTask<object> FromJsonAsync(Type type, DbDataReader reader, int index, CancellationToken cancellationToken = default)
{
return new (FromJson(type, reader, index));
}
public string ToCleanJson(object? document)
{
return ToJson(document);
}
public EnumStorage EnumStorage => EnumStorage.AsString;
public Casing Casing => Casing.Default;
public string ToJsonWithTypes(object document)
{
throw new NotSupportedException();
}
}
#endregion
public class TestsSerializer : JsonNetSerializer
{
}
public static class JilSamples
{
public static void Build_With_Jil()
{
#region sample_replacing_serializer_with_jil
var store = DocumentStore.For(_ =>
{
_.Connection("the connection string");
// Replace the ISerializer w/ the TestsSerializer
_.Serializer<TestsSerializer>();
});
#endregion
}
}
}