using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace MSCO
{
///
/// Kleiner eigener JSON-Leser (ohne Newtonsoft.Json - das braucht System.Runtime.Serialization, das ohne MSCLoader im
/// Spiel fehlt). Ergebnis: Dictionary<string, object> (Objekt, Schlüssel ohne Groß-/Kleinschreibung),
/// List<object>, string, double, bool oder null.
///
public static class MiniJson
{
public static object Parse(string json)
{
if (json == null) throw new FormatException("No JSON");
int i = 0;
object value = ReadValue(json, ref i);
SkipWhitespace(json, ref i);
if (i < json.Length) throw new FormatException("Unexpected text at " + i);
return value;
}
// ---------------------------------------------------------------- Zugriff
public static string Str(Dictionary o, string key, string fallback = null)
{
object v;
if (o == null || !o.TryGetValue(key, out v) || v == null) return fallback;
if (v is double) return ((double)v).ToString(CultureInfo.InvariantCulture);
return v.ToString();
}
public static int Int(Dictionary o, string key, int fallback = 0)
{
object v;
if (o == null || !o.TryGetValue(key, out v)) return fallback;
if (v is double) return (int)(double)v;
int parsed;
return v is string && int.TryParse((string)v, NumberStyles.Integer, CultureInfo.InvariantCulture, out parsed) ? parsed : fallback;
}
public static bool Bool(Dictionary o, string key, bool fallback = false)
{
object v;
if (o == null || !o.TryGetValue(key, out v)) return fallback;
return v is bool ? (bool)v : fallback;
}
public static List Strings(Dictionary o, string key)
{
var result = new List();
object v;
if (o == null || !o.TryGetValue(key, out v)) return result;
var list = v as List