Exemplo n.º 1
0
        /// <summary>
        /// Parse
        /// </summary>
        /// <param name="cVariable"></param>
        /// <param name="value">either a JArray, JObject or primitive value</param>
        /// <exception cref="InvalidParsingException"></exception>
        private static void SetFromJObject(this IEditableVariable cVariable, object value)
        {
            // switch over the jobject
            switch (value)
            {
            // enums and arrays are JArrays and can be directly deserialized
            case JArray jArray:
                switch (cVariable)
                {
                // enums are serialized as list of strings
                case IREDEnum:
                    var enumobj = jArray.ToObject <List <string> >();
                    if (enumobj == null)
                    {
                        throw new InvalidParsingException("not a CVariable");
                    }
                    cVariable.SetValue(enumobj);
                    break;

                // arrays
                case ICurveDataAccessor b:

                    break;

                case IREDArray redArray:
                    var listOfObjects = jArray.ToObject <List <object> >();
                    if (listOfObjects == null)
                    {
                        throw new InvalidParsingException("not a CVariable");
                    }
                    for (var i = 0; i < listOfObjects.Count; i++)
                    {
                        var jitem   = listOfObjects[i];
                        var element = redArray.GetElementInstance(i.ToString());
                        // parse the elements according to the array type
                        element.SetFromJObject(jitem);
                        redArray.AddVariable(element);
                    }
                    break;

                default:
                    throw new InvalidParsingException("not a CVariable");
                }
                break;

            // complex objects are JObjects and can be deserialized as CVariableDto (recursive)
            case JObject jObject:
                cVariable.SetFromDictionary(jObject.ToObject <Dictionary <string, object> >());
                break;

            // does that ever happen?
            case JToken jToken:
                throw new InvalidParsingException("file format not supported");

            // primitive types can be set directly
            default:
                switch (value)
                {
                // all numeric values
                case long:
                case double:
                case BigInteger:
                    cVariable.SetValue(value.ToString());         // cast to string to do the parsing in the classes :/
                    break;

                // other primitive types can be set directly
                case bool b:
                    cVariable.SetValue(b);
                    break;

                case string s:
                    cVariable.SetValue(s);
                    break;

                default:
                    throw new InvalidParsingException("file format not supported");
                }

                break;
            }
        }