Search code examples
c#jsonebay-api

C# repeating the same field name in class to allow serializing to JSON


I am trying to build a class which I serialize to become the body of a web request.

The output I'm trying to achieve when serializing the object is :

{
  "findCompletedItemsRequest": {
    "keywords": "searchtext",
    "itemFilter": [
      {
        "name": "SoldItemsOnly",
        "value": "true"
      }
    ],
    "outputSelector": "PictureURLLarge",
    "outputSelector": "SellerInfo",
    "paginationInput": {
      "entriesPerPage": "100",
      "pageNumber": "1"
    }
  }
}

For the field 'outputSelector', I can have it in there 0, 1 or many times. How would I define this in my class ? I have tried searching but I'm unsure what I'm trying to achieve may be called. If I use a list or array it just creates outputSelect JSON property that has an array in it, not multiple outputSelector properties.

Here is what I have so far (other critique welcomed as I'm from a procedural programming background)

public class eBaySearchBody
        {
            public FindCompletedItemsRequest findCompletedItemsRequest = new FindCompletedItemsRequest();
            public class FindCompletedItemsRequest
            {
                public string keywords { get; set; }
                public List<itemFilters> itemFilter { get; set; }
                public string sortOrder { get; set; }
                public PaginationInput paginationInput = new PaginationInput();

            }
            public class PaginationInput
            {
                public string entriesPerPage { get; set; }
                public string pageNumber { get; set; }
            }
            public class itemFilters
            {
                public string name { get; set; }
                public string value { get; set; }
            }
        }

Solution

  • It's not a valid JSON. It has SyntaxError: Duplicate key 'outputSelector' on line 9.

    enter image description here

    If you consider using an array of outputSelector, the code needs to be modified as below to produce valid JSON. Sample Valid JSON:

    {
        "findCompletedItemsRequest": {
            "keywords": "searchtext",
            "itemFilter": [{
                "name": "SoldItemsOnly",
                "value": "true"
            }],
            "outputSelector": ["PictureURLLarge", "SellerInfo"],
            "paginationInput": {
                "entriesPerPage": "100",
                "pageNumber": "1"
            }
        }
    }
    

    Sample C# Code Modification:

    namespace Solutions{
        using System.Collections.Generic;
        using Newtonsoft.Json;
        public class EBaySearchBody{
            public class FindCompletedItemsRequest{
                [JsonProperty("keywords", Order = 1)]
                public string Keywords { get; set; }
    
                [JsonProperty("itemFilter",Order = 2)]
                public List<ItemFilter> ItemFilters { get; set; }
    
                [JsonProperty("outputSelector", Order = 3)]
                public List<string> OutputSelectors { get; set; }
    
                [JsonProperty("paginationInput", Order = 4)]
                public PaginationInput PaginationInput { get; set; }
            }
            public class PaginationInput{
                [JsonProperty("entriesPerPage", Order = 1)]
                public string EntriesPerPage { get; set; }
    
                [JsonProperty("pageNumber", Order = 2)]
                public string PageNumber { get; set; }
            }
            public class ItemFilter{
                [JsonProperty("name", Order = 1)]
                public string Name { get; set; }
    
                [JsonProperty("value", Order = 2)]
                public string Value { get; set; }
            }
            [JsonProperty("findCompletedItemsRequest")]
            public FindCompletedItemsRequest FindCompletedItemsRequestObject { get; set; }
    
            /// <summary>
            /// Create a <see cref="EBaySearchBody"/>object and serialize it to a JSON stream
            /// </summary>
            /// <returns></returns>
            public static string WriteFromObject()
            {
                //Create EbaySearchBody object.  
                EBaySearchBody searchBody = new EBaySearchBody(){
                        FindCompletedItemsRequestObject = new FindCompletedItemsRequest(){
                            Keywords = "searchtext",
                            ItemFilters = new List<ItemFilter>(){
                                new ItemFilter {
                                    Name = "SoldItemsOnly",
                                    Value = "true"
                                }
                            },
                            OutputSelectors = new List<string>(){
                                "PictureURLLarge",
                                "SellerInfo"
                            },
                            PaginationInput = new PaginationInput(){
                                EntriesPerPage = "100",
                                PageNumber = "1"
                            }
                        }
                    };
                return JsonConvert.SerializeObject(searchBody);
            }
        }
    }