olayk
olayk
CC#
Created by olayk on 5/7/2024 in #help
Help with sorting package issues in Visual Studio.
No description
15 replies
CC#
Created by olayk on 5/6/2024 in #help
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.
50 replies
CC#
Created by olayk on 5/6/2024 in #help
How to implement default indexing for my own JSONValue class
namespace JSONNS;

abstract class JSONValue;

class JSONNum(int val) : JSONValue {
public int Val { get; set; } = val;
public override string ToString() { return $"{Val}"; }
}

class JSONString(string val) : JSONValue {
public string Val { get; set; } = val;
public override string ToString() { return $"{Val}"; }
}

class JSONBool(bool val) : JSONValue {
public bool Val { get; set; } = val;
public override string ToString() { return $"{Val}"; }
}

class JSONNull : JSONValue {
public static int? Val = null;
}

class JSONArray(List<JSONValue> val) : JSONValue {
public List<JSONValue> Val { get; set; } = val;
}

class JSONObject(Dictionary<string, JSONValue> val) : JSONValue {
public Dictionary<string, JSONValue> Val { get; set; } = val;
public JSONValue this[string s] { get => Val[s]; }
}
namespace JSONNS;

abstract class JSONValue;

class JSONNum(int val) : JSONValue {
public int Val { get; set; } = val;
public override string ToString() { return $"{Val}"; }
}

class JSONString(string val) : JSONValue {
public string Val { get; set; } = val;
public override string ToString() { return $"{Val}"; }
}

class JSONBool(bool val) : JSONValue {
public bool Val { get; set; } = val;
public override string ToString() { return $"{Val}"; }
}

class JSONNull : JSONValue {
public static int? Val = null;
}

class JSONArray(List<JSONValue> val) : JSONValue {
public List<JSONValue> Val { get; set; } = val;
}

class JSONObject(Dictionary<string, JSONValue> val) : JSONValue {
public Dictionary<string, JSONValue> Val { get; set; } = val;
public JSONValue this[string s] { get => Val[s]; }
}
In another file I have a Parser class with Parser.parse() returning a JSONValue or throwing an error. Q1) If I have an abstract class as a return value / value does it enforces that it returns an instance of a child of this class? Q2) If I want to be able to index an unknown JSONValue (e.g. json['key1]['key2']) how should I do this? Would it work to have an abstract method for indexing, which returns an error on types such as JSONNumber? Q3) How could I enforce a Val in the JSONValue class? I would like to have a default ToString where I return the Val ToString which I would override on the JSONArray and JSONObject types, but I am not sure what type the Val could even be. Q4) Would it be a good idea to create my own error types for parsing, lexing and json indexing? Many thanks for any help! I'd appreciate any input on the code shown here also as I am new to C#. I'm not sure if this is the best way to create a type, so I'd appreciate any comments on a better way to implement this.
14 replies