public Serialize ( object value ) : string | ||
value | object | The value to serialize. |
return | string |
public static string Stringify(ScriptEngine engine, object value, [DefaultParameterValue(null)] object replacer = null, [DefaultParameterValue(null)] object spacer = null) { var serializer = new JSONSerializer(engine); // The replacer object can be either a function or an array. serializer.ReplacerFunction = replacer as FunctionInstance; if (replacer is ArrayInstance) { var replacerArray = (ArrayInstance)replacer; var serializableProperties = new HashSet <string>(StringComparer.Ordinal); foreach (object elementValue in replacerArray.ElementValues) { if (elementValue is string || elementValue is int || elementValue is double || elementValue is StringInstance || elementValue is NumberInstance) { serializableProperties.Add(TypeConverter.ToString(elementValue)); } } serializer.SerializableProperties = serializableProperties; } // The spacer argument can be the number of spaces or a string. if (spacer is NumberInstance) { spacer = ((NumberInstance)spacer).Value; } else if (spacer is StringInstance) { spacer = ((StringInstance)spacer).Value; } if (spacer is double) { serializer.Indentation = new string(' ', Math.Max(Math.Min(TypeConverter.ToInteger((double)spacer), 10), 0)); } else if (spacer is int) { serializer.Indentation = new string(' ', Math.Max(Math.Min(TypeConverter.ToInteger((int)spacer), 10), 0)); } else if (spacer is string) { serializer.Indentation = ((string)spacer).Substring(0, Math.Min(((string)spacer).Length, 10)); } // Serialize the value. return(serializer.Serialize(value)); }
public static object Stringify(ScriptEngine engine, object value, object replacer = null, object spacer = null) { var serializer = new JSONSerializer(engine); // The replacer object can be either a function or an array. serializer.ReplacerFunction = replacer as FunctionInstance; if (replacer is ObjectInstance replaceObjectInstance && ArrayConstructor.IsArray(replacer)) { var serializableProperties = new HashSet <string>(StringComparer.Ordinal); foreach (object elementValue in TypeUtilities.CreateListFromArrayLike(replaceObjectInstance)) { if (elementValue is string || elementValue is int || elementValue is uint || elementValue is double || elementValue is StringInstance || elementValue is NumberInstance) { serializableProperties.Add(TypeConverter.ToString(elementValue)); } } serializer.SerializableProperties = serializableProperties; } // The spacer argument can be the number of spaces or a string. if (spacer is NumberInstance) { spacer = ((NumberInstance)spacer).Value; } else if (spacer is StringInstance) { spacer = ((StringInstance)spacer).Value; } if (spacer is double) { serializer.Indentation = new string(' ', Math.Max(Math.Min(TypeConverter.ToInteger((double)spacer), 10), 0)); } else if (spacer is int || spacer is uint) { serializer.Indentation = new string(' ', Math.Max(Math.Min(TypeConverter.ToInteger(spacer), 10), 0)); } else if (spacer is string) { serializer.Indentation = ((string)spacer).Substring(0, Math.Min(((string)spacer).Length, 10)); } // Serialize the value. return(serializer.Serialize(value) ?? (object)Undefined.Value); }
public static string Stringify(ScriptEngine engine, object value, object replacer = null, object spacer = null) { var serializer = new JSONSerializer(engine); // The replacer object can be either a function or an array. serializer.ReplacerFunction = replacer as FunctionInstance; if (replacer is ArrayInstance) { var replacerArray = (ArrayInstance)replacer; var serializableProperties = new HashSet<string>(StringComparer.Ordinal); foreach (object elementValue in replacerArray.ElementValues) { if (elementValue is string || elementValue is int || elementValue is double || elementValue is StringInstance || elementValue is NumberInstance) serializableProperties.Add(TypeConverter.ToString(elementValue)); } serializer.SerializableProperties = serializableProperties; } // The spacer argument can be the number of spaces or a string. if (spacer is NumberInstance) spacer = ((NumberInstance)spacer).Value; else if (spacer is StringInstance) spacer = ((StringInstance)spacer).Value; if (spacer is double) serializer.Indentation = new string(' ', Math.Max(Math.Min(TypeConverter.ToInteger((double)spacer), 10), 0)); else if (spacer is int) serializer.Indentation = new string(' ', Math.Max(Math.Min(TypeConverter.ToInteger((int)spacer), 10), 0)); else if (spacer is string) serializer.Indentation = ((string)spacer).Substring(0, Math.Min(((string)spacer).Length, 10)); // Serialize the value. return serializer.Serialize(value); }