OK so two things going on here.
First I am getting this when the ListBox items gets added. "OK - System.Collections.Generic.List`1[System.String] - System.Collections.Generic.List`1[ClassLibraryApi.Report]"
Other times it loads nothing and this is the exception it pulls. "System.NullReferenceException: 'Object reference not set to an instance of an object.'"
This section pulls down reports depending on what is selected for the report.
I am thinking both are related to each other but can not figure this out.
Winform code for the listbox
And this is my class
and what postman gets.
First I am getting this when the ListBox items gets added. "OK - System.Collections.Generic.List`1[System.String] - System.Collections.Generic.List`1[ClassLibraryApi.Report]"
Other times it loads nothing and this is the exception it pulls. "System.NullReferenceException: 'Object reference not set to an instance of an object.'"
This section pulls down reports depending on what is selected for the report.
I am thinking both are related to each other but can not figure this out.
Winform code for the listbox
winform:
{
ReportViewlistbox.Items.Clear();
GetReport p = new GetReport();
var client = new RestClient(p.WebGetReport);
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "Value");
request.AddHeader("Content-Type", "Value");
var thisJsonStr = "{\"Credentials\":{\"ApiKey\":\"string (CompactGUID)\",\"MerchantID\":\"string\",\"StoreID\":\"string\",\"Username\":\"string\",\"Password\":\"string\"}}", ParameterType.RequestBody);
request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };
request.AddParameter("application/json", thisJsonStr, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
var viewreport = Viewreport.FromJson(response.Content);
ReportViewlistbox.Items.Add($"{viewreport.Result} - {viewreport.ReportHeader} - {viewreport.Reports}");
}
And this is my class
Class:
public partial class Viewreport
{
[JsonProperty("Result")]
public string Result { get; set; }
[JsonProperty("ReportHeader")]
public List<string> ReportHeader { get; set; }
[JsonProperty("Reports")]
public List<Report> Reports { get; set; }
}
public partial class Report
{
[JsonProperty("ReportID")]
[JsonConverter(typeof(ParseStringConverter))]
public long ReportId { get; set; }
[JsonProperty("ReportTxt")]
public List<string> ReportTxt { get; set; }
}
public partial class Viewreport
{
public static Viewreport FromJson(string json) => JsonConvert.DeserializeObject<Viewreport>(json, ClassLibraryApi.Converter.Settings);
}
public static class Serialize
{
public static string ToJson(this Viewreport self) => JsonConvert.SerializeObject(self, ClassLibraryApi.Converter.Settings);
}
internal static class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
Converters =
{
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
},
};
}
internal class ParseStringConverter : JsonConverter
{
public override bool CanConvert(Type t) => t == typeof(long) || t == typeof(long?);
public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null) return null;
var value = serializer.Deserialize<string>(reader);
long l;
if (Int64.TryParse(value, out l))
{
return l;
}
throw new Exception("Cannot unmarshal type long");
}
public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)
{
if (untypedValue == null)
{
serializer.Serialize(writer, null);
return;
}
var value = (long)untypedValue;
serializer.Serialize(writer, value.ToString());
return;
}
public static readonly ParseStringConverter Singleton = new ParseStringConverter();
}
and what postman gets.
Postman:
{
"Result": "OK",
"ReportHeader": [
" Company: Pizza Schmizza - Pearl",
" Run Time: 02/05/21 09:27AM",
"Reporting On: Current #1"
],
"Reports": [
{
"ReportID": "34",
"ReportTxt": [
" ",
"REPORT GROUP: MANDY REPORT",
"--------------------------------",
" ",
"REPORT PRESET: Employee Hours",
" ",
" ",
"EMPLOYEE SHIFTS:",
" ",
" Employee Job Time In Time Out Reg OT Hours Pay ",
"----------------|--------------|---------------|---------------|-------|-------|-------|---------",
"F, Tanner |MANAGER |02/05/21 08:32A|02/05/21 09:27A| 0.93| 0.00| 0.93| 15.14",
"----------------|--------------|---------------|---------------|-------|-------|-------|---------",
" | | | | 0.93| 0.00| 0.93| 15.14"
]
}
]
}