Beispiel #1
0
        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);
        }
Beispiel #2
0
        private static JsonElement JsonEncodeValue(JsonElement context, Step step, JsonElement currentValue, JsonElement entryValue, bool isFile)
        {
            //1. Let context be the context this algorithm is called with.
            //2. Let step be the step of the path this algorithm is called with.
            //3. Let current value be the current value this algorithm is called with.
            //4. Let entry value be the entry value this algorithm is called with.
            //5. Let is file be the is file flag this algorithm is called with.

            //6. If is file is set then replace entry value with an Object have its "name" property set to the file's name, its "type" property set to the file's type, and its "body" property set to the Base64 encoding of the file's body. [RFC2045]
            if (isFile)
            {
                JsonObject file = new JsonObject();
                file["name"] = new JsonValue("dummy.txt");
                file["type"] = new JsonValue("txt/txt");
                //file["body"] = new JsonValue(Convert.ToBase64String((byte[])file));
                //var file = (object)entryValue; //cast to underlying file type
                entryValue = file;
            }

            //7. If step has its last flag set, run the following substeps:
            if (step.Last)
            {
                //7.1. If current value is undefined, run the following subsubsteps:
                if (currentValue == null) //undefined
                {
                    //7.1.1. If step's append flag is set, set the context's property named by the step's key to a new Array containing entry value as its only member.
                    if (step.Append)
                    {
                        var arr = new JsonArray();
                        arr.Elements.Add(entryValue);
                        context[step.Key] = arr;
                    }
                    //7.1.2. Otherwise, set the context's property named by the step's key to entry value.
                    else
                    {
                        context[step.Key] = entryValue;
                    }
                }
                //7.2. Else if current value is an Array, then get the context's property named by the step's key and push entry value onto it.
                else if (currentValue is JsonArray)
                {
                    (context[step.Key] as JsonArray).Elements.Add(entryValue);
                }
                //7.3. Else if current value is an Object and the is file flag is not set, then run the steps to set a JSON encoding value with
                //context set to the current value;
                //a step with its type set to "object", its key set to the empty string, and its last flag set;
                //current value set to the current value's property named by the empty string;
                //the entry value;
                //and the is file flag.
                //Return the result.
                else if (currentValue is JsonObject && !isFile)
                {
                    return JsonEncodeValue(currentValue, new Step { Type = StepType.Object, Key = "", Last = true }, currentValue[""], entryValue, true);
                }
                //7.4. Otherwise, set the context's property named by the step's key to an Array containing current value and entry value, in this order.
                else
                {
                    JsonArray arr = new JsonArray();
                    arr.Elements.Add(currentValue);
                    arr.Elements.Add(entryValue);
                    context[step.Key] = arr;
                }
                //7.5. Return context.
                return context;
            }
            //8. Otherwise, run the following substeps:
            else
            {
                //8.1. If current value is undefined, run the following subsubsteps:
                if (currentValue == null)
                {
                    //8.1.1. If step's next type is "array", set the context's property named by the step's key to a new empty Array and return it.
                    if (step.NextType == StepType.Array)
                    {
                        return context[step.Key] = new JsonArray();
                    }
                    //8.2.2. Otherwise,set the context's property named by the step's key to a new empty Object and return it.
                    else
                    {
                        return context[step.Key] = new JsonObject();
                    }
                }
                //8.2. Else if current value is an Object, then return the value of the context's property named by the step's key.
                else if (currentValue is JsonObject)
                {
                    return context[step.Key];
                }
                //8.3. Else if current value is an Array, then run the following subsubsteps:
                else if (currentValue is JsonArray)
                {
                    //8.3.1. If step's next type is "array", return current value.
                    if (step.NextType == StepType.Array)
                    {
                        return currentValue;
                    }
                    //8.3.2. Otherwise, run the following subsubsubsteps:
                    else
                    {
                        //8.3.2.1. Let object be a new empty Object.
                        var @object = new JsonObject();

                        //8.3.2.2. For each item and zero-based index i in current value, if item is not undefined then set a property of object named i to item.
                        int i = 0;
                        foreach (var item in (currentValue as JsonArray).Elements)
                        {
                            if (item != null)
                            {
                                @object[i] = item;
                            }
                            i++;
                        }

                        //8.3.2.3. Otherwise, set the context's property named by the step's key to object.
                        context[step.Key] = @object;

                        //8.3.2.4. Return object.
                        return @object;
                    }
                }
                //8.4. Otherwise, run the following subsubsteps:
                else
                {
                    //8.4.1. Let object be a new Object with a property named by the empty string set to current value.
                    var @object = new JsonObject();
                    @object[""] = currentValue;

                    //8.4.2. Set the context's property named by the step's key to object.
                    context[step.Key] = @object;

                    //8.4.3. Return object.
                    return @object;
                }
            }
        }