Is my way for parser written properly? I have zones

public class Zone
{
     public string Caption { get; set; }
     public IList&lt;Module&gt; Contents { get; set; }        
}

public class Zone1 : Zone
{
}
public class Zone2 : Zone
{
}
public class Zone3 : Zone
{
    public enum Direction
    {
        Up = 1,
        Down = 2,
        Left = 3,
        Right = 4
    }
    public Direction Direction { get; set; }
}


I have json string [{"Caption":"Tab1","IDs":[4,6]},{"Caption":"Tab2","IDs":[9]}] that corresponds to the regular zones like Zone1,Zone2

{"Caption":"Tab1","IDs":[4,6]} means that the name of this zone is Tab1 and it will contain Modules #4 and #6.

I have default parser that will create modules according to IDs(4,6 AND 9).

public interface IZoneParser&lt;T&gt;
{
    IList&lt;T&gt; Parse(string json);    
}
public abstract class ZoneParserBase : IZoneParser&lt;Zone&gt;
{
    public virtual IList&lt;Zone&gt; Parse(string json)
    {
        IList&lt;Zone&gt; zones = new List&lt;Zone&gt;();
        try
        {
            IList&lt;ZoneJson&gt; parsedZones = json.FromJSON&lt;IList&lt;ZoneJson&gt;&gt;();

            foreach (var parsedZone in parsedZones)
            {
                var zone = new Zone
                {
                    Caption = parsedZone.Caption,
                    Contents = new List&lt;Module&gt;()
                };
                foreach (var id in parsedZone.IDs)
                {
                    var module = currentPage.CreateModule(id);
                    zone.Contents.Add(module);
                }
                zones.Add(zone);
            }

            return zones;
        }
        catch
        {
            return null;
        }
    }
}


So, in order to parse the above json I'll need some class like

[DataContract]  
private class ZoneJson
{
     [DataMember]
     public string Caption { get; set; }
     [DataMember]
     public IList&lt;int&gt; IDs { get; set; }
}


If I'll need to parse Zone3 with json string [{"Caption":"Tab1","IDs":[4,6],"Direction":"1"},{"Caption":"Tab2","IDs":[9]},"Direction":"3"], so I'll create

public class ZoneParserA : ZoneParserBase
{
   publice override IList&lt;Zone3&gt; Parse(string json)
   {
       ....
       IList&lt;Zone3Json&gt; parsedZones = json.FromJSON&lt;IList&lt;Zone3Json&gt;&gt;();

       foreach (var parsedZone in parsedZones)
       {
           var zone = new Zone3
           {
                Caption = parsedZone.Caption,
                Direction = parsedZone.Direction,
                Contents = new List&lt;Module&gt;()
           };
           foreach (var id in parsedZone.IDs)
           {
                var module = currentPage.CreateModule(id);
                zone.Contents.Add(module);
           }
           zones.Add(zone);
       }
       ....
   }
}


and 

[DataContract]
[KnownType(typeof(Direction))]
public class Zone3Json : ZoneJson
{        
    public Direction Direction { get; set; }
}


So, for each type of Zone(Zone,Zone1,Zone2,Zone3...) I'll need some kind Zone(X)Json that will differ from Zone(X) just with IList&lt;int&gt; IDs property. I can add this property to class Zone, but I don't want that anybody could access it from outside.

In addition, the functionality of method Parse is more or less the same for ZoneParserA and ZoneParserBase. 

Is there way to avoid Zone(X)Json classes and/or maybe there are any suggestions to rewriting ZoneParsers?

I hope I explained myself properly.

Thank you
I can add this property to class Zone, but I don't want that anybody
  could access it from outside.


Which 'access' do you have in mind? through JSON (send you a 'fake' direction for zone1,2) or through code?

Either way, I don't think that by not adding it to the Zone class you will improve the security of your solution?

How do you decide on which parser to use when you receive a json string? BTW, your second json string looks wrong to me (the last direction param)


  [{"Caption":"Tab1","IDs":[4,6],"Direction":"1"},{"Caption":"Tab2","IDs":[9]},"Direction":"3"]


Why do you need Zone1 and Zone2? They're identical so why not simply use Zone? Even your ZoneBaseParser creates Zone objects and has no notion of Zone1 or Zone2.

Maybe you have a predefined set of zones (up to 4) and you need an index. Add an index property, if that's the case.

Your question is interesting but, instead of implementing a solution to the current problem (which would involve reflection), I'd focus on rethinking the json format and the classes and remove the problem altogether.

Quick and easy:

[DataContract]  
private class ZoneJson
{
     [DataMember]
     public int ZoneType { get; set; }//could use an enum instead of int
     [DataMember]
     public int ZoneIndex { get; set; }//if a zone index is needed
     [DataMember]
     public string Caption { get; set; }
     [DataMember]
     public IList&lt;int&gt; IDs { get; set; }
     [DataMember]
     public Direction Direction { get; set; }
}


Or, a more generic approach:

[DataContract]  
private class ZoneJson
{
     [DataMember]
     public int ZoneType { get; set; }//could use an enum instead of int
     [DataMember]
     public int ZoneIndex { get; set; }//if a zone index is needed
     [DataMember]
     public string Caption { get; set; }
     [DataMember]
     public IList&lt;int&gt; IDs { get; set; }


     //a json string containing additional params
     //might be set for certain ZoneTypes and it's contents can vary, based on ZoneType
     [DataMember]
     public string JsonParams { get; set; }
}


Next, you can add a Zone3Params class to implement the storage for Zone3 params (direction):

[DataContract]
[KnownType(typeof(Direction))]
public class Zone3ParamsJson : ZoneParamsBase
{        
    [DataMember]
    public Direction Direction { get; set; }
}


and parse any defined zone type like this:

public class ZoneParserBase : IZoneParser&lt;Zone&gt;
{
    public virtual IList&lt;Zone&gt; Parse(string json)
    {
        IList&lt;Zone&gt; zones = new List&lt;Zone&gt;();
        try
        {
            IList&lt;ZoneJson&gt; parsedZones = json.FromJSON&lt;IList&lt;ZoneJson&gt;&gt;();

            foreach (var parsedZone in parsedZones)
            {
                var zone = new Zone
                {
                    //don't forget the index and type
                    ZoneType = parsedZone.ZoneType,
                    Caption = parsedZone.Caption,
                    Contents = new List&lt;Module&gt;()
                };
                foreach (var id in parsedZone.IDs)
                {
                    var module = currentPage.CreateModule(id);
                    zone.Contents.Add(module);
                }
                //parseParams
                if(zone.ZoneType==3)
                {
                    var params = parsedZone.JsonParams.FromJSON&lt;Zone3ParamsJson&gt;();
                    zone.Params = params;//zone.Params would be something like ZoneParamsBase
                }
                zones.Add(zone);
            }

            return zones;
        }
        catch
        {
            return null;
        }
    }
}