© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
49 replies
olayk

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:
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.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Problems creating my own SSO Provider
C#CC# / help
2y ago
How to implement default indexing for my own JSONValue class
C#CC# / help
2y ago
creating a new type
C#CC# / help
3y ago