ln.json/JSON.cs

203 lines
4.6 KiB
C#
Raw Normal View History

2017-10-26 16:41:14 +02:00
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
2017-11-03 13:13:09 +01:00
using System.Net;
using System.Reflection;
using System.Collections;
using System.Security.Cryptography;
2017-11-23 13:05:58 +01:00
using System.CodeDom;
using System.Globalization;
2017-10-26 16:41:14 +02:00
namespace sharp.json
{
2017-11-03 13:13:09 +01:00
public delegate object JSONActivationDelegate(Type t);
public abstract class JSON : IEnumerable<JSON>
2017-10-26 16:41:14 +02:00
{
2017-11-03 13:13:09 +01:00
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(); } }
2017-11-23 13:05:58 +01:00
public virtual object Object { get { throw new NotImplementedException(); } }
2017-11-03 13:13:09 +01:00
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;
}
2017-11-23 13:05:58 +01:00
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;
2017-11-03 13:13:09 +01:00
}
2017-11-23 13:05:58 +01:00
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() );
2017-11-03 13:13:09 +01:00
stream.Write(data,0,data.Length);
}
public void WriteTo(String filename)
2017-11-23 13:05:58 +01:00
{
WriteTo(filename, false);
}
public void WriteTo(String filename,bool pretty)
2017-11-03 13:13:09 +01:00
{
2017-12-07 20:17:29 +01:00
using (FileStream fs = new FileStream(filename, FileMode.Create))
{
WriteTo(fs, pretty);
fs.Close();
}
2017-11-03 13:13:09 +01:00
}
2017-11-23 13:05:58 +01:00
public static JSON ReadFrom(string filename)
{
return ReadFrom(filename, null);
2017-11-03 13:13:09 +01:00
}
2017-11-23 13:05:58 +01:00
public static JSON ReadFrom(string filename, object defaultValue)
2017-11-03 13:13:09 +01:00
{
2017-11-23 13:05:58 +01:00
JSONParser parser = new JSONParser();
string source = "";
try
2017-11-03 13:13:09 +01:00
{
2017-11-23 13:05:58 +01:00
source = File.ReadAllText(filename);
2017-10-26 16:41:14 +02:00
}
2017-11-23 13:05:58 +01:00
catch (FileNotFoundException e)
2017-11-03 13:13:09 +01:00
{
2017-11-23 13:05:58 +01:00
return JSON.From(defaultValue);
2017-10-26 16:41:14 +02:00
}
2017-11-23 13:05:58 +01:00
return parser.Parse(source);
2017-10-26 16:41:14 +02:00
}
2017-11-23 13:05:58 +01:00
public static JSON ReadFrom(string filename,JSON defaultValue){
JSONParser parser = new JSONParser();
string source = "";
try
2017-11-03 13:13:09 +01:00
{
2017-11-23 13:05:58 +01:00
source = File.ReadAllText(filename);
} catch (FileNotFoundException e){
return defaultValue;
2017-11-03 13:13:09 +01:00
}
2017-12-07 20:17:29 +01:00
if (source.Equals(""))
{
return defaultValue;
}
2017-11-23 13:05:58 +01:00
return parser.Parse(source);
2017-10-26 16:41:14 +02:00
}
2017-11-23 13:05:58 +01:00
public object Native(){
switch (JSONType)
2017-11-03 13:13:09 +01:00
{
2017-11-23 13:05:58 +01:00
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;
2017-11-03 13:13:09 +01:00
}
2017-11-23 13:05:58 +01:00
throw new InvalidCastException(String.Format("Can't create native type of {0}",this.JSONType));
2017-10-26 16:41:14 +02:00
}
2017-11-03 13:13:09 +01:00
2017-10-26 16:41:14 +02:00
2017-11-03 13:13:09 +01:00
public virtual IEnumerator<JSON> GetEnumerator()
{
throw new NotImplementedException();
2017-10-26 16:41:14 +02:00
}
2017-11-03 13:13:09 +01:00
IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator)GetEnumerator();
2017-10-26 16:41:14 +02:00
}
}
}