Home > front end >  Servicestack return array instead of object with an array
Servicestack return array instead of object with an array

Time:05-20

I have a servicestack POCO object

public class SiteCalendarItem
{
    [DataMember(Name = "title")]
    public string Title { get; set; }
    [DataMember(Name = "start")]
    public string StartD { get; set; } //DateTime
    [DataMember(Name = "end")]
    public string EndD { get; set; }
    /***/

}

Which is returned as a JSON object when the webservice is called

public object Get(GetFullCalendarRequest request)
{
    CalendarItemList tmp = new CalendarItemList();
    return new GetFullCalendarResponse { Result = tmp.ReturnObject() };
}

This returns an object with an array of calendar objects.

{"Result":[{"title":"test 1","start":"2019-09-08","end":"2019-09-10"}]}

I want to give this as a feed for a website which makes use of the "Fullcalendar" plugin. Now the issue is the fullcalendar plugin expects data in the form of:

[{"title":"All Day Event","start":"2022-05-01"},{"title":"Long Event","start":"2022-05-07","end":"2022-05-10"},{"groupId":"999","title":"Repeating Event","start":"2022-05-09T16:00:00 00:00"}]

This is given as a "fixed" eventfeed (events: 'http://127.0.0.1:8089/GetFullCalendar?format=json') of which I suspect that I cannot pass configuration parameters.

So how can I make sure that the Servicestack service doesnt return an object with an array but directly the array?

I did my google-homework but unless if I missed it it isn't straightforward (for me).

CodePudding user response:

You can just return the naked array, e.g:

public object Get(GetFullCalendarRequest request) => 
    new CalendarItemList().ReturnObject();

I'd also recommend annotating what your API returns to clients with:

public class GetFullCalendarRequest : IReturn<SiteCalendarItem[]> { ... }
  • Related