Skip to content

Commit

Permalink
Merge pull request finos#128 from ztanczos/fdc3_intents
Browse files Browse the repository at this point in the history
Add custom JsonConverters for Intents
  • Loading branch information
bingenito authored Apr 18, 2024
2 parents 5e332b8 + edce140 commit dff2f32
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 7 deletions.
16 changes: 10 additions & 6 deletions src/Fdc3.Json/Serialization/Fdc3JsonSerializerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,21 @@ namespace Finos.Fdc3.Json.Serialization
public static class Fdc3JsonSerializerOptions
{
public static JsonSerializerOptions Create()
{
var options = CreateWithoutConverters();
options.Converters.Add(new JsonStringEnumConverter(new Fdc3CamelCaseNamingPolicy()));
options.Converters.Add(new Fdc3AppConverter());
options.Converters.Add(new RecipientJsonConverter());
options.Converters.Add(new IntentsConverter());
return options;
}

internal static JsonSerializerOptions CreateWithoutConverters()
{
return new JsonSerializerOptions
{
ReferenceHandler = ReferenceHandler.IgnoreCycles,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
Converters =
{
new JsonStringEnumConverter(new Fdc3CamelCaseNamingPolicy()),
new Fdc3AppConverter(),
new RecipientJsonConverter()
},
PropertyNamingPolicy = new Fdc3CamelCaseNamingPolicy()
};
}
Expand Down
43 changes: 43 additions & 0 deletions src/Fdc3.Json/Serialization/IntentsConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Morgan Stanley makes this available to you under the Apache License,
* Version 2.0 (the "License"). You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.
*
* See the NOTICE file distributed with this work for additional information
* regarding copyright ownership. Unless required by applicable law or agreed
* to in writing, software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/

using Finos.Fdc3.AppDirectory;
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Finos.Fdc3.Json.Serialization
{
public class IntentsConverter : JsonConverter<Intents>
{
public override Intents? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var result = JsonSerializer.Deserialize<Intents>(ref reader, Fdc3JsonSerializerOptions.CreateWithoutConverters());
if (result?.ListensFor != null)
{
foreach (var intentName in result.ListensFor.Keys)
{
result.ListensFor[intentName].Name = intentName;
}
}

return result;
}

public override void Write(Utf8JsonWriter writer, Intents value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ public Fdc3JsonSerializerSettings()
{
NamingStrategy = new Fdc3CamelCaseNamingStrategy()
};
this.Converters = new JsonConverter[] { new StringEnumConverter(new CamelCaseNamingStrategy()), new RecipientJsonConverter(), new Fdc3AppConverter() };
this.Converters = new JsonConverter[]
{
new StringEnumConverter(new CamelCaseNamingStrategy()),
new RecipientJsonConverter(),
new Fdc3AppConverter(),
new IntentsConverter()
};
}
}
}
52 changes: 52 additions & 0 deletions src/Fdc3.NewtonsoftJson/Serialization/IntentsConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Morgan Stanley makes this available to you under the Apache License,
* Version 2.0 (the "License"). You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.
*
* See the NOTICE file distributed with this work for additional information
* regarding copyright ownership. Unless required by applicable law or agreed
* to in writing, software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/

using Finos.Fdc3.AppDirectory;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;

namespace Finos.Fdc3.NewtonsoftJson.Serialization
{
public class IntentsConverter : JsonConverter<Intents>
{
public override bool CanWrite => false;

public override Intents? ReadJson(JsonReader reader, Type objectType, Intents? existingValue, bool hasExistingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
{
return null;
}

var obj = JObject.Load(reader);
var result = obj.ToObject(typeof(Intents)) as Intents;

if (result?.ListensFor != null)
{
foreach (var intentName in result.ListensFor.Keys)
{
result.ListensFor[intentName].Name = intentName;
}
}

return result;
}

public override void WriteJson(JsonWriter writer, Intents? value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ protected void ValidateApp(Fdc3App app)
Assert.Contains("fdc3.instrument", app.Interop!.Intents!.ListensFor!["myApp.GetPrice"].Contexts!);
Assert.Equal("Get Price", app.Interop!.Intents!.ListensFor!["myApp.GetPrice"].DisplayName);
Assert.Equal("myApp.quote", app.Interop!.Intents!.ListensFor!["myApp.GetPrice"].ResultType);
Assert.Equal("ViewChart", app.Interop!.Intents!.ListensFor!["ViewChart"].Name);
Assert.Equal("myApp.GetPrice", app.Interop!.Intents!.ListensFor!["myApp.GetPrice"].Name);
Assert.Contains("ViewOrders", app.Interop.Intents.Raises!.Keys);
Assert.Contains("fdc3.instrument", app.Interop.Intents.Raises!["ViewOrders"]);
Assert.Contains("StartEmail", app.Interop.Intents.Raises.Keys);
Expand Down

0 comments on commit dff2f32

Please sign in to comment.