-Fixed settings not being used when deserializing F# unions
This commit is contained in:
@@ -33,4 +33,4 @@ using System.Runtime.InteropServices;
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("6.0.0.0")]
|
||||
[assembly: AssemblyVersion("6.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("6.0.6.17901")]
|
||||
[assembly: AssemblyFileVersion("6.0.6.17905")]
|
||||
|
||||
@@ -37,6 +37,44 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
[TestFixture]
|
||||
public class DiscriminatedUnionConverterTests : TestFixtureBase
|
||||
{
|
||||
public class DoubleDoubleConverter : JsonConverter
|
||||
{
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
double d = (double)value;
|
||||
|
||||
writer.WriteValue(d * 2);
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
double d = (double)reader.Value;
|
||||
|
||||
return d / 2;
|
||||
}
|
||||
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
return objectType == typeof(double);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeUnionWithConverter()
|
||||
{
|
||||
string json = JsonConvert.SerializeObject(Shape.NewRectangle(10.0, 5.0), new DoubleDoubleConverter());
|
||||
|
||||
Assert.AreEqual(@"{""Case"":""Rectangle"",""Fields"":[20.0,10.0]}", json);
|
||||
|
||||
Shape c = JsonConvert.DeserializeObject<Shape>(json, new DoubleDoubleConverter());
|
||||
Assert.AreEqual(true, c.IsRectangle);
|
||||
|
||||
Shape.Rectangle r = (Shape.Rectangle)c;
|
||||
|
||||
Assert.AreEqual(5.0, r.length);
|
||||
Assert.AreEqual(10.0, r.width);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeBasicUnion()
|
||||
{
|
||||
|
||||
@@ -54,6 +54,52 @@ namespace Newtonsoft.Json.Tests.Linq.JsonPath
|
||||
[TestFixture]
|
||||
public class JPathExecuteTests : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void ParseWithEmptyArrayContent()
|
||||
{
|
||||
var json = @"{
|
||||
'controls': [
|
||||
{
|
||||
'messages': {
|
||||
'addSuggestion': {
|
||||
'en-US': 'Add'
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
'header': {
|
||||
'controls': []
|
||||
},
|
||||
'controls': [
|
||||
{
|
||||
'controls': [
|
||||
{
|
||||
'defaultCaption': {
|
||||
'en-US': 'Sort by'
|
||||
},
|
||||
'sortOptions': [
|
||||
{
|
||||
'label': {
|
||||
'en-US': 'Name'
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}";
|
||||
JObject jToken = JObject.Parse(json);
|
||||
IList<JToken> tokens = jToken.SelectTokens("$..en-US").ToList();
|
||||
|
||||
Assert.AreEqual(3, tokens.Count);
|
||||
Assert.AreEqual("Add", (string)tokens[0]);
|
||||
Assert.AreEqual("Sort by", (string)tokens[1]);
|
||||
Assert.AreEqual("Name", (string)tokens[2]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SelectTokenAfterEmptyContainer()
|
||||
{
|
||||
|
||||
@@ -1161,5 +1161,95 @@ keyword such as type of business.""
|
||||
}, "The best overloaded method match for 'System.Collections.Generic.IDictionary<string,string>.Add(string, string)' has some invalid arguments");
|
||||
}
|
||||
#endif
|
||||
|
||||
[Test]
|
||||
public void SerializeWithNoRedundentIdPropertiesTest()
|
||||
{
|
||||
Dictionary<string, object> dic1 = new Dictionary<string, object>();
|
||||
Dictionary<string, object> dic2 = new Dictionary<string, object>();
|
||||
Dictionary<string, object> dic3 = new Dictionary<string, object>();
|
||||
List<object> list1 = new List<object>();
|
||||
List<object> list2 = new List<object>();
|
||||
|
||||
dic1.Add("list1", list1);
|
||||
dic1.Add("list2", list2);
|
||||
dic1.Add("dic1", dic1);
|
||||
dic1.Add("dic2", dic2);
|
||||
dic1.Add("dic3", dic3);
|
||||
dic1.Add("integer", 12345);
|
||||
|
||||
list1.Add("A string!");
|
||||
list1.Add(dic1);
|
||||
list1.Add(new List<object>());
|
||||
|
||||
dic3.Add("dic3", dic3);
|
||||
|
||||
var json = SerializeWithNoRedundentIdProperties(dic1);
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""$id"": ""1"",
|
||||
""list1"": [
|
||||
""A string!"",
|
||||
{
|
||||
""$ref"": ""1""
|
||||
},
|
||||
[]
|
||||
],
|
||||
""list2"": [],
|
||||
""dic1"": {
|
||||
""$ref"": ""1""
|
||||
},
|
||||
""dic2"": {},
|
||||
""dic3"": {
|
||||
""$id"": ""3"",
|
||||
""dic3"": {
|
||||
""$ref"": ""3""
|
||||
}
|
||||
},
|
||||
""integer"": 12345
|
||||
}", json);
|
||||
}
|
||||
|
||||
private static string SerializeWithNoRedundentIdProperties(object o)
|
||||
{
|
||||
JTokenWriter writer = new JTokenWriter();
|
||||
JsonSerializer serializer = JsonSerializer.Create(new JsonSerializerSettings
|
||||
{
|
||||
Formatting = Formatting.Indented,
|
||||
PreserveReferencesHandling = PreserveReferencesHandling.Objects
|
||||
});
|
||||
serializer.Serialize(writer, o);
|
||||
|
||||
JToken t = writer.Token;
|
||||
|
||||
if (t is JContainer)
|
||||
{
|
||||
JContainer c = t as JContainer;
|
||||
|
||||
// find all the $id properties in the JSON
|
||||
IList<JProperty> ids = c.Descendants().OfType<JProperty>().Where(d => d.Name == "$id").ToList();
|
||||
|
||||
if (ids.Count > 0)
|
||||
{
|
||||
// find all the $ref properties in the JSON
|
||||
IList<JProperty> refs = c.Descendants().OfType<JProperty>().Where(d => d.Name == "$ref").ToList();
|
||||
|
||||
foreach (JProperty idProperty in ids)
|
||||
{
|
||||
// check whether the $id property is used by a $ref
|
||||
bool idUsed = refs.Any(r => idProperty.Value.ToString() == r.Value.ToString());
|
||||
|
||||
if (!idUsed)
|
||||
{
|
||||
// remove unused $id
|
||||
idProperty.Remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
string json = t.ToString();
|
||||
return json;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -74,4 +74,4 @@ using System.Security;
|
||||
// by using the '*' as shown below:
|
||||
|
||||
[assembly: AssemblyVersion("6.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("6.0.6.17901")]
|
||||
[assembly: AssemblyFileVersion("6.0.6.17905")]
|
||||
|
||||
@@ -25,7 +25,6 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Tests.TestObjects;
|
||||
@@ -155,10 +154,10 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
|
||||
Example e2 = JsonConvert.DeserializeObject<Example>(json);
|
||||
|
||||
JToken o1 = (JToken)e2.Data["extensionData1"];
|
||||
JArray o1 = (JArray)e2.Data["extensionData1"];
|
||||
|
||||
Assert.AreEqual(JTokenType.Array, o1.Type);
|
||||
Assert.AreEqual(3, o1.Count());
|
||||
Assert.AreEqual(3, o1.Count);
|
||||
Assert.AreEqual(1, (int)o1[0]);
|
||||
Assert.AreEqual(2, (int)o1[1]);
|
||||
Assert.AreEqual(3, (int)o1[2]);
|
||||
|
||||
@@ -157,7 +157,7 @@ namespace Newtonsoft.Json.Converters
|
||||
JToken t = fields[i];
|
||||
PropertyInfo fieldProperty = fieldProperties[i];
|
||||
|
||||
typedFieldValues[i] = t.ToObject(fieldProperty.PropertyType);
|
||||
typedFieldValues[i] = t.ToObject(fieldProperty.PropertyType, serializer);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,7 +189,7 @@ namespace Newtonsoft.Json.Converters
|
||||
foreach (object attribute in attributes)
|
||||
{
|
||||
Type attributeType = attribute.GetType();
|
||||
if (attributeType.Name == "CompilationMappingAttribute")
|
||||
if (attributeType.FullName == "Microsoft.FSharp.Core.CompilationMappingAttribute")
|
||||
{
|
||||
FSharpUtils.EnsureInitialized(attributeType.Assembly());
|
||||
|
||||
|
||||
@@ -91,5 +91,5 @@ using System.Security;
|
||||
// by using the '*' as shown below:
|
||||
|
||||
[assembly: AssemblyVersion("6.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("6.0.6.17901")]
|
||||
[assembly: AssemblyFileVersion("6.0.6.17905")]
|
||||
[assembly: CLSCompliant(true)]
|
||||
|
||||
Reference in New Issue
Block a user