Creating a Printer class for my own type.
I am doing this for learning and am aware that if I want to work with JSON I can use System.Text.Json
This is the type to extend:
And want to define a class like:
How should I extend the functionality of the JSON types. I had a ToString() method, but it didnt deal with indents and single responsibility principle.
This is the type to extend:
using System.Text;
namespace JSON.Types;
abstract class JSONValue {
public abstract JSONValue this[string s] { get; }
public abstract JSONValue this[int i] { get; }
}
class JSONNum(int val) : JSONValue {
public int Val { get; set; } = val;
public override JSONValue this[string s] { get => throw new IndexOutOfRangeException("Cannot index type JSONNum"); }
public override JSONValue this[int i] { get => throw new IndexOutOfRangeException("Cannot index type JSONNum"); }
}
class JSONString(string val) : JSONValue {
public string Val { get; set; } = val;
public override JSONValue this[string s] { get => throw new IndexOutOfRangeException("Cannot index type JSONString"); }
public override JSONValue this[int i] { get => throw new IndexOutOfRangeException("Cannot index type JSONString"); }
}
class JSONObject(Dictionary<string, JSONValue> val) : JSONValue {
public Dictionary<string, JSONValue> Val { get; set; } = val;
public override JSONValue this[string s] { get => Val[s]; }
public override JSONValue this[int i] { get => throw new IndexOutOfRangeException("Type JSONArray cannot be indexed via int"); }
}using System.Text;
namespace JSON.Types;
abstract class JSONValue {
public abstract JSONValue this[string s] { get; }
public abstract JSONValue this[int i] { get; }
}
class JSONNum(int val) : JSONValue {
public int Val { get; set; } = val;
public override JSONValue this[string s] { get => throw new IndexOutOfRangeException("Cannot index type JSONNum"); }
public override JSONValue this[int i] { get => throw new IndexOutOfRangeException("Cannot index type JSONNum"); }
}
class JSONString(string val) : JSONValue {
public string Val { get; set; } = val;
public override JSONValue this[string s] { get => throw new IndexOutOfRangeException("Cannot index type JSONString"); }
public override JSONValue this[int i] { get => throw new IndexOutOfRangeException("Cannot index type JSONString"); }
}
class JSONObject(Dictionary<string, JSONValue> val) : JSONValue {
public Dictionary<string, JSONValue> Val { get; set; } = val;
public override JSONValue this[string s] { get => Val[s]; }
public override JSONValue this[int i] { get => throw new IndexOutOfRangeException("Type JSONArray cannot be indexed via int"); }
}And want to define a class like:
using JSON.Types;
namespace JSON.Display;
class PrettyPrint() {
public static void Print(JSONValue v) {}
public static void PrintValue(string prefix, JSONNum v) {}
public static void PrintValue(string prefix, JSONString v) {}
public static void PrintValue(string prefix, JSONBool v) {}
public static void PrintValue(string prefix, JSONNull v) {}
public static void PrintValue(string prefix, JSONArray v) {}
public static void PrintValue(string prefix, JSONObject v) {}
}using JSON.Types;
namespace JSON.Display;
class PrettyPrint() {
public static void Print(JSONValue v) {}
public static void PrintValue(string prefix, JSONNum v) {}
public static void PrintValue(string prefix, JSONString v) {}
public static void PrintValue(string prefix, JSONBool v) {}
public static void PrintValue(string prefix, JSONNull v) {}
public static void PrintValue(string prefix, JSONArray v) {}
public static void PrintValue(string prefix, JSONObject v) {}
}How should I extend the functionality of the JSON types. I had a ToString() method, but it didnt deal with indents and single responsibility principle.