Skip to content

Commit

Permalink
Update JSON.
Browse files Browse the repository at this point in the history
  • Loading branch information
kingcean committed May 5, 2024
1 parent 2aab30b commit d29d15c
Show file tree
Hide file tree
Showing 6 changed files with 378 additions and 31 deletions.
10 changes: 10 additions & 0 deletions Core/Data/Result/ConciseModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Runtime.Serialization;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
Expand Down Expand Up @@ -363,6 +364,15 @@ public bool ContainKeyword(string value, bool ignoreCase = false)
return false;
}

/// <summary>
/// Converts to JSON object.
/// </summary>
/// <param name="options">Options to control the reader behavior during parsing.</param>
/// <returns>A JSON object instance.</returns>
/// <exception cref="JsonException">Its property does not represent a valid single JSON object.</exception>
public virtual JsonObjectNode ToJson(JsonSerializerOptions options = default)
=> JsonObjectNode.ConvertFrom(this, options);

/// <summary>
/// Returns a string that represents the current model.
/// </summary>
Expand Down
59 changes: 59 additions & 0 deletions Core/Data/Result/DataResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,14 @@ public JsonDataResult(JsonObjectNode data, string message)
[Description("The additional information of the result.")]
public JsonObjectNode AdditionalInfo { get; set; }

/// <summary>
/// Gets or sets the components.
/// </summary>
[DataMember(Name = "components")]
[JsonPropertyName("components")]
[Description("The components for reference.")]
public JsonObjectNode Components { get; set; }

/// <summary>
/// Gets a value indicating whether the data is null.
/// </summary>
Expand Down Expand Up @@ -345,6 +353,57 @@ public IJsonDataNode TryGetValue(string key, string subKey, params string[] keyP
/// <exception cref="ArgumentOutOfRangeException">The property does not exist.</exception>
public IJsonDataNode TryGetValue(ReadOnlySpan<char> key)
=> Data?.TryGetValue(key);

/// <summary>
/// Gets schema.
/// </summary>
/// <param name="key">The schema key.</param>
/// <returns>The schema information.</returns>
public JsonObjectNode GetSchema(string key)
=> string.IsNullOrWhiteSpace(key) ? null : GetComponent("schemas")?.TryGetObjectValue(key);

/// <summary>
/// Gets reference object.
/// </summary>
/// <param name="type">The type or group key of the resource.</param>
/// <param name="id">The identifier of the object.</param>
/// <returns>The reference object.</returns>
public JsonObjectNode GetReferenceObject(string type, string id)
{
if (string.IsNullOrWhiteSpace(id)) return null;
var components = GetComponents();
if (components == null) return null;
var valueKind = components.GetValueKind(type);
var json = valueKind switch
{
JsonValueKind.Object => components.TryGetObjectValue(type)?.TryGetObjectValue(id),
JsonValueKind.Array => components.TryGetArrayValue(type)?.TryGetObjectValueById(id),
_ => null,
};
if (json == null) return null;
var refPath = json.TryGetStringValue("$ref");
if (refPath == null || json.Count != 1) return json;
return JsonObjectNode.TryGetRefObjectValue(Data, json, ToJson());
}

internal JsonObjectNode GetComponents()
=> Components ?? Data?.TryGetObjectValue("components");

internal JsonObjectNode GetComponent(string key)
{
var dict = GetComponents();
return string.IsNullOrWhiteSpace(key) ? dict : dict?.TryGetObjectValue(key);
}

internal JsonObjectNode ToJson()
=> new()
{
{ "track", TrackingId },
{ "message", Message },
{ "data", Data },
{ "info", AdditionalInfo },
{ "components", Components }
};
}

/// <summary>
Expand Down
14 changes: 14 additions & 0 deletions Core/Net/Http/JsonHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@

namespace Trivial.Net;

/// <summary>
/// The maker to create JSON HTTP client.
/// </summary>
public interface IJsonHttpClientMaker
{
/// <summary>
/// Creates a JSON HTTP client.
/// </summary>
/// <typeparam name="T">The type of response.</typeparam>
/// <param name="callback">An optional callback raised on data received.</param>
/// <returns>A new JSON HTTP client.</returns>
JsonHttpClient<T> Create<T>(Action<ReceivedEventArgs<T>> callback = null);
}

/// <summary>
/// The event arguments on sending.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions Core/Security/Token/OAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Trivial.Security;
/// <para>The OAuth HTTP web client (for RFC-6749).</para>
/// <para>You can use this to login and then create the JSON HTTP web clients with the authentication information.</para>
/// </summary>
public class OAuthClient : TokenContainer
public class OAuthClient : TokenContainer, IJsonHttpClientMaker
{
/// <summary>
/// The app accessing key instance.
Expand Down Expand Up @@ -699,7 +699,7 @@ private HttpClient CreateHttpClient()
/// <summary>
/// The OAuth based JSON HTTP web client.
/// </summary>
public abstract class OAuthBasedClient : TokenContainer
public abstract class OAuthBasedClient : TokenContainer, IJsonHttpClientMaker
{
/// <summary>
/// The OAuth client.
Expand Down
Loading

0 comments on commit d29d15c

Please sign in to comment.