/// <summary> /// Serializes the object with formatting for displaying in the console. /// </summary> /// <param name="value">The value to format and serialize.</param> /// <param name="theme">(Optional) QuantumTheme to use for formatting the results.</param> /// <returns>The formatted serialization.</returns> public string SerializeFormatted(object value, QuantumTheme theme = null) { if (value is null) { return(string.Empty); } Type type = value.GetType(); string result = string.Empty; string SerializeInternal(IQcSerializer serializer) { try { return(serializer.SerializeFormatted(value, theme, _recursiveSerializer)); } catch (Exception e) { throw new Exception($"Serialization of {type.GetDisplayName()} via {serializer} failed:\n{e.Message}", e); } } if (_serializerLookup.ContainsKey(type)) { result = SerializeInternal(_serializerLookup[type]); } else if (_unserializableLookup.Contains(type)) { result = value.ToString(); } else { bool converted = false; foreach (IQcSerializer serializer in _serializers) { if (serializer.CanSerialize(type)) { result = SerializeInternal(serializer); _serializerLookup[type] = serializer; converted = true; break; } } if (!converted) { result = value.ToString(); _unserializableLookup.Add(type); } } if (theme && !string.IsNullOrWhiteSpace(result)) { result = theme.ColorizeReturn(result, type); } return(result); }
public static string SerializeFormatted(object data, QuantumTheme theme = null) { if (_serializerObsolete == null) { _serializerObsolete = new QuantumSerializer(); } return(_serializerObsolete.SerializeFormatted(data, theme)); }
/// <summary>Applies a theme to the Quantum Console.</summary> /// <param name="theme">The desired theme to apply.</param> public void ApplyTheme(QuantumTheme theme, bool forceRefresh = false) { _theme = theme; if (theme) { if (_textComponents == null || forceRefresh) { _textComponents = GetComponentsInChildren <TextMeshProUGUI>(true); } foreach (TextMeshProUGUI text in _textComponents) { if (theme.Font) { text.font = theme.Font; } } foreach (Image panel in _panels) { panel.material = theme.PanelMaterial; panel.color = theme.PanelColor; } } }
public abstract string SerializeFormatted(T value, QuantumTheme theme);
protected string SerializeRecursive(object value, QuantumTheme theme) { return(_recursiveSerializer(value, theme)); }
string IQcSerializer.SerializeFormatted(object value, QuantumTheme theme, Func <object, QuantumTheme, string> recursiveSerializer) { _recursiveSerializer = recursiveSerializer; return(SerializeFormatted((T)value, theme)); }