123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Topten.JsonKit;
- using System.Reflection;
- using Xunit;
- namespace TestCases
- {
- [Obfuscation(Exclude = true, ApplyToMembers = true)]
- class ModelNotDecorated
- {
- public string Field1;
- public string Field2;
- public string Prop1 { get; set; }
- public string Prop2 { get; set; }
- }
- [Obfuscation(Exclude = true, ApplyToMembers = true)]
- class ModelInclude
- {
- [Json] public string Field1;
- public string Field2;
- [Json] public string Prop1 { get; set; }
- public string Prop2 { get; set; }
- }
- [Obfuscation(Exclude = true, ApplyToMembers = true)]
- class ModelExclude
- {
- public string Field1;
- public string Field2;
- public string Prop1 { get; set; }
- public string Prop2 { get; set; }
- [JsonExclude]
- public string Field3;
- [JsonExclude]
- public string Prop3 { get; set; }
- }
- [Obfuscation(Exclude = true, ApplyToMembers = true)]
- class ModelRenamedMembers
- {
- [Json("Field1")] public string Field1;
- public string Field2;
- [Json("Prop1")] public string Prop1 { get; set; }
- public string Prop2 { get; set; }
- }
- [Json]
- [Obfuscation(Exclude = true, ApplyToMembers = true)]
- class InstanceObject
- {
- public int IntVal1;
- [JsonExclude] public int IntVal2;
- }
- [Json]
- [Obfuscation(Exclude = true, ApplyToMembers = true)]
- class ModelKeepInstance
- {
- [Json(KeepInstance=true)]
- public InstanceObject InstObj;
- }
- [Json]
- [Obfuscation(Exclude = true, ApplyToMembers = true)]
- class ModelWithInstance
- {
- [Json]
- public InstanceObject InstObj;
- }
- [Json]
- [Obfuscation(Exclude = true, ApplyToMembers = true)]
- struct ModelStruct
- {
- public int IntField;
- public int IntProp { get; set; }
- }
- [Obfuscation(Exclude = true, ApplyToMembers = true)]
- public class TestsReflection
- {
- [Fact]
- public void ExcludeAttribute()
- {
- var m = new ModelExclude()
- {
- Field1 = "f1",
- Field2 = "f2",
- Field3 = "f3",
- Prop1 = "p1",
- Prop2 = "p2",
- Prop3 = "p3",
- };
- var json = Json.Format(m);
- Assert.Contains("field1", json);
- Assert.Contains("field2", json);
- Assert.DoesNotContain("field3", json);
- Assert.Contains("prop1", json);
- Assert.Contains("prop2", json);
- Assert.DoesNotContain("prop3", json);
- }
- [Fact]
- public void NonDecorated()
- {
- var m = new ModelNotDecorated()
- {
- Field1 = "f1",
- Field2 = "f2",
- Prop1 = "p1",
- Prop2 = "p2",
- };
- var json = Json.Format(m);
- Assert.Contains("field1", json);
- Assert.Contains("field2", json);
- Assert.Contains("prop1", json);
- Assert.Contains("prop2", json);
- }
- [Fact]
- public void Include()
- {
- var m = new ModelInclude()
- {
- Field1 = "f1",
- Field2 = "f2",
- Prop1 = "p1",
- Prop2 = "p2",
- };
- var json = Json.Format(m);
- Assert.Contains("field1", json);
- Assert.DoesNotContain("field2", json);
- Assert.Contains("prop1", json);
- Assert.DoesNotContain("prop2", json);
- }
- [Fact]
- public void RenamedMembers()
- {
- var m = new ModelRenamedMembers()
- {
- Field1 = "f1",
- Field2 = "f2",
- Prop1 = "p1",
- Prop2 = "p2",
- };
- var json = Json.Format(m);
- Assert.Contains("Field1", json);
- Assert.DoesNotContain("field2", json);
- Assert.Contains("Prop1", json);
- Assert.DoesNotContain("prop2", json);
- }
- [Fact]
- public void KeepInstanceTest1()
- {
- // Create model and save it
- var ki = new ModelKeepInstance();
- ki.InstObj = new InstanceObject();
- ki.InstObj.IntVal1 = 1;
- ki.InstObj.IntVal2 = 2;
- var json = Json.Format(ki);
- // Update the kept instance object
- ki.InstObj.IntVal1 = 11;
- ki.InstObj.IntVal2 = 12;
- // Reload
- var oldInst = ki.InstObj;
- Json.ParseInto(json, ki);
- // Check object instance kept
- Assert.Same(oldInst, ki.InstObj);
- // Check json properties updated, others not
- Assert.Equal(1, ki.InstObj.IntVal1);
- Assert.Equal(12, ki.InstObj.IntVal2);
- }
- [Fact]
- public void KeepInstanceTest2()
- {
- // Create model and save it
- var ki = new ModelKeepInstance();
- ki.InstObj = new InstanceObject();
- ki.InstObj.IntVal1 = 1;
- ki.InstObj.IntVal2 = 2;
- var json = Json.Format(ki);
- // Update the kept instance object
- ki.InstObj = null;
- // Reload
- Json.ParseInto(json, ki);
- // Check object instance kept
- Assert.NotNull(ki.InstObj);
- // Check json properties updated, others not
- Assert.Equal(1, ki.InstObj.IntVal1);
- Assert.Equal(0, ki.InstObj.IntVal2);
- }
- [Fact]
- public void StructTest()
- {
- var o = new ModelStruct();
- o.IntField = 23;
- o.IntProp = 24;
- var json = Json.Format(o);
- Assert.Contains("23", json);
- Assert.Contains("24", json);
- var o2 = Json.Parse<ModelStruct>(json);
- Assert.Equal(23, o2.IntField);
- Assert.Equal(24, o2.IntProp);
- // Test parseInto on a value type not supported
- var o3 = new ModelStruct();
- Assert.Throws<InvalidOperationException>(() => Json.ParseInto(json, o3));
- }
- [Fact]
- public void NullClassMember()
- {
- var m = new ModelWithInstance();
- var json = Json.Format(m);
- Assert.Contains("null", json);
- m.InstObj = new InstanceObject();
- Json.ParseInto(json, m);
- Assert.Null(m.InstObj);
- }
- [Fact]
- public void NullClass()
- {
- // Save null
- var json = Json.Format(null);
- Assert.Equal("null", json);
- // Load null
- var m = Json.Parse<ModelWithInstance>("null");
- Assert.Null(m);
- // Should fail to parse null into an existing instance
- m = new ModelWithInstance();
- Assert.Throws<JsonParseException>(() => Json.ParseInto("null", m));
- }
- }
- }
|