-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomJConvert.cs
49 lines (46 loc) · 1.95 KB
/
CustomJConvert.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
using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
namespace Berry.Serialization
{
/// <summary>
/// Provides methods for converting between .NET types and JSON types.
/// </summary>
public static class CustomJConvert
{
/// <summary>
/// Serializes the specified object to a JSON string.
/// </summary>
/// <param name="value">The object to serialize.</param>
/// <returns>A JSON string representation of the object.</returns>
public static string SerializeObject(object value)
{
return SerializeObject(value, Formatting.None);
}
/// <summary>
/// Serializes the specified object to a JSON string using formatting.
/// </summary>
/// <param name="value">The object to serilize.</param>
/// <param name="formatting">Indicates how the output should be formatted.</param>
/// <returns>A JSON string representation of the object.</returns>
public static string SerializeObject(object value, Formatting formatting)
{
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.ContractResolver = new ContractSerializeResolver();
return JsonConvert.SerializeObject(value, formatting, settings);
}
/// <summary>
/// Deserializes the JSON to the specified .NET type.
/// </summary>
/// <typeparam name="T">The type of the objetc to deserialize to.</typeparam>
/// <param name="value">The JSON to deserialize.</param>
/// <returns>The deserialized object from the JSON string.</returns>
public static T DeserializeObject<T>(string value)
{
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.ContractResolver = new ContractDeserializeResolver();
return JsonConvert.DeserializeObject<T>(value, settings);
}
}
}