@@ -136,7 +136,7 @@ public override async Task<CloudEvent> DecodeStructuredModeMessageAsync(Stream b
136
136
137
137
var jsonReader = CreateJsonReader ( body , MimeUtilities . GetEncoding ( contentType ) ) ;
138
138
var jObject = await JObject . LoadAsync ( jsonReader ) . ConfigureAwait ( false ) ;
139
- return DecodeJObject ( jObject , extensionAttributes ) ;
139
+ return DecodeJObject ( jObject , extensionAttributes , nameof ( body ) ) ;
140
140
}
141
141
142
142
/// <inheritdoc />
@@ -146,7 +146,7 @@ public override CloudEvent DecodeStructuredModeMessage(Stream body, ContentType?
146
146
147
147
var jsonReader = CreateJsonReader ( body , MimeUtilities . GetEncoding ( contentType ) ) ;
148
148
var jObject = JObject . Load ( jsonReader ) ;
149
- return DecodeJObject ( jObject , extensionAttributes ) ;
149
+ return DecodeJObject ( jObject , extensionAttributes , nameof ( body ) ) ;
150
150
}
151
151
152
152
/// <inheritdoc />
@@ -177,14 +177,23 @@ public override IReadOnlyList<CloudEvent> DecodeBatchModeMessage(Stream body, Co
177
177
public override IReadOnlyList < CloudEvent > DecodeBatchModeMessage ( ReadOnlyMemory < byte > body , ContentType ? contentType , IEnumerable < CloudEventAttribute > ? extensionAttributes ) =>
178
178
DecodeBatchModeMessage ( BinaryDataUtilities . AsStream ( body ) , contentType , extensionAttributes ) ;
179
179
180
+ /// <summary>
181
+ /// Converts the given <see cref="JObject"/> into a <see cref="CloudEvent"/>.
182
+ /// </summary>
183
+ /// <param name="jObject">The JSON representation of a CloudEvent. Must not be null.</param>
184
+ /// <param name="extensionAttributes">The extension attributes to use when populating the CloudEvent. May be null.</param>
185
+ /// <returns>The SDK representation of the CloudEvent.</returns>
186
+ public CloudEvent ConvertFromJObject ( JObject jObject , IEnumerable < CloudEventAttribute > ? extensionAttributes ) =>
187
+ DecodeJObject ( Validation . CheckNotNull ( jObject , nameof ( jObject ) ) , extensionAttributes , nameof ( jObject ) ) ;
188
+
180
189
private IReadOnlyList < CloudEvent > DecodeJArray ( JArray jArray , IEnumerable < CloudEventAttribute > ? extensionAttributes , string paramName )
181
190
{
182
191
List < CloudEvent > events = new List < CloudEvent > ( jArray . Count ) ;
183
192
foreach ( var token in jArray )
184
193
{
185
194
if ( token is JObject obj )
186
195
{
187
- events . Add ( DecodeJObject ( obj , extensionAttributes ) ) ;
196
+ events . Add ( DecodeJObject ( obj , extensionAttributes , paramName ) ) ;
188
197
}
189
198
else
190
199
{
@@ -194,7 +203,7 @@ private IReadOnlyList<CloudEvent> DecodeJArray(JArray jArray, IEnumerable<CloudE
194
203
return events ;
195
204
}
196
205
197
- private CloudEvent DecodeJObject ( JObject jObject , IEnumerable < CloudEventAttribute > ? extensionAttributes )
206
+ private CloudEvent DecodeJObject ( JObject jObject , IEnumerable < CloudEventAttribute > ? extensionAttributes , string paramName )
198
207
{
199
208
if ( ! jObject . TryGetValue ( CloudEventsSpecVersion . SpecVersionAttribute . Name , out var specVersionToken )
200
209
|| specVersionToken . Type != JTokenType . String )
@@ -207,9 +216,7 @@ private CloudEvent DecodeJObject(JObject jObject, IEnumerable<CloudEventAttribut
207
216
var cloudEvent = new CloudEvent ( specVersion , extensionAttributes ) ;
208
217
PopulateAttributesFromStructuredEvent ( cloudEvent , jObject ) ;
209
218
PopulateDataFromStructuredEvent ( cloudEvent , jObject ) ;
210
- // "data" is always the parameter from the public method. It's annoying not to be able to use
211
- // nameof here, but this will give the appropriate result.
212
- return Validation . CheckCloudEventArgument ( cloudEvent , "data" ) ;
219
+ return Validation . CheckCloudEventArgument ( cloudEvent , paramName ) ;
213
220
}
214
221
215
222
private void PopulateAttributesFromStructuredEvent ( CloudEvent cloudEvent , JObject jObject )
@@ -395,6 +402,19 @@ public override ReadOnlyMemory<byte> EncodeStructuredModeMessage(CloudEvent clou
395
402
return stream . ToArray ( ) ;
396
403
}
397
404
405
+ /// <summary>
406
+ /// Converts the given <see cref="CloudEvent"/> to a <see cref="JObject"/> containing the structured mode JSON format representation
407
+ /// of the event.
408
+ /// </summary>
409
+ /// <param name="cloudEvent">The event to convert. Must not be null.</param>
410
+ /// <returns>A <see cref="JObject"/> containing the structured mode JSON format representation of the event.</returns>
411
+ public JObject ConvertToJObject ( CloudEvent cloudEvent )
412
+ {
413
+ var writer = new JTokenWriter ( ) ;
414
+ WriteCloudEventForBatchOrStructuredMode ( writer , cloudEvent ) ;
415
+ return ( JObject ) writer . Token ! ;
416
+ }
417
+
398
418
/// <inheritdoc />
399
419
public override ReadOnlyMemory < byte > EncodeBatchModeMessage ( IEnumerable < CloudEvent > cloudEvents , out ContentType contentType )
400
420
{
0 commit comments