Skip to content

Commit

Permalink
When serialising SearchConditionsJA / SearchConditions, json exceptio…
Browse files Browse the repository at this point in the history
…ns occur (#121)

Added unit test showing issue.
Altered serialisation process to deal with search conditions.
  • Loading branch information
CraigHawker authored Dec 11, 2023
1 parent edaaafe commit 080c29c
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 5 deletions.
62 changes: 62 additions & 0 deletions MFiles.VAF.Extensions.Tests/NewtonsoftJsonConvert.cs
Original file line number Diff line number Diff line change
@@ -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);

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<SearchConditions> a)
{
writer.WriteRawValue(a.ToJson());
return;
}
}
{
if (value is IMFJsonAdaptor<SearchCondition> a)
{
writer.WriteRawValue(a.ToJson());
return;
}
}

// Use the basic approach.
base.WriteJson(writer, value, serializer);
return;
}
Expand Down

0 comments on commit 080c29c

Please sign in to comment.