internal static void Init(Scriptable scope, bool @sealed) { Rhino.NativeJSON obj = new Rhino.NativeJSON(); obj.ActivatePrototypeMap(MAX_ID); obj.SetPrototype(GetObjectPrototype(scope)); obj.SetParentScope(scope); if (@sealed) { obj.SealObject(); } ScriptableObject.DefineProperty(scope, "JSON", obj, ScriptableObject.DONTENUM); }
private static string Ja(NativeArray value, NativeJSON.StringifyState state) { if (state.stack.Search(value) != -1) { throw ScriptRuntime.TypeError0("msg.cyclic.value"); } state.stack.Push(value); string stepback = state.indent; state.indent = state.indent + state.gap; IList<object> partial = new List<object>(); long len = value.GetLength(); for (long index = 0; index < len; index++) { object strP; if (index > int.MaxValue) { strP = Str(System.Convert.ToString(index), value, state); } else { strP = Str((int)index, value, state); } if (strP == Undefined.instance) { partial.Add("null"); } else { partial.Add(strP); } } string finalValue; if (partial.IsEmpty()) { finalValue = "[]"; } else { if (state.gap.Length == 0) { finalValue = '[' + Join(partial, ",") + ']'; } else { string separator = ",\n" + state.indent; string properties = Join(partial, separator); finalValue = "[\n" + state.indent + properties + '\n' + stepback + ']'; } } state.stack.Pop(); state.indent = stepback; return finalValue; }
private static string Jo(Scriptable value, NativeJSON.StringifyState state) { if (state.stack.Search(value) != -1) { throw ScriptRuntime.TypeError0("msg.cyclic.value"); } state.stack.Push(value); string stepback = state.indent; state.indent = state.indent + state.gap; object[] k = null; if (state.propertyList != null) { k = Sharpen.Collections.ToArray(state.propertyList); } else { k = value.GetIds(); } IList<object> partial = new List<object>(); foreach (object p in k) { object strP = Str(p, value, state); if (strP != Undefined.instance) { string member = Quote(p.ToString()) + ":"; if (state.gap.Length > 0) { member = member + " "; } member = member + strP; partial.Add(member); } } string finalValue; if (partial.IsEmpty()) { finalValue = "{}"; } else { if (state.gap.Length == 0) { finalValue = '{' + Join(partial, ",") + '}'; } else { string separator = ",\n" + state.indent; string properties = Join(partial, separator); finalValue = "{\n" + state.indent + properties + '\n' + stepback + '}'; } } state.stack.Pop(); state.indent = stepback; return finalValue; }
private static object Str(object key, Scriptable holder, NativeJSON.StringifyState state) { object value = null; if (key is string) { value = GetProperty(holder, (string)key); } else { value = GetProperty(holder, System.Convert.ToInt32(((Number)key))); } if (value is Scriptable) { object toJSON = GetProperty((Scriptable)value, "toJSON"); if (toJSON is Callable) { value = CallMethod(state.cx, (Scriptable)value, "toJSON", new object[] { key }); } } if (state.replacer != null) { value = state.replacer.Call(state.cx, state.scope, holder, new object[] { key, value }); } if (value is NativeNumber) { value = ScriptRuntime.ToNumber(value); } else { if (value is NativeString) { value = ScriptRuntime.ToString(value); } else { if (value is NativeBoolean) { value = ((NativeBoolean)value).GetDefaultValue(ScriptRuntime.BooleanClass); } } } if (value == null) { return "null"; } if (value.Equals(true)) { return "true"; } if (value.Equals(false)) { return "false"; } if (value is CharSequence) { return Quote(value.ToString()); } if (value is Number) { double d = System.Convert.ToDouble(((Number)value)); if (d == d && d != double.PositiveInfinity && d != double.NegativeInfinity) { return ScriptRuntime.ToString(value); } else { return "null"; } } if (value is Scriptable && !(value is Callable)) { if (value is NativeArray) { return Ja((NativeArray)value, state); } return Jo((Scriptable)value, state); } return Undefined.instance; }