-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNewtonsoftJsonTextSerializer.cs
More file actions
31 lines (28 loc) · 796 Bytes
/
NewtonsoftJsonTextSerializer.cs
File metadata and controls
31 lines (28 loc) · 796 Bytes
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
#if HAVE_NEWTONSOFT_JSON
using System;
using Newtonsoft.Json;
namespace Gilzoide.KeyValueStore.ObjectSerializers
{
public class NewtonsoftJsonTextSerializer : ITextSerializer
{
public JsonSerializerSettings JsonSettings { get; set; } = new JsonSerializerSettings();
public bool TryDeserializeObject<T>(string text, out T value)
{
try
{
value = JsonConvert.DeserializeObject<T>(text, JsonSettings);
return true;
}
catch (Exception)
{
value = default;
return false;
}
}
public string SerializeObject<T>(T obj)
{
return JsonConvert.SerializeObject(obj, JsonSettings);
}
}
}
#endif