This commit is contained in:
James Newton-King
2014-06-22 19:53:01 +12:00
parent 74500669b9
commit 56e45bbbab
3 changed files with 79 additions and 13 deletions
@@ -5806,27 +5806,86 @@ To fix this error either change the environment to be fully trusted, change the
} }
} }
[Test] public class ReflectionContractResolver : DefaultContractResolver
public void SerializeStaticField()
{ {
StaticFieldTestClass c = new StaticFieldTestClass protected override IValueProvider CreateMemberValueProvider(MemberInfo member)
{
return new ReflectionValueProvider(member);
}
}
[Test]
public void SerializeStaticDefault()
{
DefaultContractResolver contractResolver = new DefaultContractResolver();
StaticTestClass c = new StaticTestClass
{ {
x = int.MaxValue x = int.MaxValue
}; };
string json = JsonConvert.SerializeObject(c, Formatting.Indented); StaticTestClass.y = 2;
StaticTestClass.z = 3;
string json = JsonConvert.SerializeObject(c, Formatting.Indented, new JsonSerializerSettings
{
ContractResolver = contractResolver
});
Assert.AreEqual(@"{ Assert.AreEqual(@"{
""x"": 2147483647, ""x"": 2147483647,
""y"": 2 ""y"": 2,
""z"": 3
}", json); }", json);
StaticFieldTestClass c2 = JsonConvert.DeserializeObject<StaticFieldTestClass>(@"{ StaticTestClass c2 = JsonConvert.DeserializeObject<StaticTestClass>(@"{
""x"": -1, ""x"": -1,
""y"": -2 ""y"": -2,
}"); ""z"": -3
}",
new JsonSerializerSettings
{
ContractResolver = contractResolver
});
Assert.AreEqual(-1, c2.x); Assert.AreEqual(-1, c2.x);
Assert.AreEqual(-2, StaticFieldTestClass.y); Assert.AreEqual(-2, StaticTestClass.y);
Assert.AreEqual(-3, StaticTestClass.z);
}
[Test]
public void SerializeStaticReflection()
{
ReflectionContractResolver contractResolver = new ReflectionContractResolver();
StaticTestClass c = new StaticTestClass
{
x = int.MaxValue
};
StaticTestClass.y = 2;
StaticTestClass.z = 3;
string json = JsonConvert.SerializeObject(c, Formatting.Indented, new JsonSerializerSettings
{
ContractResolver = contractResolver
});
Assert.AreEqual(@"{
""x"": 2147483647,
""y"": 2,
""z"": 3
}", json);
StaticTestClass c2 = JsonConvert.DeserializeObject<StaticTestClass>(@"{
""x"": -1,
""y"": -2,
""z"": -3
}",
new JsonSerializerSettings
{
ContractResolver = contractResolver
});
Assert.AreEqual(-1, c2.x);
Assert.AreEqual(-2, StaticTestClass.y);
Assert.AreEqual(-3, StaticTestClass.z);
} }
#if !(NET20 || NETFX_CORE) #if !(NET20 || NETFX_CORE)
@@ -8151,12 +8210,20 @@ Parameter name: value",
} }
[JsonObject(MemberSerialization.OptIn)] [JsonObject(MemberSerialization.OptIn)]
public class StaticFieldTestClass public class StaticTestClass
{ {
[JsonProperty] [JsonProperty]
public int x = 1; public int x = 1;
[JsonProperty] [JsonProperty]
public static int y = 2; public static int y = 2;
[JsonProperty]
public static int z { get; set; }
static StaticTestClass()
{
z = 3;
}
} }
} }
@@ -1134,7 +1134,7 @@ namespace Newtonsoft.Json.Serialization
else else
valueProvider = new ReflectionValueProvider(member); valueProvider = new ReflectionValueProvider(member);
#elif !(PORTABLE40) #elif !(PORTABLE40)
valueProvider = new ExpressionValueProvider(member); valueProvider = new ExpressionValueProvider(member);
#else #else
valueProvider = new ReflectionValueProvider(member); valueProvider = new ReflectionValueProvider(member);
#endif #endif
@@ -110,5 +110,4 @@ namespace Newtonsoft.Json.Serialization
} }
} }
} }
#endif #endif