public void JsonConvertSerializerSettings()
    {
      Person person = new Person();
      person.BirthDate = new DateTime(2000, 11, 20, 23, 55, 44, DateTimeKind.Utc);
      person.LastModified = new DateTime(2000, 11, 20, 23, 55, 44, DateTimeKind.Utc);
      person.Name = "Name!";

      string json = JsonConvert.SerializeObject(person, Formatting.Indented, new JsonSerializerSettings
                                                               {
                                                                 ContractResolver = new CamelCasePropertyNamesContractResolver()
                                                               });

      Assert.AreEqual(@"{
  ""name"": ""Name!"",
  ""birthDate"": ""2000-11-20T23:55:44Z"",
  ""lastModified"": ""2000-11-20T23:55:44Z""
}", json);

      Person deserializedPerson = JsonConvert.DeserializeObject<Person>(json, new JsonSerializerSettings
                                                                        {
                                                                          ContractResolver = new CamelCasePropertyNamesContractResolver()
                                                                        });

      Assert.AreEqual(person.BirthDate, deserializedPerson.BirthDate);
      Assert.AreEqual(person.LastModified, deserializedPerson.LastModified);
      Assert.AreEqual(person.Name, deserializedPerson.Name);

      json = JsonConvert.SerializeObject(person, Formatting.Indented);
      Assert.AreEqual(@"{
  ""Name"": ""Name!"",
  ""BirthDate"": ""2000-11-20T23:55:44Z"",
  ""LastModified"": ""2000-11-20T23:55:44Z""
}", json);

    }
        public void PopulatePerson()
        {
            Person p = new Person();

            JsonConvert.PopulateObject(@"{""Name"":""James""}", p);

            Assert.AreEqual("James", p.Name);
        }
    public void CreateGetWithBadObjectTarget()
    {
      Person p = new Person();
      p.Name = "Hi";

      Func<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateGet<object>(typeof(Movie).GetProperty("Name"));

      setter(p);
    }
    public void CreateGetWithBadObjectTarget()
    {
      ExceptionAssert.Throws<InvalidCastException>("Unable to cast object of type 'Newtonsoft.Json.Tests.TestObjects.Person' to type 'Newtonsoft.Json.Tests.TestObjects.Movie'.",
      () =>
      {
        Person p = new Person();
        p.Name = "Hi";

        Func<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateGet<object>(typeof(Movie).GetProperty("Name"));

        setter(p);
      });
    }
    public void CreateSetWithBadObjectTarget()
    {
      Person p = new Person();
      Movie m = new Movie();

      Action<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateSet<object>(typeof(Movie).GetProperty("Name"));

      setter(m, "Hi");

      Assert.AreEqual(m.Name, "Hi");

      setter(p, "Hi");
    }
        public void BlogCodeSample()
        {
            Person p = new Person
            {
                Name = "Keith",
                BirthDate = new DateTime(1980, 3, 8),
                LastModified = new DateTime(2009, 4, 12, 20, 44, 55),
            };

            string jsonText = JsonConvert.SerializeObject(p, new IsoDateTimeConverter());
            // {
            //   "Name": "Keith",
            //   "BirthDate": "1980-03-08T00:00:00",
            //   "LastModified": "2009-04-12T20:44:55"
            // }

            Assert.AreEqual(@"{""Name"":""Keith"",""BirthDate"":""1980-03-08T00:00:00"",""LastModified"":""2009-04-12T20:44:55""}", jsonText);
        }
    public void BlogCodeSample()
    {
      Person p = new Person
                   {
                     Name = "Keith",
                     BirthDate = new DateTime(1980, 3, 8),
                     LastModified = new DateTime(2009, 4, 12, 20, 44, 55),
                   };

      string jsonText = JsonConvert.SerializeObject(p, new IsoDateTimeConverter());
      // {
      //   "Name": "Keith",
      //   "BirthDate": "1980-03-08T00:00:00",
      //   "LastModified": "2009-04-12T20:44:55"
      // }

      Console.WriteLine(jsonText);

    }
    public void FromObjectInsideConverterWithCustomSerializer()
    {
      var p = new Person
      {
        Name = "Daniel Wertheim",
      };

      var settings = new JsonSerializerSettings
      {
        Converters = new List<JsonConverter> { new FooJsonConverter() },
        ContractResolver = new CamelCasePropertyNamesContractResolver()
      };

      var json = JsonConvert.SerializeObject(p, settings);

      Assert.AreEqual(@"{""foo"":""bar"",""name"":""Daniel Wertheim"",""birthDate"":""0001-01-01T00:00:00"",""lastModified"":""0001-01-01T00:00:00""}", json);
    }
        public void CreatePropertyGetter()
        {
            PropertyInfo namePropertyInfo = typeof(Person).GetProperty(nameof(Person.Name));

            Assert.IsNotNull(namePropertyInfo);

            var call = DynamicReflectionDelegateFactory.Instance.CreateGet<Person>(namePropertyInfo);

            Person p = new Person();
            p.Name = "Name!";

            object result = call(p);
            Assert.IsNotNull(result);

            Assert.AreEqual("Name!", (string)result);
        }
        public void CreateSetWithBadObjectTarget()
        {
            ExceptionAssert.Throws<InvalidCastException>(() =>
            {
                Person p = new Person();
                Movie m = new Movie();

                Action<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateSet<object>(typeof(Movie).GetProperty("Name"));

                setter(m, "Hi");

                Assert.AreEqual(m.Name, "Hi");

                setter(p, "Hi");

                Assert.AreEqual(p.Name, "Hi");
            }, "Unable to cast object of type 'Newtonsoft.Json.Tests.TestObjects.Person' to type 'Newtonsoft.Json.Tests.TestObjects.Movie'.");
        }
        public void CreateSetWithBadObjectTarget()
        {
            ExceptionAssert.Throws<InvalidCastException>(
                () => {
                    Person p = new Person();
                    Movie m = new Movie();

                    Action<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(typeof(Movie).GetProperty("Name"));

                    setter(m, "Hi");

                    Assert.AreEqual(m.Name, "Hi");

                    setter(p, "Hi");

                    Assert.AreEqual(p.Name, "Hi");
                },
                new [] {
                    "Unable to cast object of type 'Newtonsoft.Json.Tests.TestObjects.Person' to type 'Newtonsoft.Json.Tests.TestObjects.Movie'.",
                    "Cannot cast from source type to destination type." // mono
                });
        }
 public PersonPropertyClass()
 {
   Person = new WagePerson();
 }
        public void CreateGetWithBadObjectTarget()
        {
            ExceptionAssert.Throws<InvalidCastException>(
                () => {
                    Person p = new Person();
                    p.Name = "Hi";

                    Func<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateGet<object>(TestReflectionUtils.GetProperty(typeof(Movie), "Name"));

                    setter(p);
                },
                new [] {
                    "Unable to cast object of type 'Newtonsoft.Json.Tests.TestObjects.Person' to type 'Newtonsoft.Json.Tests.TestObjects.Movie'.",
                    "Cannot cast from source type to destination type." // mono
                });
        }
        public void JsonConvertSerializerSettings()
        {
            var person = new Person();
              person.BirthDate = new DateTime(2000, 11, 20, 23, 55, 44, DateTimeKind.Utc);
              person.LastModified = new DateTime(2000, 11, 20, 23, 55, 44, DateTimeKind.Utc);
              person.Name = "Name!";

              string json = ChasonSerializer.SerializeToString(person, new ChasonSerializerSettings()
                                                               {
                                                                   PropertyNameComparer = StringComparer.OrdinalIgnoreCase,
                                                                   OutputFormattedJson = true,
                                                                   OutputCamelCasePropertyNames = true,
                                                                   DateTimeFormat = ChasonSerializerSettings.JavascriptDateObjectDateTimeFormat
                                                               });

              json.Should().Be(@"{
              ""name"": ""Name!"",
              ""birthDate"": ""\/Date(974764544000)\/"",
              ""lastModified"": ""\/Date(974764544000)\/""
            }");

              Person deserializedPerson = ChasonSerializer.DeserializeFromString<Person>(json, new ChasonSerializerSettings
                                                                        {
                                                                            PropertyNameComparer = StringComparer.OrdinalIgnoreCase,
                                                                            OutputFormattedJson = true,
                                                                            OutputCamelCasePropertyNames = true,
                                                                            DateTimeFormat = ChasonSerializerSettings.JavascriptDateObjectDateTimeFormat
                                                                        });

              Assert.AreEqual(person.BirthDate, deserializedPerson.BirthDate);
              Assert.AreEqual(person.LastModified, deserializedPerson.LastModified);
              Assert.AreEqual(person.Name, deserializedPerson.Name);

              json = ChasonSerializer.SerializeToString(person, new ChasonSerializerSettings() { OutputFormattedJson = true });
              json.Should().Be(@"{
              ""Name"": ""Name!"",
              ""BirthDate"": ""\/Date(974764544000)\/"",
              ""LastModified"": ""\/Date(974764544000)\/""
            }");
        }