private bool TryParseCoordinate(JArray coordinates, out CoordinateInfo result)
 {
     if (coordinates != null && coordinates.Count > 1 && coordinates.All(x => x is JValue))
     {
         var vals = coordinates.Cast<JValue>().ToList();
         if (vals.All(x => x.Type == JTokenType.Float || x.Type == JTokenType.Integer))
         {
             result = new CoordinateInfo
             {
                 X = Convert.ToDouble(vals[0].Value),
                 Y = Convert.ToDouble(vals[1].Value),
                 Z = vals.Count > 2 ? Convert.ToDouble(vals[2].Value) : (double?)null,
                 M = vals.Count > 3 ? Convert.ToDouble(vals[3].Value) : (double?)null
             };
             return true;
         }
     }
     result = null;
     return false;
 }
            /// <summary>
            /// Helper function to parse a <see cref="List{T}"/> of <see cref="List{T}"/> of <see cref="List{T}"/> of <see cref="Position"/>.
            /// </summary>
            /// <param name="array">
            /// Get JSON from this.
            /// </param>
            /// <returns>
            /// The parsed JSON.
            /// </returns>
            /// <exception cref="ArgumentException">
            /// Unexpected JSON.
            /// </exception>
            public static List<List<List<Position>>> ParseListListListPosition(JArray array)
            {
                if (array.Cast<JArray>().Any(p => p.Cast<JArray>().Any(r => r.Cast<JArray>().Any(l => l.Count < 2))))
                {
                    throw new ArgumentException(
                    string.Format(
                    "Expected all points to have greater than two points, got {0} with zero and {1} with one",
                    array.Cast<JArray>().Sum(p => p.Cast<JArray>().Sum(r => r.Cast<JArray>().Count(l => l.Count == 0))),
                    array.Cast<JArray>().Sum(p => p.Cast<JArray>().Sum(r => r.Cast<JArray>().Count(l => l.Count == 1)))),
                    "array");
                }

                return array.Select(p => p.Select(r => r.Select(l => new Position { P1 = (double)l[0], P2 = (double)l[1] }).ToList()).ToList()).ToList();
            }
 public void SetModelPropertyData(JArray modelPropertyData)
 {
     InputParameters = modelPropertyData.Cast<JObject>().Where(inputParameter=>!(bool)inputParameter["IsResult"]).Select<JObject,EffectsInputParameterViewModel>(inputParameter =>
     {
         switch ((int) inputParameter["ParameterType"])
         {
             case 1:
                 // Integer
                 // todo: if both min and max are specified create slider input
                 if (inputParameter["Data"]["Min"] != null && inputParameter["Data"]["Max"] != null && !double.IsNaN((double)inputParameter["Data"]["Min"]) && !double.IsNaN((double)inputParameter["Data"]["Max"]))
                 {
                     return new EffectsRangeInputParameterViewModel() {Data = inputParameter};
                 }
                 else
                 {
                     return new EffectsDoubleInputParameterViewModel() {Data = inputParameter};
                 }
                 break;
             case 6:
                 // Chemical
                 return new EffectsChemicalInputParameterViewModel(){Data = inputParameter, ChemicalData = new JArray(Chemicals)};
             default:
                 return null;
         }
     }).Where(vm=>vm != null);
 }
Example #4
0
            /// <summary>
            /// Helper function to parse a <see cref="List{T}"/> of <see cref="Position"/>.
            /// </summary>
            /// <param name="array">
            /// Get JSON from this.
            /// </param>
            /// <returns>
            /// The parsed JSON.
            /// </returns>
            /// <exception cref="ArgumentException">
            /// Unexpected JSON.
            /// </exception>
            public static List<Position> ParseListPosition(JArray array)
            {
                if (array.Cast<JArray>().Any(l => l.Count < 2))
                {
                    throw new ArgumentException(
                    string.Format(
                    "Expected all points to have greater than two points, got {0} with zero and {1} with one",
                    array.Cast<JArray>().Count(l => l.Count == 0),
                    array.Cast<JArray>().Count(l => l.Count == 1)),
                    "array");
                }

                return array.Select(l => new Position(l)).ToList();
            }
Example #5
0
 public static UpdateInfo[] ParseArray(JArray jsonArray)
 {
     return jsonArray.Cast<JObject>().Select(jobject => new UpdateInfo(jobject)).ToArray();
 }
Example #6
0
        /// <summary>
        /// 反序列化指定的数组为一个实体的列表。
        /// </summary>
        /// <param name="listType"></param>
        /// <param name="jArray"></param>
        /// <returns></returns>
        public EntityList DeserializeList(Type listType, JArray jArray)
        {
            var entityType = EntityMatrix.FindByList(listType).EntityType;
            var repo = RF.Find(entityType);

            //先从数据库中找出所有提供了 Id 的实体。
            var idList = jArray.Cast<JObject>().Select(item => TryGetId(item))
                .Where(id => id != null).ToArray();
            var list = repo.GetByIdList(idList);

            //依次反序列化数组中的实体:
            //如果有 Id,则在数据库中查询出的列表 list 中查找出对应的实体,然后反序列化值。否则,直接构造新实体。
            for (int i = 0, c = jArray.Count; i < c; i++)
            {
                var jEntity = jArray[i] as JObject;
                var child = FindOrCreate(list, jEntity);
                DeserializeProperties(jEntity, child);
            }

            return list;
        }
Example #7
0
 internal static PhotoSizeInfo[] ParseArray(JArray jsonArray)
 {
     return jsonArray.Cast<JObject>().Select(jobject => new PhotoSizeInfo(jobject)).ToArray();
 }
            /// <inheritdoc/>
            public override void ParseJson(JArray array)
            {
                if (array.Cast<JArray>().Any(l => l.Count < 2))
                {
                    throw new ArgumentException(
                    string.Format(
                    "Expected all points to have greater than two points, got {0} with zero and {1} with one",
                    array.Cast<JArray>().Count(l => l.Count == 0),
                    array.Cast<JArray>().Count(l => l.Count == 1)),
                    "array");
                }

                this.Points = array.Cast<JArray>().Select(l => new Point { Position = new Position { P1 = (double)l[0], P2 = (double)l[1] } }).ToList();
            }