Example #1
0
 private static string Stringify(JsonElement resultingObject)
 {
     return resultingObject.ToString();
 }
Example #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;
                }
            }
        }