forked from anthonyreilly/NetCoreForce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonSerializer.cs
More file actions
102 lines (92 loc) · 3.91 KB
/
JsonSerializer.cs
File metadata and controls
102 lines (92 loc) · 3.91 KB
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
using System;
using Newtonsoft.Json;
using NetCoreForce.Client.Serializer;
namespace NetCoreForce.Client
{
public static class JsonSerializer
{
/// <summary>
/// Serializes an object into JSON including all non-null properties.
/// <para>Not to be used for create and update calls</para>
/// </summary>
/// <param name="inputObject">Object to serialize</param>
/// <param name="indented">use indented formatting, usually for readability</param>
/// <returns>JSON string</returns>
public static string SerializeComplete(object inputObject, bool indented)
{
Formatting formatting = Formatting.None;
if(indented)
{
formatting = Formatting.Indented;
}
string serializedJson = JsonConvert.SerializeObject(inputObject,
formatting,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
DateFormatString = DateFormats.FullDateFormatString
});
return serializedJson;
}
/// <summary>
/// Serializes an object into JSON for SObject updates, using the UpdateableContractResolver
/// </summary>
/// <param name="inputObject">Object to serialize</param>
/// <returns>JSON string, unformatted</returns>
public static string SerializeForUpdate(object inputObject)
{
string serializedJson = JsonConvert.SerializeObject(inputObject,
Formatting.None,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new UpdateableContractResolver(),
DateFormatString = DateFormats.FullDateFormatString
});
return serializedJson;
}
/// <summary>
/// Serializes an object into JSON for SObject updates, using the UpdateableContractResolver.
/// Includes the SObject ID for calls that require it
/// </summary>
/// <param name="inputObject">Object to serialize</param>
/// <returns>JSON string, unformatted</returns>
public static string SerializeForUpdateWithObjectId(object inputObject)
{
string serializedJson = JsonConvert.SerializeObject(inputObject,
Formatting.None,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new UpdateableWithIdContractResolver(),
DateFormatString = DateFormats.FullDateFormatString
});
return serializedJson;
}
/// <summary>
/// Serializes an object into JSON for SObject creation, using the CreateableContractResolver
/// </summary>
/// <param name="inputObject">Object to serialize</param>
/// <returns>JSON string, unformatted</returns>
public static string SerializeForCreate(object inputObject)
{
string serializedJson = JsonConvert.SerializeObject(inputObject,
Formatting.None,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new CreateableContractResolver(),
DateFormatString = DateFormats.FullDateFormatString
});
return serializedJson;
}
/// <summary>
/// Deserializes raw JSON into given type
/// </summary>
/// <param name="json">JSON object string</param>
public static T Deserialize<T>(string json)
{
return JsonConvert.DeserializeObject<T>(json);
}
}
}