none
Convert comma-seperated string into int[] directly RRS feed

  • Question

  • Hi guys,

    I have a json string as

    "products":"714,715,716"

    and define

            [JsonProperty(PropertyName = "products")]
            public string ProductIds { get; set; }

    In the function, I am using 

       int[] productIds = ProductIds.Split(',').Select(Int32.Parse).ToArray();

    I just want to know if there is another way to convert this comma-seperated string into int[] directly in its set method

    so I can define

            public int[] ProductIds { get; set; }

    Thanks for your help

    Best regards,

    Richard


    lurc

    Tuesday, January 16, 2018 10:03 AM

Answers

  • Given that you have a JSON string with comma separated values then your JSON converter is probably not going to like that without some sort of converter. It completely depends upon your conversion library though. 

    But arrays shouldn't be properties. It is considered bad design. 

    https://msdn.microsoft.com/en-us/library/0fss9skc.aspx?f=255&MSPPError=-2147217396

    https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/arrays

    In general you should avoid arrays anyway but if you are going to use them then only as return values of methods where the resulting array is a copy of the actual data (since anyone can change the members.

    But to the general question of converting that JSON string to something more useable you can use a helper method and/or property without invalidating the JSON.

    public class Product
    {
       public Product ()
       {
          _idList = new Lazy<IEnumerable<int>>(LoadIds);   
       }
    
       [JsonProperty(PropertyName = "products")]
       public string ProductIds 
       {
          get => _ids;
          set 
          {
             _ids = value;
             
             //Reset the list
             _idList = new Lazy<IEnumerable<int>>(LoadIds);   
          }
       }
    
       //Using IEnumerable<int> here so the list cannot be 
       //changed
       public IEnumerable<int> Ids => _idList.Value;
    
       //Using lazy loading but you could do this in the setter
       private IEnumerable<int> LoadIds ()
               => (_ids ?? "").Split(',').Select(i => Int32.Parse(i));
    
       private Lazy<IEnumerable<int>> _idList;
    }

    Note though that exposing ProductIds may still not be desirable so perhaps, depending upon your conversion library, creating it as a private or internal property that isn't visible and then exposing only the public member would be preferable. 

    Alternatively, if there are several manipulations on the JSON data you need to do then consider creating a DTO to convert to/from JSON. Then have your actual business object be constructable from/to the DTO. This avoids mixing persistence with your business objects.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by richardlu Thursday, January 18, 2018 6:40 AM
    Wednesday, January 17, 2018 3:58 PM
    Moderator

All replies

  • Hi,

    Welcome to MSDN forum.

    I will move your thread to c# forum for professional support. If you have problems about visual studio IDE, please feel free to let me know.

    Best regards,

    Joyce


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 17, 2018 1:55 AM
  • Given that you have a JSON string with comma separated values then your JSON converter is probably not going to like that without some sort of converter. It completely depends upon your conversion library though. 

    But arrays shouldn't be properties. It is considered bad design. 

    https://msdn.microsoft.com/en-us/library/0fss9skc.aspx?f=255&MSPPError=-2147217396

    https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/arrays

    In general you should avoid arrays anyway but if you are going to use them then only as return values of methods where the resulting array is a copy of the actual data (since anyone can change the members.

    But to the general question of converting that JSON string to something more useable you can use a helper method and/or property without invalidating the JSON.

    public class Product
    {
       public Product ()
       {
          _idList = new Lazy<IEnumerable<int>>(LoadIds);   
       }
    
       [JsonProperty(PropertyName = "products")]
       public string ProductIds 
       {
          get => _ids;
          set 
          {
             _ids = value;
             
             //Reset the list
             _idList = new Lazy<IEnumerable<int>>(LoadIds);   
          }
       }
    
       //Using IEnumerable<int> here so the list cannot be 
       //changed
       public IEnumerable<int> Ids => _idList.Value;
    
       //Using lazy loading but you could do this in the setter
       private IEnumerable<int> LoadIds ()
               => (_ids ?? "").Split(',').Select(i => Int32.Parse(i));
    
       private Lazy<IEnumerable<int>> _idList;
    }

    Note though that exposing ProductIds may still not be desirable so perhaps, depending upon your conversion library, creating it as a private or internal property that isn't visible and then exposing only the public member would be preferable. 

    Alternatively, if there are several manipulations on the JSON data you need to do then consider creating a DTO to convert to/from JSON. Then have your actual business object be constructable from/to the DTO. This avoids mixing persistence with your business objects.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by richardlu Thursday, January 18, 2018 6:40 AM
    Wednesday, January 17, 2018 3:58 PM
    Moderator