diff --git a/MFiles.VAF.Extensions.Tests/NewtonsoftJsonConvert.cs b/MFiles.VAF.Extensions.Tests/NewtonsoftJsonConvert.cs new file mode 100644 index 0000000..3a6e5da --- /dev/null +++ b/MFiles.VAF.Extensions.Tests/NewtonsoftJsonConvert.cs @@ -0,0 +1,62 @@ +using MFiles.VAF.Configuration.JsonAdaptor; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace MFiles.VAF.Extensions.Tests +{ + [TestClass] + public class NewtonsoftJsonConvertTests + { + [DataContract] + public class Configuration + { + [DataMember] + public SearchConditionsJA SearchConditions { get; set; } + } + + [TestMethod] + public void SearchConditions() + { + Configuration config = new Configuration(); + config.SearchConditions = new SearchConditionsJA(); + config.SearchConditions.Add(new SearchConditionJA() + { + ConditionType = MFilesAPI.MFConditionType.MFConditionTypeEqual, + Expression = new ExpressionJA() + { + PropertyDef = 0, + DataType = MFilesAPI.MFDataType.MFDatatypeText + }, + TypedValue = new TypedValueJA() + { + DataType = MFilesAPI.MFDataType.MFDatatypeText, + Value = "hello world" + } + }); + config.SearchConditions.Add(new SearchConditionJA() + { + ConditionType = MFilesAPI.MFConditionType.MFConditionTypeEqual, + Expression = new ExpressionJA() + { + PropertyDef = 123, + DataType = MFilesAPI.MFDataType.MFDatatypeBoolean + }, + TypedValue = new TypedValueJA() + { + DataType = MFilesAPI.MFDataType.MFDatatypeBoolean, + Value = true + } + }); + + var serializer = new NewtonsoftJsonConvert(); + var x = serializer.Serialize(config); + Assert.IsNotNull(x); + + } + } +} diff --git a/MFiles.VAF.Extensions/Configuration/Upgrading/JsonConversion/NewtonsoftJsonConvert.cs b/MFiles.VAF.Extensions/Configuration/Upgrading/JsonConversion/NewtonsoftJsonConvert.cs index 32e146d..1d34b8e 100644 --- a/MFiles.VAF.Extensions/Configuration/Upgrading/JsonConversion/NewtonsoftJsonConvert.cs +++ b/MFiles.VAF.Extensions/Configuration/Upgrading/JsonConversion/NewtonsoftJsonConvert.cs @@ -3,6 +3,7 @@ using MFiles.VAF.Configuration.Logging; using MFiles.VAF.Extensions.Configuration; using MFiles.VAF.Extensions.Configuration.Upgrading; +using MFilesAPI; using Newtonsoft.Json; using Newtonsoft.Json.Converters; using Newtonsoft.Json.Linq; @@ -241,13 +242,20 @@ public object GetValue(object target) return null; } - var serializedValue = null == value ? "{}" : this.JsonConvert.Serialize(value); - var serializedDefaultValue = null == defaultValue ? "{}" : this.JsonConvert.Serialize(defaultValue); + try + { + var serializedValue = null == value ? "{}" : this.JsonConvert.Serialize(value); + var serializedDefaultValue = null == defaultValue ? "{}" : this.JsonConvert.Serialize(defaultValue); - if (serializedValue == "{}" || serializedDefaultValue == serializedValue) + if (serializedValue == "{}" || serializedDefaultValue == serializedValue) + { + this.Logger?.Trace($"Value at {this.memberInfo.ReflectedType.FullName}.{this.memberInfo.Name} is empty, so not writing it to JSON."); + return null; + } + } + catch (Exception e) { - this.Logger?.Trace($"Value at {this.memberInfo.ReflectedType.FullName}.{this.memberInfo.Name} is empty, so not writing it to JSON."); - return null; + this.Logger?.Warn(e, $"Could not create default serialised value for {this.memberInfo.ReflectedType.FullName}.{this.memberInfo.Name}"); } } catch(Exception e) @@ -411,6 +419,24 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s if(null == value || !RawJsonLookup.ContainsKey(value)) { this.Logger?.Warn($"No data found in cache for {writer.Path}."); + + // We have to deal with some types explicitly... + { + if (value is IMFJsonAdaptor a) + { + writer.WriteRawValue(a.ToJson()); + return; + } + } + { + if (value is IMFJsonAdaptor a) + { + writer.WriteRawValue(a.ToJson()); + return; + } + } + + // Use the basic approach. base.WriteJson(writer, value, serializer); return; }