forked from finos/fdc3-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request finos#128 from ztanczos/fdc3_intents
Add custom JsonConverters for Intents
- Loading branch information
Showing
5 changed files
with
114 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters