protected override void SolveInstance(IGH_DataAccess DA) { var keys = new List <string>(); var values = new List <dynamic>(); DA.GetDataList("Key", keys); DA.GetDataList("Value", values); var valueOutput = new JsonDict(); if (keys.Count != 1 && values.Count != keys.Count) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Key and Value count mismatch. This component expects either one key for a whole list, or one key per item in a list."); return; } if (keys.Count == 1) // process as an array { valueOutput[keys.First()] = values.Select(v => v?.Value); } else { for (var i = 0; i < keys.Count; i++) { var key = keys[i]; var val = values[i]?.Value; valueOutput[key] = val; } } if (valueOutput.Count > 0) { DA.SetData("JSON", new JDictGoo(valueOutput)); } }
/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param> protected override void SolveInstance(IGH_DataAccess DA) { var valueOutput = new JsonDict(); for (var i = 0; i < Params.Input.Count; i++) { var name = Params.Input[i].NickName; var access = Params.Input[i].Access; try { switch (access) { case GH_ParamAccess.item: dynamic dataValue = null; DA.GetData(i, ref dataValue); var rawValue = dataValue?.Value; if (StructureLocked || rawValue != null) { valueOutput[name] = rawValue; } break; case GH_ParamAccess.list: var dataValues = new List <dynamic>(); DA.GetDataList(i, dataValues); if (StructureLocked || dataValues.Any(v => v != null)) { valueOutput[name] = dataValues.Select(v => v?.Value); } break; } } catch (Exception e) { AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, e.Message); } if (valueOutput.Count > 0) { DA.SetData("JSON", new JDictGoo(valueOutput)); } } }