-1

I am working on a back end data integration between an ERP system and Shopify using their rest API. Recently I came across what I see as an inconsistency and I'm not certain how to handle this in C sharp without too much duplication. Specifically this relates to their admin API and the product API.

What I did is I took the Jason response from an API that invokes a return of a list of products and I got this class structure (I'm going to keep it simple by showing only the root and the main products property)

public class Root
{
    public List<Product> products { get; set; }
}

However there is another API that allows you to get a single product an its return response does not utilize a list but a single subclass that is the same as the product subclass.

public class Root
{
    public Product product { get; set; }
}

Everything else about the overall product class is the same. The only difference is that in one return we have a list of subclass product in the other a single element. Without having to create two completely different classes is there a way to do this so the rest of the subclass definitions which includes other properties and subclasses can be used. This is the subclass product:

public class Product
{
    public Int64 id { get; set; }
    public string title { get; set; }
    public string body_html { get; set; }
    public string vendor { get; set; }
    public string product_type { get; set; }
    public DateTime? created_at { get; set; }
    public string handle { get; set; }
    public DateTime? updated_at { get; set; }
    public DateTime? published_at { get; set; }
    public object template_suffix { get; set; }
    public string published_scope { get; set; }
    public string tags { get; set; }
    public string admin_graphql_api_id { get; set; }
    public List<Variant> variants { get; set; }
    public List<Option> options { get; set; }
    public List<Image> images { get; set; }
    public Image image { get; set; }
}

I looked at inheritance but that didn't seem to quite fit and I wasn't certain if even extends would work and while my work around is too not use the get single product API but get a list of 1, I'm curious if there's a way to do this where I can not have to create two full classes just because in the root one is a list and one is a single object.

I am using Newton json to do the serialization and deserialization of the object data.

j.hull
  • 321
  • 3
  • 15
  • Can you show us the JSON that make you believe you need to do this? You can have two classes with the same name in different namespaces – Flydog57 Apr 14 '21 at 21:55
  • Use the `SingleOrArrayConverter` from [this answer](https://stackoverflow.com/a/18997172/10263) to the linked duplicate question. `T` would be `Product` in your case. – Brian Rogers Apr 15 '21 at 04:15

2 Answers2

0

You can have two classes with different names. When you deserialize the json, it will not matter what the root class is called.

public class Multi
{
    public List<Product> products { get; set; }
}

public class One
{
    public Product product { get; set; }
}

Or, you could have a single root class with both the list property and the single instance property. When the deserialization is done, just access the appropriate property.

public class Root
{
    public List<Product> products { get; set; }
    public Product product { get; set; }
}
Steve Harris
  • 5,014
  • 1
  • 10
  • 25
0

You could use inheritance:

public abstract class Root
{
    //All the other properties you don't want to duplicate
}

public class RootWithProduct : Root
{
    public Product product { get; set; }
}

public class RootWithProductList : Root
{
    public List<Product> products { get; set; }
}

When you need to deserialize, use:

RootWithProduct x = JsonConvert.DeserializeObject<RootWithProduct>(json);
Product product = x.product;

Or

RootWithProductList y = JsonConvert.DeserializeObject<RootWithProductList>(json);
List<Product> products = y.products;
John Wu
  • 50,556
  • 8
  • 44
  • 80