static void Example8() { FormDataSet set = new FormDataSet(); set.Append("highlander[]", "one", "text"); var result = Encoding.UTF8.GetString(ApplicationJsonEncode(set)); Debug.Assert("{\"highlander\":[\"one\"]}" == result); }
static void Example6() { FormDataSet set = new FormDataSet(); set.Append("wow[such][deep][3][much][power][!]", "Amaze", "text"); var result = Encoding.UTF8.GetString(ApplicationJsonEncode(set)); Debug.Assert("{\"wow\":{\"such\":{\"deep\":[null,null,null,{\"much\":{\"power\":{\"!\":\"Amaze\"}}}]}}}" == result); }
static void Example4() { FormDataSet set = new FormDataSet(); set.Append("hearbeat[0]", "thunk", "text"); set.Append("hearbeat[2]", "thunk", "text"); var result = Encoding.UTF8.GetString(ApplicationJsonEncode(set)); Debug.Assert("{\"hearbeat\":[\"thunk\",null,\"thunk\"]}" == result); }
static void Example10() { FormDataSet set = new FormDataSet(); set.Append("error[good]", "BOOM!", "text"); set.Append("error[bad", "BOOM BOOM!", "text"); var result = Encoding.UTF8.GetString(ApplicationJsonEncode(set)); Debug.Assert("{\"error\":{\"good\":\"BOOM!\"},\"error[bad\":\"BOOM BOOM!\"}" == result); }
static void Example2() { FormDataSet set = new FormDataSet(); set.Append("bottle-on-wall", "1", "number"); set.Append("bottle-on-wall", "2", "number"); set.Append("bottle-on-wall", "3", "number"); var result = Encoding.UTF8.GetString(ApplicationJsonEncode(set)); Debug.Assert("{\"bottle-on-wall\":[1,2,3]}" == result); }
static void Example1() { FormDataSet set = new FormDataSet(); set.Append("name", "Bender", "text"); set.Append("hind", "Bitable", "checkbox"); set.Append("shiny", "true", "text"); var result = Encoding.UTF8.GetString(ApplicationJsonEncode(set)); Debug.Assert("{\"name\":\"Bender\",\"hind\":\"Bitable\",\"shiny\":\"true\"}" == result); }
private static byte[] ApplicationJsonEncode(FormDataSet formDataSet) { //1. Let resulting object be a new Object. var resultingObject = new JsonObject(); //2. For each entry in the form data set, perform these substeps: foreach (var entry in formDataSet._entries) { //2.1. If the entry's type is file, set the is file flag. bool isFile = entry is FileDataSetEntry; TextDataSetEntry text = entry as TextDataSetEntry; FileDataSetEntry file = entry as FileDataSetEntry; JsonValue entryValue; if (text != null) { entryValue = new JsonValue(text.Value); } else { var stream = file.Value.Body; MemoryStream ms = stream as MemoryStream; if (ms == null) { ms = new MemoryStream(); stream.CopyTo(ms); } entryValue = new JsonValue(Convert.ToBase64String(ms.ToArray())); } //2.2. Let steps be the result of running the steps to parse a JSON encoding path on the entry's name. List <Step> steps = ParseJSONPath(entry.Name); //2.3. Let context be set to the value of resulting object. JsonElement context = resultingObject; //2.4. For each step in the list of steps, run the following subsubsteps: foreach (var step in steps) { //2.4.1. Let the current value be the value obtained by getting the step's key from the current context. JsonElement currentValue = context[step.Key]; //2.4.2. Run the steps to set a JSON encoding value with the current context, the step, the current value, the entry's value, and the is file flag. //2.4.3. Update context to be the value returned by the steps to set a JSON encoding value ran above. context = JsonEncodeValue(context, step, currentValue, entryValue, isFile); } } //3. Let result be the value returned from calling the stringify operation with resulting object as its first parameter and the two remaining parameters left undefined. string result = Stringify(resultingObject); //4. Encode result as UTF - 8 and return the resulting byte stream. return(Encoding.UTF8.GetBytes(result)); }
static void Example5() { FormDataSet set = new FormDataSet(); set.Append("pet[0][species]", "Dahut", "text"); set.Append("pet[0][name]", "Hypatia", "text"); set.Append("pet[1][species]", "Felis Stultus", "text"); set.Append("pet[1][name]", "Billie", "text"); var result = Encoding.UTF8.GetString(ApplicationJsonEncode(set)); Debug.Assert("{\"pet\":[{\"species\":\"Dahut\",\"name\":\"Hypatia\"},{\"species\":\"Felis Stultus\",\"name\":\"Billie\"}]}" == result); }
static void Example3() { FormDataSet set = new FormDataSet(); set.Append("pet[species]", "Dahut", "text"); set.Append("pet[name]", "Hypatia", "text"); set.Append("kids[1]", "Thelma", "text"); set.Append("kids[0]", "Ashley", "text"); var result = Encoding.UTF8.GetString(ApplicationJsonEncode(set)); Debug.Assert("{\"pet\":{\"species\":\"Dahut\",\"name\":\"Hypatia\"},\"kids\":[\"Ashley\",\"Thelma\"]}" == result); }
static void Example7() { FormDataSet set = new FormDataSet(); set.Append("mix", "scalar", "text"); set.Append("mix[0]", "array 1", "text"); set.Append("mix[2]", "array 2", "text"); set.Append("mix[key]", "key key", "text"); set.Append("mix[car]", "car key", "text"); var result = Encoding.UTF8.GetString(ApplicationJsonEncode(set)); Debug.Assert("{\"mix\":{\"\":\"scalar\",\"0\":\"array 1\",\"2\":\"array 2\",\"key\":\"key key\",\"car\":\"car key\"}}" == result); }