Minor tweaks to System.Text.Json event formatter
- Make PopulateDataFromStructuredEvent clearer via new local variables - Make DecodeStructuredModeMessageAsync async rather than returning the task directly Signed-off-by: Jon Skeet <jonskeet@google.com>
This commit is contained in:
parent
d840bc26c9
commit
08593b80a7
|
@ -108,8 +108,8 @@ namespace CloudNative.CloudEvents.SystemTextJson
|
||||||
DocumentOptions = documentOptions;
|
DocumentOptions = documentOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Task<CloudEvent> DecodeStructuredModeMessageAsync(Stream data, ContentType contentType, IEnumerable<CloudEventAttribute> extensionAttributes) =>
|
public override async Task<CloudEvent> DecodeStructuredModeMessageAsync(Stream data, ContentType contentType, IEnumerable<CloudEventAttribute> extensionAttributes) =>
|
||||||
DecodeStructuredModeMessageImpl(data, contentType, extensionAttributes, true);
|
await DecodeStructuredModeMessageImpl(data, contentType, extensionAttributes, true).ConfigureAwait(false);
|
||||||
|
|
||||||
public override CloudEvent DecodeStructuredModeMessage(Stream data, ContentType contentType, IEnumerable<CloudEventAttribute> extensionAttributes) =>
|
public override CloudEvent DecodeStructuredModeMessage(Stream data, ContentType contentType, IEnumerable<CloudEventAttribute> extensionAttributes) =>
|
||||||
DecodeStructuredModeMessageImpl(data, contentType, extensionAttributes, false).GetAwaiter().GetResult();
|
DecodeStructuredModeMessageImpl(data, contentType, extensionAttributes, false).GetAwaiter().GetResult();
|
||||||
|
@ -240,31 +240,26 @@ namespace CloudNative.CloudEvents.SystemTextJson
|
||||||
{
|
{
|
||||||
// Fetch data and data_base64 tokens, and treat null as missing.
|
// Fetch data and data_base64 tokens, and treat null as missing.
|
||||||
document.RootElement.TryGetProperty(DataPropertyName, out var dataElement);
|
document.RootElement.TryGetProperty(DataPropertyName, out var dataElement);
|
||||||
if (dataElement is JsonElement { ValueKind: JsonValueKind.Null })
|
document.RootElement.TryGetProperty(DataBase64PropertyName, out var dataBase64Element);
|
||||||
{
|
|
||||||
dataElement = new JsonElement();
|
bool dataPresent = dataElement.ValueKind != JsonValueKind.Null && dataElement.ValueKind != JsonValueKind.Undefined;
|
||||||
}
|
bool dataBase64Present = dataBase64Element.ValueKind != JsonValueKind.Null && dataBase64Element.ValueKind != JsonValueKind.Undefined;
|
||||||
document.RootElement.TryGetProperty(DataBase64PropertyName, out var dataBase64Token);
|
|
||||||
if (dataBase64Token is JsonElement { ValueKind: JsonValueKind.Null })
|
|
||||||
{
|
|
||||||
dataBase64Token = new JsonElement();
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we don't have any data, we're done.
|
// If we don't have any data, we're done.
|
||||||
if (dataElement.ValueKind == JsonValueKind.Undefined && dataBase64Token.ValueKind == JsonValueKind.Undefined)
|
if (!dataPresent && !dataBase64Present)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// We can't handle both properties being set.
|
// We can't handle both properties being set.
|
||||||
if (dataElement.ValueKind != JsonValueKind.Undefined && dataBase64Token.ValueKind != JsonValueKind.Undefined)
|
if (dataPresent && dataBase64Present)
|
||||||
{
|
{
|
||||||
throw new ArgumentException($"Structured mode content cannot contain both '{DataPropertyName}' and '{DataBase64PropertyName}' properties.");
|
throw new ArgumentException($"Structured mode content cannot contain both '{DataPropertyName}' and '{DataBase64PropertyName}' properties.");
|
||||||
}
|
}
|
||||||
// Okay, we have exactly one non-null data/data_base64 property.
|
// Okay, we have exactly one non-null data/data_base64 property.
|
||||||
// Decode it, potentially using overridden methods for specialization.
|
// Decode it, potentially using overridden methods for specialization.
|
||||||
if (dataBase64Token.ValueKind != JsonValueKind.Undefined)
|
if (dataBase64Present)
|
||||||
{
|
{
|
||||||
DecodeStructuredModeDataBase64Property(dataBase64Token, cloudEvent);
|
DecodeStructuredModeDataBase64Property(dataBase64Element, cloudEvent);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue