Example #1
0
        /// <summary>
        /// Transforms the value stored in the given object using toJSON and/or the replacer function.
        /// </summary>
        /// <param name="holder"> The object containing the value. </param>
        /// <param name="arrayIndex"> The array index of the property holding the value to transform. </param>
        /// <returns> The transformed value. </returns>
        private object TransformPropertyValue(ScriptEngine engine, Nitrassic.Library.Array holder, uint arrayIndex)
        {
            string propertyName = null;
            object value        = holder[arrayIndex];

            // Transform the value by calling toJSON(), if the method exists on the object.
            if (value is ObjectInstance)
            {
                propertyName = arrayIndex.ToString();
                object toJSONResult;
                if (((ObjectInstance)value).TryCallMemberFunction(out toJSONResult, "toJSON", propertyName) == true)
                {
                    value = toJSONResult;
                }
            }

            // Transform the value by calling the replacer function, if one was provided.
            if (this.ReplacerFunction != null)
            {
                if (propertyName == null)
                {
                    propertyName = arrayIndex.ToString();
                }
                value = this.ReplacerFunction.CallLateBound(engine, holder, propertyName, value);
            }

            return(value);
        }
Example #2
0
        /// <summary>
        /// Serializes an array into a JSON string.
        /// </summary>
        /// <param name="value"> The array to serialize. </param>
        /// <param name="result"> The StringBuilder to write the JSON representation of the
        /// array to. </param>
        private void SerializeArray(Nitrassic.Library.Array value, StringBuilder result)
        {
            // Add the spacer string to the current separator string.
            string previousSeparator = this.separator;

            this.separator += this.Indentation;

            // Check for cyclical references.
            if (this.arrayStack.Contains(value) == true)
            {
                throw new JavaScriptException(this.engine, "TypeError", "The given object must not contain cyclical references");
            }
            this.arrayStack.Push(value);

            result.Append('[');

            uint arrLen = (uint)Nitrassic.Library.Array.get_Length(engine, value);

            for (uint i = 0; i < arrLen; i++)
            {
                // Append the separator.
                if (i > 0)
                {
                    result.Append(',');
                }
                result.Append(this.separator);

                // Transform the value using the replacer function or toJSON().
                object elementValue = TransformPropertyValue(engine, value, i);

                if (elementValue == null || elementValue == Undefined.Value || elementValue is FunctionInstance)
                {
                    // Undefined is serialized as "null".
                    result.Append("null");
                }
                else
                {
                    // Serialize the element value to the output.
                    SerializePropertyValue(elementValue, result);
                }
            }
            if (arrLen > 0)
            {
                result.Append(previousSeparator);
            }
            result.Append(']');

            // Remove this object from the stack.
            this.arrayStack.Pop();

            // Restore the separator to it's previous value.
            this.separator = previousSeparator;
        }