Last active
September 26, 2015 20:18
-
-
Save jschmid/1153585 to your computer and use it in GitHub Desktop.
Handling arrays in POST data with Nancy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Text.RegularExpressions; | |
using Nancy; | |
namespace NancyComponents | |
{ | |
public class PostArrayHandler | |
{ | |
public static object GetFormWithArrays(dynamic form) | |
{ | |
var newDictionnary = new DynamicDictionary (); | |
var memberNames = (IEnumerable<string>) form.GetDynamicMemberNames (); | |
foreach (var key in memberNames) | |
{ | |
string value = form[key]; | |
var match = PostArrayHandler.reg.Match (key); | |
if (match.Success) | |
{ | |
string arrayName = match.Groups[1].Value; | |
List<string> list = newDictionnary[arrayName].Value; | |
if (list == null) | |
{ | |
list = new List<string> (); | |
newDictionnary[arrayName] = list; | |
} | |
list.Add (value); | |
} | |
else | |
{ | |
newDictionnary[key] = value; | |
} | |
} | |
return newDictionnary; | |
} | |
private static readonly Regex reg = new Regex (@"^([^\[]+)\[[^\]]?\]$"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment