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)); }
/// <summary> /// Replaces a charset field (if any) that is hidden with the given /// character encoding. /// </summary> /// <param name="encoding">The encoding to use.</param> void ReplaceCharset(Encoding encoding) { for (int i = 0; i < _entries.Count; i++) { var entry = _entries[i]; if (!String.IsNullOrEmpty(entry.Name) && entry.Name.Equals("_charset_") && entry.Type.Equals("hidden", StringComparison.OrdinalIgnoreCase)) { _entries[i] = new TextDataSetEntry(entry.Name, encoding.WebName, entry.Type); } } }