-
Notifications
You must be signed in to change notification settings - Fork 2
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
321b017
commit 634b686
Showing
5 changed files
with
63 additions
and
8 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
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,33 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using AssemblyAI.Lemur; | ||
|
||
namespace AssemblyAI.Core; | ||
|
||
public class LemurResponseConverter : JsonConverter<LemurResponse> | ||
{ | ||
public override LemurResponse? Read( | ||
ref Utf8JsonReader reader, | ||
Type typeToConvert, | ||
JsonSerializerOptions options | ||
) | ||
{ | ||
if (reader.TokenType is JsonTokenType.Null) | ||
return default; | ||
|
||
var jsonElement = JsonSerializer.Deserialize<JsonElement>(ref reader, options); | ||
if (jsonElement.GetProperty("response").ValueKind == JsonValueKind.Array) | ||
{ | ||
return jsonElement.Deserialize<LemurQuestionAnswerResponse>() ?? | ||
throw new Exception("Failed to deserialize LemurQuestionAnswerResponse"); | ||
} | ||
|
||
return jsonElement.Deserialize<LemurStringResponse>() ?? | ||
throw new Exception("Failed to deserialize LemurStringResponse"); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, LemurResponse value, JsonSerializerOptions options) | ||
{ | ||
JsonSerializer.Serialize(writer, value.Value, options); | ||
} | ||
} |
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,22 @@ | ||
using System.Text.Json.Serialization; | ||
using AssemblyAI.Core; | ||
using OneOf; | ||
|
||
namespace AssemblyAI.Lemur; | ||
|
||
[JsonConverter(typeof(LemurResponseConverter))] | ||
public class LemurResponse : OneOfBase<LemurStringResponse, LemurQuestionAnswerResponse> | ||
{ | ||
private LemurResponse(OneOf<LemurStringResponse, LemurQuestionAnswerResponse> response) : base(response) | ||
{ | ||
} | ||
|
||
public static implicit operator LemurResponse(OneOf<LemurStringResponse, LemurQuestionAnswerResponse> _) => new(_); | ||
public static implicit operator LemurResponse(LemurStringResponse _) => new(_); | ||
public static implicit operator LemurResponse(LemurQuestionAnswerResponse _) => new(_); | ||
|
||
public bool IsLemurStringResponse => IsT0; | ||
public bool IsLemurQuestionAnswerResponse => IsT1; | ||
public LemurStringResponse AsLemurStringResponse => AsT0; | ||
public LemurQuestionAnswerResponse AsLemurQuestionAnswerResponse => AsT1; | ||
} |