Last active
November 14, 2024 00:48
-
-
Save TheHelpfulHelper/0b059ac91901ecec048306c4a94cd71a to your computer and use it in GitHub Desktop.
Formats a VRC.SDK3.Data.DataToken similar to VRCJson.TrySerializeToJson, however with handling for Reference and Error Tokens
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UdonSharp; | |
using UnityEngine; | |
using VRC.SDK3.Data; | |
namespace THH.Utility | |
{ | |
public static class VRCDataExtensions | |
{ | |
public static void Log(this DataToken token) | |
{ | |
Debug.Log(token.Format()); | |
} | |
public static void Log(this DataDictionary dataDictionary) | |
{ | |
Debug.Log(dataDictionary.Format()); | |
} | |
public static void Log(this DataList dataList) | |
{ | |
Debug.Log(dataList.Format()); | |
} | |
[RecursiveMethod] | |
public static string Format(this DataToken token, int indentLevel = 0) | |
{ | |
switch (token.TokenType) | |
{ | |
case TokenType.DataDictionary: | |
return FormatDataDictionary(token.DataDictionary, indentLevel + 1); | |
case TokenType.DataList: | |
return FormatDataList(token.DataList, indentLevel + 1); | |
case TokenType.Reference: | |
return $"\"[Reference({token.Reference.GetType().FullName})]\""; | |
case TokenType.Error: | |
return $"\"ERROR: {token.Error}\""; | |
case TokenType.Null: | |
return "null"; | |
case TokenType.String: | |
return $"\"{token}\""; | |
default: | |
return token.ToString(); | |
} | |
} | |
public static string Format(this DataDictionary dataDictionary) => FormatDataDictionary(dataDictionary, 1); | |
public static string Format(this DataList dataList) => FormatDataList(dataList, 1); | |
[RecursiveMethod] | |
public static string FormatDataList(DataList dataList, int indentLevel) | |
{ | |
if (dataList.Count == 0) return "[ ]"; | |
string[] strings = new string[dataList.Count]; | |
for (int i = 0; i < dataList.Count; i++) | |
{ | |
strings[i] = Format(dataList[i], indentLevel); | |
} | |
return $"[\n{Indent(indentLevel)}{string.Join($",\n{Indent(indentLevel)}", strings)}\n{Indent(indentLevel - 1)}]"; | |
} | |
[RecursiveMethod] | |
public static string FormatDataDictionary(DataDictionary dataDictionary, int indentLevel) | |
{ | |
if (dataDictionary.Count == 0) return "{ }"; | |
string[] strings = new string[dataDictionary.Count]; | |
var keys = dataDictionary.GetKeys(); | |
for (int i = 0; i < dataDictionary.Count; i++) | |
{ | |
var key = keys[i]; | |
strings[i] = $"\"{key.String}\": {Format(dataDictionary[key], indentLevel)}"; | |
} | |
return $"{{\n{Indent(indentLevel)}{string.Join($",\n{Indent(indentLevel)}", strings)}\n{Indent(indentLevel - 1)}}}"; | |
} | |
private static string Indent(int n) | |
{ | |
return new string(' ', n * 4); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment