in src/Custom/Moderations/ModerationResult.Serialization.cs [110:191]
internal static ModerationResult DeserializeModerationResult(JsonElement element, ModelReaderWriterOptions options = null)
{
options ??= ModelSerializationExtensions.WireOptions;
if (element.ValueKind == JsonValueKind.Null)
{
return null;
}
bool flagged = default;
InternalModerationCategories internalCategories = default;
InternalModerationCategoryScores internalCategoryScores = default;
InternalCreateModerationResponseResultCategoryAppliedInputTypes internalAppliedInputTypes = default;
IDictionary<string, BinaryData> serializedAdditionalRawData = default;
Dictionary<string, BinaryData> rawDataDictionary = new Dictionary<string, BinaryData>();
foreach (var property in element.EnumerateObject())
{
if (property.NameEquals("flagged"u8))
{
flagged = property.Value.GetBoolean();
continue;
}
if (property.NameEquals("categories"u8))
{
internalCategories = InternalModerationCategories.DeserializeInternalModerationCategories(property.Value, options);
continue;
}
if (property.NameEquals("category_scores"u8))
{
internalCategoryScores = InternalModerationCategoryScores.DeserializeInternalModerationCategoryScores(property.Value, options);
continue;
}
if (property.NameEquals("category_applied_input_types"u8))
{
internalAppliedInputTypes = InternalCreateModerationResponseResultCategoryAppliedInputTypes.DeserializeInternalCreateModerationResponseResultCategoryAppliedInputTypes(property.Value, options);
continue;
}
if (true)
{
rawDataDictionary ??= new Dictionary<string, BinaryData>();
rawDataDictionary.Add(property.Name, BinaryData.FromString(property.Value.GetRawText()));
}
}
ModerationCategory MakeCategory<T>(
Func<InternalModerationCategories, bool> categoryFlaggedGetter,
Func<InternalModerationCategoryScores, float> scoreGetter,
Func<
InternalCreateModerationResponseResultCategoryAppliedInputTypes,
IReadOnlyList<T>
> internalAppliedInputTypesGetter)
{
IReadOnlyList<T> genericInputTypes
= internalAppliedInputTypes is null ? null
: internalAppliedInputTypesGetter.Invoke(internalAppliedInputTypes);
IReadOnlyList<string> stringInputTypes =
(genericInputTypes as IReadOnlyList<string>)
?? genericInputTypes?.Select(t => t.ToString()).ToList().AsReadOnly();
return new ModerationCategory(
categoryFlaggedGetter.Invoke(internalCategories),
scoreGetter.Invoke(internalCategoryScores),
ModerationApplicableInputKindsExtensions.FromInternalApplicableInputKinds(stringInputTypes));
}
serializedAdditionalRawData = rawDataDictionary;
return new ModerationResult(
flagged: flagged,
hate: MakeCategory(cats => cats.Hate, catScores => catScores.Hate, types => types.Hate),
hateThreatening: MakeCategory(cats => cats.HateThreatening, catScores => catScores.HateThreatening, types => types.HateThreatening),
harassment: MakeCategory(cats => cats.Harassment, catScores => catScores.Harassment, types => types.Harassment),
harassmentThreatening: MakeCategory(cats => cats.HarassmentThreatening, catScores => catScores.HarassmentThreatening, types => types.HarassmentThreatening),
illicit: MakeCategory(cats => cats.Illicit, catScores => catScores.Illicit, types => types.Illicit),
illicitViolent: MakeCategory(cats => cats.IllicitViolent, catScores => catScores.IllicitViolent, types => types.IllicitViolent),
selfHarm: MakeCategory(cats => cats.SelfHarm, catScores => catScores.SelfHarm, types => types.SelfHarm),
selfHarmIntent: MakeCategory(cats => cats.SelfHarmIntent, catScores => catScores.SelfHarmIntent, types => types.SelfHarmIntent),
selfHarmInstructions: MakeCategory(cats => cats.SelfHarmInstructions, catScores => catScores.SelfHarmInstructions, types => types.SelfHarmInstructions),
sexual: MakeCategory(cats => cats.Sexual, catScores => catScores.Sexual, types => types.Sexual),
sexualMinors: MakeCategory(cats => cats.SexualMinors, catScores => catScores.SexualMinors, types => types.SexualMinors),
violence: MakeCategory(cats => cats.Violence, catScores => catScores.Violence, types => types.Violence),
violenceGraphic: MakeCategory(cats => cats.ViolenceGraphic, catScores => catScores.ViolenceGraphic, types => types.ViolenceGraphic),
serializedAdditionalRawData: serializedAdditionalRawData);
}