A variable of two types

前端 未结 1 1282
时光取名叫无心
时光取名叫无心 2021-01-23 06:04

I need to read an JSON object from TypeScript, which has a variable names prop which of two types, either Identifier or Expression to C#.

相关标签:
1条回答
  • 2021-01-23 06:50

    If the property can have either of these two formats:

    { "prop": "identifier" }
    { "prop": { "complex": "object" } }
    

    ie. either a simple string or a more complex object you can solve this by using dynamic to accept the parsed prop value, and extra properties to return either the identifier or the complex object. Here is a LINQPad program that demonstrates:

    void Main()
    {
        var jsonStrings = new[]
        {
            "{ \"prop\": \"identifier\" }",
            "{ \"prop\": { \"complex\": \"object\" } }"
        };
    
        jsonStrings.Select(json => JsonConvert.DeserializeObject<Test>(json)).Dump();
    }
    
    public class Test
    {
        public Test()
        {
            _Complex = new Lazy<Complex>(GetComplex);
        }
    
        [JsonProperty("prop")]
        public dynamic prop { get; set; }
    
        [JsonIgnore]
        public string Identifier => prop as string;
    
        private readonly Lazy<Complex> _Complex;
    
        [JsonIgnore]
        public Complex Complex => _Complex.Value;
    
        private Complex GetComplex()
        {
            if (!(prop is JObject))
                return null;
    
            return ((JObject)prop).ToObject<Complex>();
        }
    }
    
    public class Complex
    {
        public string complex { get; set; }
    }
    

    Output:

    A different approach would be to create your own types to handle the multiple outputs and again use a secondary property to evaluate what you actually got from the json:

    void Main()
    {
        var jsonStrings = new[]
        {
            "{ \"prop\": \"identifier\" }",
            "{ \"prop\": { \"complex\": \"object\" } }"
        };
    
        jsonStrings.Select(json => JsonConvert.DeserializeObject<Test>(json)).Dump();
    }
    
    public class Test
    {
        [JsonProperty("prop")]
        public dynamic prop { get; set; }
    
        [JsonIgnore]
        public PropertyValue Property
        {
            get
            {
                if (prop is string)
                    return new IdentifierProperty(prop as string);
                return new ExpressionProperty(((JObject)prop).ToObject<Expression>());
            }
        }
    }
    
    public abstract class PropertyValue
    {
    }
    
    public class IdentifierProperty : PropertyValue
    {
        public IdentifierProperty(string identifier)
        {
            Identifier = identifier;
        }
    
        public string Identifier { get; }
    
        public override string ToString() => Identifier;
    }
    
    public class ExpressionProperty : PropertyValue
    {
        public ExpressionProperty(Expression expression)
        {
            Expression = expression;
        }
    
        public Expression Expression { get; }
    
        public override string ToString() => Expression.ToString();
    }
    
    public class Expression
    {
        public string complex { get; set; }
    
        public override string ToString() => $"complex: {complex}";
    }
    

    Output:

    0 讨论(0)
提交回复
热议问题