Como mapear atributos JSON nas propriedades de classes do C#/How to associate JSON attributes in C# class properties
{
"name":"form1",
"text":"Primeiro formulário",
"type":"OK",
"controls":[
{
"name":"txtText1",
"type":"TextBox",
"key":"text1",
"label":"Campo texto",
"initialValue":"Some text"
},
{
"name":"cboName",
"type":"ComboBox",
"key":"name1",
"label":"Campo múltipla escolha",
"initialValue":"1",
"bindingSource": {
"connection":"Data source=local; Initial catalog=databasename; Integrated security=true;",
"sql":"SELECT ID, NAME FROM USERS ORDER BY NAME",
"keyValue":"id",
"displayValue":"name"
}}]
}
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
namespace WindowsForms
{
/// <summary>
/// Class to build a dynamic form
/// </summary>
[Serializable]
public class DynamicDialog
{
#region fields
string _dialogType;
IEnumerable<string> _validDialogTypes =
new List<string>() { "OK", "YesNo" };
DynamicForm _DynamicForm;
#endregion
#region properties
[JsonProperty(PropertyName = "name")]
public string FormName { get; set; }
[JsonProperty(PropertyName = "text")]
public string Text { get; set; }
[JsonProperty(PropertyName = "type")]
public string DialogType
{
get { return _dialogType; }
set
{
if (_validDialogTypes.Contains(value))
{
_dialogType = value;
}
else
{
throw new Exception(
"Dialog type is not valid." +
"\nCurrent valid types are:\n\n\t\"OK\" and \"YesNo\"");
}
}
}
[JsonProperty(PropertyName = "controls")]
public List<DynamicControl> DynamicControls { get; set; }
