-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
64 lines (57 loc) · 2.17 KB
/
Program.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
using System;
using System.Dynamic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
namespace jobject_poc {
class Program {
private const string json = @"
{
'SimpleString': 'Value',
'Ciccio': [1, 2, 'buffo'],
'CalpurnioSiculo': {
'AbraCadaBra': true
},
'i_Am_an_Ugly_Snake': {
'i_am_a_cool_snake': 'yeah'
}
}
";
static JObject GetData () => JObject.Parse (json);
static void Main (string[] args) {
var obj = new ModelSample {
AncientAge = 666,
FirstName = "Calpurnio",
CustomData = GetData ()
};
Console.WriteLine ("NOT WORKING SNAKE CASE (JObject's properties aren't serialized accordingly");
BadExample (obj);
Console.WriteLine ("\n\nWORKING SNAKE CASE");
SnakeCaseSample (obj);
}
static void BadExample (object obj) {
var snakeNameStrategy = new SnakeCaseNamingStrategy ();
var jsonSnakeSettings = new JsonSerializerSettings {
Formatting = Formatting.Indented,
ContractResolver = new DefaultContractResolver {
NamingStrategy = snakeNameStrategy
},
};
var json = JsonConvert.SerializeObject (obj, jsonSnakeSettings);
Console.WriteLine (json);
}
static void SnakeCaseSample (object obj) {
var snakeNameStrategy = new SnakeCaseNamingStrategy ();
var jsonSnakeSettings = new JsonSerializerSettings {
Formatting = Formatting.Indented,
Converters = new [] { new JObjectNamingStrategyConverter (snakeNameStrategy) },
ContractResolver = new DefaultContractResolver {
NamingStrategy = snakeNameStrategy
},
};
var json = JsonConvert.SerializeObject (obj, jsonSnakeSettings);
Console.WriteLine (json);
return;
}
}
}