首页 > 解决方案 > Marketo - Response of Bulk API serialization

问题描述

I'm trying to use the Bulk Import API endpoint within Marketo to fetch the warnings and failures associated with a bulk job I created within Marketo.

The output of these jobs is stated as an ObservableOfInputStreamContent, yet the response of these endpoints returns a csv string (with header column), that's not a JSON object. To compound to this problem, we are using the generated swagger files with the swagger definition file provided by Marketo. These generated c# client side files have the ObservableOfInputStreamContent object, but it's an empty object. I'm not sure if this is intended, or a mistake on Marketo's side. The generated files will attempt to use Newtonsoft.Json.JsonConvert.DeserializeObject<ObservableOfInputStreamContent>(responseText, JsonSerializerSettings); to deserialize the API response to the ObservableOfInputStreamContent.

Generated code that deserializes the API response:

var responseText = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
try
{
    var typedBody = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(responseText, JsonSerializerSettings);
    return new ObjectResponseResult<T>(typedBody, responseText);
}
catch (Newtonsoft.Json.JsonException exception)
{
    var message = "Could not deserialize the response body string as " + typeof(T).FullName + ".";
    throw new ApiException(message, (int)response.StatusCode, responseText, headers, exception);
}

Problem one is that the API does not return JSON to begin with (eg):

address,city,country,
123 lane, new york, USA
745 street, new york, USA

This call will return this error because of this:

Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: a. Path '', line 0, position 0.

The second issue is that ObservableOfInputStreamContent is defined as an empty object to begin with in the generated files. So if the API response was valid JSON, I don't think it would know how to convert to that empty ObservableOfInputStreamContent object. The good news about the generated code is that it gives me an option to extend the ObservableOfInputStreamContent because it's defined as a partial class.

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.1.11.0 (Newtonsoft.Json v11.0.0.0)")]
public partial class ObservableOfInputStreamContent
{

}

That said, is there any possible way I can use the JsonSerializerSettings to get around this issue? Could I extended the ObservableOfInputStreamContent class to hold a string property and then create my own JsonConverter to convert the string returned from the API into the new ObservableOfInputStreamContent?

标签: c#serializationjson.netmarketo

解决方案


推荐阅读