-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
45b8d6a
commit 5c18d93
Showing
3 changed files
with
85 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,7 @@ | |
|
||
namespace LEGO.AsyncAPI.Models | ||
{ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.Linq; | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
using LEGO.AsyncAPI.Models.Interfaces; | ||
|
@@ -26,19 +23,27 @@ public class AsyncApiAny : IAsyncApiElement, IAsyncApiExtension | |
private JsonNode node; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="AsyncApiAny"/> class. | ||
/// Initializes a new instance of the <see cref="AsyncApiAny" /> class. | ||
/// </summary> | ||
/// <param name="node">The node.</param> | ||
public AsyncApiAny(JsonNode node) | ||
{ | ||
this.node = node; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="AsyncApiAny"/> class. | ||
/// </summary> | ||
/// <param name="obj">The object.</param> | ||
public AsyncApiAny(object obj) | ||
{ | ||
this.node = JsonNode.Parse(JsonSerializer.Serialize(obj, this.options)); | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="AsyncApiAny"/> class. | ||
/// </summary> | ||
/// <param name="dictionary">The dictionary.</param> | ||
public AsyncApiAny(Dictionary<string, AsyncApiAny> dictionary) | ||
{ | ||
var jsonObject = new JsonObject(); | ||
|
@@ -50,6 +55,10 @@ public AsyncApiAny(Dictionary<string, AsyncApiAny> dictionary) | |
this.node = jsonObject; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="AsyncApiAny"/> class. | ||
/// </summary> | ||
/// <param name="list">The list.</param> | ||
public AsyncApiAny(List<object> list) | ||
{ | ||
var jsonArray = new JsonArray(); | ||
|
@@ -62,6 +71,10 @@ public AsyncApiAny(List<object> list) | |
this.node = jsonArray; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="AsyncApiAny"/> class. | ||
/// </summary> | ||
/// <param name="dictionary">The dictionary.</param> | ||
public AsyncApiAny(Dictionary<string, object> dictionary) | ||
{ | ||
var jsonObject = new JsonObject(); | ||
|
@@ -74,6 +87,7 @@ public AsyncApiAny(Dictionary<string, object> dictionary) | |
this.node = jsonObject; | ||
} | ||
|
||
Check warning on line 89 in src/LEGO.AsyncAPI/Models/Any/AsyncApiAny.cs GitHub Actions / build (windows-latest)
Check warning on line 89 in src/LEGO.AsyncAPI/Models/Any/AsyncApiAny.cs GitHub Actions / build (macos-latest)
|
||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="AsyncApiAny"/> class. | ||
/// </summary> | ||
|
@@ -92,19 +106,66 @@ public AsyncApiAny(JsonObject node) | |
this.node = node; | ||
} | ||
|
||
/// <summary> | ||
/// Converts to <see cref="{T}" /> from an Extension. | ||
/// </summary> | ||
/// <typeparam name="T">T.</typeparam> | ||
/// <param name="extension">The extension.</param> | ||
/// <returns><see cref="{T}"/>.</returns> | ||
public static T FromExtension<T>(IAsyncApiExtension extension) | ||
{ | ||
if (extension is AsyncApiAny any) | ||
{ | ||
return any.GetValueOrDefault<T>(); | ||
} | ||
else | ||
{ | ||
return default(T); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets the node. | ||
/// </summary> | ||
/// <returns><see cref="JsonNode"/>.</returns> | ||
/// <value> | ||
/// The node. | ||
/// </value> | ||
public JsonNode GetNode() => this.node; | ||
|
||
/// <summary> | ||
/// Gets the value. | ||
/// </summary> | ||
/// <typeparam name="T"><see cref="{T}" />.</typeparam> | ||
/// <returns><see cref="{T}" />.</returns> | ||
public T GetValue<T>() | ||
{ | ||
return JsonSerializer.Deserialize<T>(this.node.ToJsonString()); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the value or default. | ||
/// </summary> | ||
/// <typeparam name="T"><see cref="{T}" />.</typeparam> | ||
/// <returns><see cref="{T}" /> or default.</returns> | ||
public T GetValueOrDefault<T>() | ||
{ | ||
try | ||
{ | ||
return this.GetValue<T>(); | ||
} | ||
catch (System.Exception) | ||
{ | ||
return default(T); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Tries the get value. | ||
/// </summary> | ||
/// <typeparam name="T"><see cref="{T}" />.</typeparam> | ||
/// <param name="value">The value.</param> | ||
/// <returns>true if the value could be converted, otherwise false.</returns> | ||
public bool TryGetValue<T>(out T value) | ||
{ | ||
try | ||
|
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