ln.json/ln.json/JSONValue.cs

78 lines
2.5 KiB
C#

// /**
// * File: JSONValue.cs
// * Author: haraldwolff
// *
// * This file and it's content is copyrighted by the Author and / or copyright holder.
// * Any use wihtout proper permission is illegal and may lead to legal actions.
// *
// *
// **/
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Text;
using System.Text.Json;
using ln.type;
namespace ln.json
{
public enum JSONValueType
{
NULL, OBJECT, ARRAY, STRING, NUMBER, TRUE, FALSE
}
public abstract class JSONValue : DynamicObject
{
public JSONValueType ValueType { get; private set; }
public virtual bool HasChildren => false;
public virtual IEnumerable<JSONValue> Children => throw new NotSupportedException();
public virtual object ToNative() => throw new NotImplementedException();
public T As<T>() where T : JSONValue => this as T;
public JSONValue(JSONValueType valueType)
{
ValueType = valueType;
}
public virtual JSONValue this[string property]
{
get => throw new NotSupportedException();
set => throw new NotSupportedException();
}
public virtual JSONValue this[int index]
{
get => throw new NotSupportedException();
set => throw new NotSupportedException();
}
public abstract JSONValue Clone();
public override string ToString() => throw new NotImplementedException();
public virtual void WriteTo(string filename)
{
using (var fs = new FileStream(filename, FileMode.Create, FileAccess.Write))
{
WriteTo(fs);
}
}
public virtual void WriteTo(Stream stream)
{
stream.Write(Encoding.UTF8.GetBytes(this.ToString()!));
}
public static implicit operator JSONValue(string v) => new JSONString(v);
public static implicit operator JSONValue(float v) => new JSONNumber(v);
public static implicit operator JSONValue(double v) => new JSONNumber(v);
public static implicit operator JSONValue(decimal v) => new JSONNumber(v);
public static implicit operator JSONValue(int v) => new JSONNumber(v);
public static implicit operator JSONValue(long v) => new JSONNumber(v);
public static implicit operator JSONValue(uint v) => new JSONNumber(v);
public static implicit operator JSONValue(ulong v) => new JSONNumber(v);
}
}