forked from ln-dotnet/ln.json
203 lines
4.6 KiB
C#
203 lines
4.6 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Collections.Generic;
|
|
using System.Net;
|
|
using System.Reflection;
|
|
using System.Collections;
|
|
using System.Security.Cryptography;
|
|
using System.CodeDom;
|
|
using System.Globalization;
|
|
|
|
namespace sharp.json
|
|
{
|
|
public delegate object JSONActivationDelegate(Type t);
|
|
|
|
public abstract class JSON : IEnumerable<JSON>
|
|
{
|
|
public virtual double Double { get { throw new NotImplementedException(); } }
|
|
public virtual long Integer { get { throw new NotImplementedException(); } }
|
|
public virtual string String { get { throw new NotImplementedException(); } }
|
|
public virtual bool Boolean { get { throw new NotImplementedException(); } }
|
|
public virtual object Object { get { throw new NotImplementedException(); } }
|
|
|
|
public virtual JSON this[int n]
|
|
{
|
|
get { throw new NotImplementedException(String.Format("{0} has no numbered elements",GetType().Name)); }
|
|
set { throw new NotImplementedException(String.Format("{0} has no numbered elements",GetType().Name)); }
|
|
}
|
|
|
|
public virtual JSON this[string name] {
|
|
get { throw new NotImplementedException(String.Format("{0} has no named elements", GetType().Name)); }
|
|
set { throw new NotImplementedException(String.Format("{0} has no named elements", GetType().Name)); }
|
|
}
|
|
|
|
public virtual int Count { get { return 0; } }
|
|
|
|
public virtual bool Contains(object o){
|
|
return false;
|
|
}
|
|
|
|
public virtual void Add(JSON child)
|
|
{
|
|
throw new NotImplementedException(String.Format("{0} has no numbered elements", GetType().Name));
|
|
}
|
|
public virtual void Remove(JSON child)
|
|
{
|
|
throw new NotImplementedException(String.Format("{0} has no numbered elements", GetType().Name));
|
|
}
|
|
|
|
|
|
public JSONTypes JSONType { get; private set; }
|
|
|
|
protected JSON(JSONTypes type){
|
|
JSONType = type;
|
|
}
|
|
|
|
public abstract string[] prettyPrint();
|
|
public override abstract string ToString();
|
|
|
|
|
|
public string prettyFormat(){
|
|
return String.Join("\n", prettyPrint());
|
|
}
|
|
|
|
|
|
|
|
public static implicit operator JSON(Double src)
|
|
{
|
|
return new JSONNumber(src);
|
|
}
|
|
public static implicit operator JSON(int src)
|
|
{
|
|
return new JSONNumber(src);
|
|
}
|
|
public static implicit operator JSON(string text)
|
|
{
|
|
return new JSONString(text);
|
|
}
|
|
public static implicit operator JSON(bool b)
|
|
{
|
|
return b ? JSONSpecial.True : JSONSpecial.False;
|
|
}
|
|
|
|
public static implicit operator string(JSON json)
|
|
{
|
|
return json.String;
|
|
}
|
|
public static implicit operator bool(JSON json)
|
|
{
|
|
return json.Boolean;
|
|
}
|
|
public static implicit operator int(JSON json)
|
|
{
|
|
return (int)json.Integer;
|
|
}
|
|
public static implicit operator long(JSON json)
|
|
{
|
|
return json.Integer;
|
|
}
|
|
public static implicit operator double(JSON json)
|
|
{
|
|
return json.Double;
|
|
}
|
|
|
|
public T To<T>()
|
|
{
|
|
return JSONConverter.To<T>(this);
|
|
}
|
|
|
|
public static JSON From(object o){
|
|
return JSONConverter.From(o);
|
|
}
|
|
|
|
|
|
public void WriteTo(Stream stream)
|
|
{
|
|
WriteTo(stream);
|
|
}
|
|
public void WriteTo(Stream stream,bool pretty){
|
|
byte[] data = Encoding.ASCII.GetBytes(pretty ? prettyFormat() : ToString() );
|
|
stream.Write(data,0,data.Length);
|
|
}
|
|
|
|
public void WriteTo(String filename)
|
|
{
|
|
WriteTo(filename, false);
|
|
}
|
|
public void WriteTo(String filename,bool pretty)
|
|
{
|
|
using (FileStream fs = new FileStream(filename, FileMode.Create))
|
|
{
|
|
WriteTo(fs, pretty);
|
|
fs.Close();
|
|
}
|
|
}
|
|
|
|
public static JSON ReadFrom(string filename)
|
|
{
|
|
return ReadFrom(filename, null);
|
|
}
|
|
|
|
public static JSON ReadFrom(string filename, object defaultValue)
|
|
{
|
|
JSONParser parser = new JSONParser();
|
|
string source = "";
|
|
try
|
|
{
|
|
source = File.ReadAllText(filename);
|
|
}
|
|
catch (FileNotFoundException e)
|
|
{
|
|
return JSON.From(defaultValue);
|
|
}
|
|
return parser.Parse(source);
|
|
}
|
|
|
|
public static JSON ReadFrom(string filename,JSON defaultValue){
|
|
JSONParser parser = new JSONParser();
|
|
string source = "";
|
|
try
|
|
{
|
|
source = File.ReadAllText(filename);
|
|
} catch (FileNotFoundException e){
|
|
return defaultValue;
|
|
}
|
|
if (source.Equals(""))
|
|
{
|
|
return defaultValue;
|
|
}
|
|
return parser.Parse(source);
|
|
}
|
|
|
|
public object Native(){
|
|
switch (JSONType)
|
|
{
|
|
case JSONTypes.Null:
|
|
return null;
|
|
case JSONTypes.True:
|
|
return true;
|
|
case JSONTypes.False:
|
|
return false;
|
|
case JSONTypes.Number:
|
|
return Double;
|
|
case JSONTypes.String:
|
|
return String;
|
|
}
|
|
throw new InvalidCastException(String.Format("Can't create native type of {0}",this.JSONType));
|
|
}
|
|
|
|
|
|
|
|
public virtual IEnumerator<JSON> GetEnumerator()
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
{
|
|
return (IEnumerator)GetEnumerator();
|
|
}
|
|
}
|
|
}
|