using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace Luneraria.Core.Tests
{
///
/// Minimal JSON reader for the golden files, without package dependencies (the same code
/// runs in Unity and in the .NET harness). Objects keep their key order; numbers without
/// fraction or exponent become long, all others double.
///
public static class MiniJson
{
public static object Parse(string text)
{
int at = 0;
object value = ReadValue(text, ref at);
SkipSpace(text, ref at);
if (at != text.Length)
throw new FormatException("trailing characters at " + at);
return value;
}
static void SkipSpace(string text, ref int at)
{
while (at < text.Length && char.IsWhiteSpace(text[at]))
at++;
}
static object ReadValue(string text, ref int at)
{
SkipSpace(text, ref at);
char c = text[at];
if (c == '{')
return ReadObject(text, ref at);
if (c == '[')
return ReadArray(text, ref at);
if (c == '"')
return ReadString(text, ref at);
if (text.Length - at >= 4 && string.CompareOrdinal(text, at, "true", 0, 4) == 0)
{
at += 4;
return true;
}
if (text.Length - at >= 5 && string.CompareOrdinal(text, at, "false", 0, 5) == 0)
{
at += 5;
return false;
}
if (text.Length - at >= 4 && string.CompareOrdinal(text, at, "null", 0, 4) == 0)
{
at += 4;
return null;
}
return ReadNumber(text, ref at);
}
static OrderedMap ReadObject(string text, ref int at)
{
var result = new OrderedMap();
at++;
SkipSpace(text, ref at);
if (text[at] == '}')
{
at++;
return result;
}
while (true)
{
SkipSpace(text, ref at);
string key = ReadString(text, ref at);
SkipSpace(text, ref at);
if (text[at] != ':')
throw new FormatException("expected ':' at " + at);
at++;
result[key] = ReadValue(text, ref at);
SkipSpace(text, ref at);
if (text[at] == ',')
{
at++;
continue;
}
if (text[at] == '}')
{
at++;
return result;
}
throw new FormatException("expected ',' or '}' at " + at);
}
}
static List