Ejemplo n.º 1
0
        public void Example()
        {
            string json;

            try
            {
                #region Usage
                // settings will automatically be used by JsonConvert.SerializeObject/DeserializeObject
                JsonConvert.DefaultSettings = () => new JsonSerializerSettings
                {
                    Formatting = Formatting.Indented,
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                };

                Staff s = new Staff
                {
                    FirstName = "Eric",
                    LastName = "Example",
                    BirthDate = new DateTime(1980, 4, 20, 0, 0, 0, DateTimeKind.Utc),
                    Department = "IT",
                    JobTitle = "Web Dude"
                };

                json = JsonConvert.SerializeObject(s);
                // {
                //   "firstName": "Eric",
                //   "lastName": "Example",
                //   "birthDate": "1980-04-20T00:00:00Z",
                //   "department": "IT",
                //   "jobTitle": "Web Dude"
                // }
                #endregion
            }
            finally
            {
                JsonConvert.DefaultSettings = null;
            }

            StringAssert.AreEqual(@"{
  ""firstName"": ""Eric"",
  ""lastName"": ""Example"",
  ""birthDate"": ""1980-04-20T00:00:00Z"",
  ""department"": ""IT"",
  ""jobTitle"": ""Web Dude""
}", json);
        }
Ejemplo n.º 2
0
        public void Example()
        {
            string json;

            try
            {
                #region Usage
                // settings will automatically be used by JsonConvert.SerializeObject/DeserializeObject
                JsonConvert.DefaultSettings = () => new JsonSerializerSettings
                {
                    Formatting       = Formatting.Indented,
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                };

                Staff s = new Staff
                {
                    FirstName  = "Eric",
                    LastName   = "Example",
                    BirthDate  = new DateTime(1980, 4, 20, 0, 0, 0, DateTimeKind.Utc),
                    Department = "IT",
                    JobTitle   = "Web Dude"
                };

                json = JsonConvert.SerializeObject(s);
                // {
                //   "firstName": "Eric",
                //   "lastName": "Example",
                //   "birthDate": "1980-04-20T00:00:00Z",
                //   "department": "IT",
                //   "jobTitle": "Web Dude"
                // }
                #endregion
            }
            finally
            {
                JsonConvert.DefaultSettings = null;
            }

            StringAssert.AreEqual(@"{
  ""firstName"": ""Eric"",
  ""lastName"": ""Example"",
  ""birthDate"": ""1980-04-20T00:00:00Z"",
  ""department"": ""IT"",
  ""jobTitle"": ""Web Dude""
}", json);
        }