1+ using System . Text . Json ;
2+
3+ namespace FAST . FBasic . InteractiveConsole . DemoObjects
4+ {
5+ // ----------------------------------------------------------------------
6+ // 1. Root Class (Mapping to the main JSON object)
7+ // Properties are now camelCase to match the JSON keys exactly,
8+ // and JsonPropertyName attributes have been removed.
9+ // ----------------------------------------------------------------------
10+ public record EmployeeProfile
11+ {
12+ // Basic types
13+ public string name { get ; init ; } = string . Empty ;
14+ public int age { get ; init ; }
15+ public decimal salary { get ; init ; }
16+ public bool isActive { get ; init ; }
17+ public string email { get ; init ; } = string . Empty ;
18+
19+ // Nested Object
20+ public Address address { get ; init ; } = new Address ( ) ;
21+
22+ // Array of strings
23+ public List < string > hobbies { get ; init ; } = new List < string > ( ) ;
24+
25+ // Array of integers
26+ public List < int > scores { get ; init ; } = new List < int > ( ) ;
27+
28+ // Array of nested objects
29+ public List < Project > projects { get ; init ; } = new List < Project > ( ) ;
30+ }
31+
32+ // ----------------------------------------------------------------------
33+ // 2. Nested Class for the 'address' object
34+ // Properties are now camelCase.
35+ // ----------------------------------------------------------------------
36+ public record Address
37+ {
38+ public string street { get ; init ; } = string . Empty ;
39+ public string city { get ; init ; } = string . Empty ;
40+ public string zipCode { get ; init ; } = string . Empty ;
41+ }
42+
43+ // ----------------------------------------------------------------------
44+ // 3. Nested Class for items within the 'projects' array
45+ // Properties are now camelCase.
46+ // ----------------------------------------------------------------------
47+ public record Project
48+ {
49+ public string name { get ; init ; } = string . Empty ;
50+ public string status { get ; init ; } = string . Empty ;
51+ }
52+
53+ // ----------------------------------------------------------------------
54+ // Example usage (Demonstrates how to actually deserialize the data)
55+ // Note: The example usage now accesses properties using their new camelCase names.
56+ // ----------------------------------------------------------------------
57+ public class EmployeeProfileExample
58+ {
59+ public EmployeeProfile GetEmployeeProfile ( )
60+ {
61+ // NOTE: JsonPropertyName is no longer needed because the C# properties
62+ // are named exactly like the JSON keys (camelCase).
63+ string json = @"{
64+ ""name"": ""John Doe"",
65+ ""age"": 30,
66+ ""salary"": 75000.50,
67+ ""isActive"": true,
68+ ""email"": ""john@example.com"",
69+ ""address"": {
70+ ""street"": ""123 Main St"",
71+ ""city"": ""New York"",
72+ ""zipCode"": ""10001""
73+ },
74+ ""hobbies"": [""reading"", ""gaming"", ""coding""],
75+ ""scores"": [95, 87, 92, 88],
76+ ""projects"": [
77+ {
78+ ""name"": ""Project A"",
79+ ""status"": ""completed""
80+ },
81+ {
82+ ""name"": ""Project B"",
83+ ""status"": ""in-progress""
84+ }
85+ ]
86+ }" ;
87+
88+ // Deserialize the JSON string into the C# object
89+ var profile = JsonSerializer . Deserialize < EmployeeProfile > ( json ) ;
90+
91+ return profile ;
92+ }
93+
94+
95+ public void TestPrint ( EmployeeProfile profile )
96+ {
97+
98+ if ( profile != null )
99+ {
100+ Console . WriteLine ( $ "Name: { profile . name } , Age: { profile . age } ") ;
101+ Console . WriteLine ( $ "City: { profile . address . city } ") ;
102+ Console . WriteLine ( $ "First Hobby: { profile . hobbies [ 0 ] } ") ;
103+ Console . WriteLine ( $ "Project Count: { profile . projects . Count } ") ;
104+ }
105+ }
106+ }
107+
108+ }
0 commit comments