make it nice

This commit is contained in:
Philip Schell 2019-07-23 14:04:06 +02:00
parent 021417c029
commit 81b0cb3990
2 changed files with 927 additions and 1054 deletions

View File

@ -19,113 +19,76 @@ using System.IO;
namespace LitJson namespace LitJson
{ {
public class JsonData : IJsonWrapper, IEquatable<JsonData> public class JsonData : IJsonWrapper, IEquatable<JsonData> {
{
#region Fields #region Fields
private IList<JsonData> inst_array; private IList<JsonData> inst_array;
private bool inst_boolean; private Boolean inst_boolean;
private double inst_double; private Double inst_double;
private int inst_int; private Int32 inst_int;
private long inst_long; private Int64 inst_long;
private IDictionary<string, JsonData> inst_object; private IDictionary<String, JsonData> inst_object;
private string inst_string; private String inst_string;
private string json; private String json;
private JsonType type; private JsonType type;
// Used to implement the IOrderedDictionary interface // Used to implement the IOrderedDictionary interface
private IList<KeyValuePair<string, JsonData>> object_list; private IList<KeyValuePair<String, JsonData>> object_list;
#endregion #endregion
#region Properties #region Properties
public int Count { public Int32 Count => this.EnsureCollection().Count;
get { return EnsureCollection ().Count; }
public Boolean IsArray => this.type == JsonType.Array;
public Boolean IsBoolean => this.type == JsonType.Boolean;
public Boolean IsDouble => this.type == JsonType.Double;
public Boolean IsInt => this.type == JsonType.Int;
public Boolean IsLong => this.type == JsonType.Long;
public Boolean IsObject => this.type == JsonType.Object;
public Boolean IsString => this.type == JsonType.String;
public ICollection<String> Keys {
get {
this.EnsureDictionary();
return this.inst_object.Keys;
}
} }
public bool IsArray {
get { return type == JsonType.Array; }
}
public bool IsBoolean {
get { return type == JsonType.Boolean; }
}
public bool IsDouble {
get { return type == JsonType.Double; }
}
public bool IsInt {
get { return type == JsonType.Int; }
}
public bool IsLong {
get { return type == JsonType.Long; }
}
public bool IsObject {
get { return type == JsonType.Object; }
}
public bool IsString {
get { return type == JsonType.String; }
}
public ICollection<string> Keys {
get { EnsureDictionary (); return inst_object.Keys; }
}
/// <summary> /// <summary>
/// Determines whether the dictionary contains an element that has the specified key. /// Determines whether the dictionary contains an element that has the specified key.
/// </summary> /// </summary>
/// <param name="key">The key to locate in the dictionary.</param> /// <param name="key">The key to locate in the dictionary.</param>
/// <returns>true if the dictionary contains an element that has the specified key; otherwise, false.</returns> /// <returns>true if the dictionary contains an element that has the specified key; otherwise, false.</returns>
public Boolean ContainsKey(String key) { public Boolean ContainsKey(String key) {
EnsureDictionary(); this.EnsureDictionary();
return this.inst_object.Keys.Contains(key); return this.inst_object.Keys.Contains(key);
} }
#endregion #endregion
#region ICollection Properties #region ICollection Properties
int ICollection.Count { Int32 ICollection.Count => this.Count;
get {
return Count;
}
}
bool ICollection.IsSynchronized { Boolean ICollection.IsSynchronized => this.EnsureCollection().IsSynchronized;
get {
return EnsureCollection ().IsSynchronized;
}
}
object ICollection.SyncRoot { Object ICollection.SyncRoot => this.EnsureCollection().SyncRoot;
get {
return EnsureCollection ().SyncRoot;
}
}
#endregion #endregion
#region IDictionary Properties #region IDictionary Properties
bool IDictionary.IsFixedSize { Boolean IDictionary.IsFixedSize => this.EnsureDictionary().IsFixedSize;
get {
return EnsureDictionary ().IsFixedSize;
}
}
bool IDictionary.IsReadOnly { Boolean IDictionary.IsReadOnly => this.EnsureDictionary().IsReadOnly;
get {
return EnsureDictionary ().IsReadOnly;
}
}
ICollection IDictionary.Keys { ICollection IDictionary.Keys {
get { get {
EnsureDictionary (); this.EnsureDictionary();
IList<string> keys = new List<string> (); IList<String> keys = new List<String>();
foreach (KeyValuePair<string, JsonData> entry in foreach (KeyValuePair<String, JsonData> entry in this.object_list) {
object_list) {
keys.Add(entry.Key); keys.Add(entry.Key);
} }
@ -135,11 +98,10 @@ namespace LitJson
ICollection IDictionary.Values { ICollection IDictionary.Values {
get { get {
EnsureDictionary (); this.EnsureDictionary();
IList<JsonData> values = new List<JsonData>(); IList<JsonData> values = new List<JsonData>();
foreach (KeyValuePair<string, JsonData> entry in foreach (KeyValuePair<String, JsonData> entry in this.object_list) {
object_list) {
values.Add(entry.Value); values.Add(entry.Value);
} }
@ -148,99 +110,67 @@ namespace LitJson
} }
#endregion #endregion
#region IJsonWrapper Properties #region IJsonWrapper Properties
bool IJsonWrapper.IsArray { Boolean IJsonWrapper.IsArray => this.IsArray;
get { return IsArray; }
}
bool IJsonWrapper.IsBoolean { Boolean IJsonWrapper.IsBoolean => this.IsBoolean;
get { return IsBoolean; }
}
bool IJsonWrapper.IsDouble { Boolean IJsonWrapper.IsDouble => this.IsDouble;
get { return IsDouble; }
}
bool IJsonWrapper.IsInt { Boolean IJsonWrapper.IsInt => this.IsInt;
get { return IsInt; }
}
bool IJsonWrapper.IsLong { Boolean IJsonWrapper.IsLong => this.IsLong;
get { return IsLong; }
}
bool IJsonWrapper.IsObject { Boolean IJsonWrapper.IsObject => this.IsObject;
get { return IsObject; }
}
bool IJsonWrapper.IsString { Boolean IJsonWrapper.IsString => this.IsString;
get { return IsString; }
}
#endregion #endregion
#region IList Properties #region IList Properties
bool IList.IsFixedSize { Boolean IList.IsFixedSize => this.EnsureList().IsFixedSize;
get {
return EnsureList ().IsFixedSize;
}
}
bool IList.IsReadOnly { Boolean IList.IsReadOnly => this.EnsureList().IsReadOnly;
get {
return EnsureList ().IsReadOnly;
}
}
#endregion #endregion
#region IDictionary Indexer #region IDictionary Indexer
object IDictionary.this[object key] { Object IDictionary.this[Object key] {
get { get => this.EnsureDictionary()[key];
return EnsureDictionary ()[key]; set {
if (!(key is String)) {
throw new ArgumentException("The key has to be a string");
} }
set { JsonData data = this.ToJsonData(value);
if (! (key is String))
throw new ArgumentException (
"The key has to be a string");
JsonData data = ToJsonData (value); this[(String)key] = data;
this[(string) key] = data;
} }
} }
#endregion #endregion
#region IOrderedDictionary Indexer #region IOrderedDictionary Indexer
object IOrderedDictionary.this[int idx] { Object IOrderedDictionary.this[Int32 idx] {
get { get {
EnsureDictionary (); this.EnsureDictionary();
return object_list[idx].Value; return this.object_list[idx].Value;
} }
set { set {
EnsureDictionary (); this.EnsureDictionary();
JsonData data = ToJsonData (value); JsonData data = this.ToJsonData(value);
KeyValuePair<string, JsonData> old_entry = object_list[idx]; KeyValuePair<String, JsonData> old_entry = this.object_list[idx];
inst_object[old_entry.Key] = data; this.inst_object[old_entry.Key] = data;
KeyValuePair<string, JsonData> entry = KeyValuePair<String, JsonData> entry = new KeyValuePair<String, JsonData>(old_entry.Key, data);
new KeyValuePair<string, JsonData> (old_entry.Key, data);
object_list[idx] = entry; this.object_list[idx] = entry;
} }
} }
#endregion #endregion
#region IList Indexer #region IList Indexer
object IList.this[int index] { object IList.this[int index]
{
get { get {
return EnsureList()[index]; return EnsureList()[index];
} }
@ -256,7 +186,8 @@ namespace LitJson
#region Public Indexers #region Public Indexers
public JsonData this[string prop_name] { public JsonData this[string prop_name]
{
get { get {
EnsureDictionary(); EnsureDictionary();
return inst_object[prop_name]; return inst_object[prop_name];
@ -284,7 +215,8 @@ namespace LitJson
} }
} }
public JsonData this[int index] { public JsonData this[int index]
{
get { get {
EnsureCollection(); EnsureCollection();
@ -971,23 +903,27 @@ namespace LitJson
IEnumerator<KeyValuePair<string, JsonData>> list_enumerator; IEnumerator<KeyValuePair<string, JsonData>> list_enumerator;
public object Current { public object Current
{
get { return Entry; } get { return Entry; }
} }
public DictionaryEntry Entry { public DictionaryEntry Entry
{
get { get {
KeyValuePair<string, JsonData> curr = list_enumerator.Current; KeyValuePair<string, JsonData> curr = list_enumerator.Current;
return new DictionaryEntry(curr.Key, curr.Value); return new DictionaryEntry(curr.Key, curr.Value);
} }
} }
public object Key { public object Key
{
get { return list_enumerator.Current.Key; } get { return list_enumerator.Current.Key; }
} }
public object Value { public object Value
{
get { return list_enumerator.Current.Value; } get { return list_enumerator.Current.Value; }
} }

View File

@ -28,15 +28,8 @@ namespace LitJson {
private Type element_type; private Type element_type;
public Type ElementType { public Type ElementType {
get { get => this.element_type ?? typeof(JsonData);
if (this.element_type == null) { set => this.element_type = value;
return typeof(JsonData);
}
return this.element_type;
}
set { this.element_type = value; }
} }
public Boolean IsArray { get; set; } public Boolean IsArray { get; set; }
@ -48,15 +41,8 @@ namespace LitJson {
private Type element_type; private Type element_type;
public Type ElementType { public Type ElementType {
get { get => this.element_type ?? typeof(JsonData);
if (this.element_type == null) { set => this.element_type = value;
return typeof(JsonData);
}
return this.element_type;
}
set { this.element_type = value; }
} }
public Boolean IsDictionary { get; set; } public Boolean IsDictionary { get; set; }
@ -64,7 +50,6 @@ namespace LitJson {
public IDictionary<String, PropertyMetadata> Properties { get; set; } public IDictionary<String, PropertyMetadata> Properties { get; set; }
} }
internal delegate void ExporterFunc(Object obj, JsonWriter writer); internal delegate void ExporterFunc(Object obj, JsonWriter writer);
public delegate void ExporterFunc<T>(T obj, JsonWriter writer); public delegate void ExporterFunc<T>(T obj, JsonWriter writer);
@ -340,8 +325,9 @@ namespace LitJson {
// Maybe it's an enum // Maybe it's an enum
#if NETSTANDARD1_5 #if NETSTANDARD1_5
if (value_type.IsEnum()) if (value_type.IsEnum()) {
return Enum.ToObject (value_type, reader.Value); return Enum.ToObject (value_type, reader.Value);
}
#else #else
if (value_type.IsEnum) { if (value_type.IsEnum) {
return Enum.ToObject(value_type, reader.Value); return Enum.ToObject(value_type, reader.Value);
@ -514,9 +500,7 @@ namespace LitJson {
return instance; return instance;
} }
private static void ReadSkip(JsonReader reader) { private static void ReadSkip(JsonReader reader) => ToWrapper(delegate { return new JsonMockWrapper(); }, reader);
ToWrapper(delegate { return new JsonMockWrapper(); }, reader);
}
private static void RegisterBaseExporters() { private static void RegisterBaseExporters() {
base_exporters_table[typeof(Byte)] = delegate (Object obj, JsonWriter writer) { base_exporters_table[typeof(Byte)] = delegate (Object obj, JsonWriter writer) {
@ -769,7 +753,6 @@ namespace LitJson {
} }
#endregion #endregion
public static String ToJson(Object obj) { public static String ToJson(Object obj) {
lock (static_writer_lock) { lock (static_writer_lock) {
static_writer.Reset(); static_writer.Reset();
@ -780,78 +763,32 @@ namespace LitJson {
} }
} }
public static void ToJson(Object obj, JsonWriter writer) { public static void ToJson(Object obj, JsonWriter writer) => WriteValue(obj, writer, false, 0);
WriteValue(obj, writer, false, 0);
}
public static JsonData ToObject(JsonReader reader) { public static JsonData ToObject(JsonReader reader) => (JsonData)ToWrapper(delegate { return new JsonData(); }, reader);
return (JsonData)ToWrapper(delegate { return new JsonData(); }, reader);
}
public static JsonData ToObject(TextReader reader) { public static JsonData ToObject(TextReader reader) => (JsonData)ToWrapper(delegate { return new JsonData(); }, new JsonReader(reader));
JsonReader json_reader = new JsonReader(reader);
return (JsonData)ToWrapper(delegate { return new JsonData(); }, json_reader); public static JsonData ToObject(String json) => (JsonData)ToWrapper(delegate { return new JsonData(); }, json);
}
public static JsonData ToObject(String json) { public static T ToObject<T>(JsonReader reader) => (T)ReadValue(typeof(T), reader);
return (JsonData)ToWrapper(delegate { return new JsonData(); }, json);
}
public static T ToObject<T>(JsonReader reader) { public static T ToObject<T>(TextReader reader) => (T)ReadValue(typeof(T), new JsonReader(reader));
return (T)ReadValue(typeof(T), reader);
}
public static T ToObject<T>(TextReader reader) { public static T ToObject<T>(String json) => (T)ReadValue(typeof(T), new JsonReader(json));
JsonReader json_reader = new JsonReader(reader);
return (T)ReadValue(typeof(T), json_reader); public static Object ToObject(String json, Type ConvertType) => ReadValue(ConvertType, new JsonReader(json));
}
public static T ToObject<T>(String json) { public static IJsonWrapper ToWrapper(WrapperFactory factory, JsonReader reader) => ReadValue(factory, reader);
JsonReader reader = new JsonReader(json);
return (T)ReadValue(typeof(T), reader); public static IJsonWrapper ToWrapper(WrapperFactory factory, String json) => ReadValue(factory, new JsonReader(json));
}
public static Object ToObject(String json, Type ConvertType) { public static void RegisterExporter<T>(ExporterFunc<T> exporter) => custom_exporters_table[typeof(T)] = (Object obj, JsonWriter writer) => { exporter((T)obj, writer); };
JsonReader reader = new JsonReader(json);
return ReadValue(ConvertType, reader); public static void RegisterImporter<TJson, TValue>(ImporterFunc<TJson, TValue> importer) => RegisterImporter(custom_importers_table, typeof(TJson), typeof(TValue), (Object input) => { return importer((TJson)input); });
}
public static IJsonWrapper ToWrapper(WrapperFactory factory, JsonReader reader) { public static void UnregisterExporters() => custom_exporters_table.Clear();
return ReadValue(factory, reader);
}
public static IJsonWrapper ToWrapper(WrapperFactory factory, String json) { public static void UnregisterImporters() => custom_importers_table.Clear();
JsonReader reader = new JsonReader(json);
return ReadValue(factory, reader);
}
public static void RegisterExporter<T>(ExporterFunc<T> exporter) {
ExporterFunc exporter_wrapper = delegate (Object obj, JsonWriter writer) {
exporter((T)obj, writer);
};
custom_exporters_table[typeof(T)] = exporter_wrapper;
}
public static void RegisterImporter<TJson, TValue>(ImporterFunc<TJson, TValue> importer) {
ImporterFunc importer_wrapper = delegate (Object input) {
return importer((TJson)input);
};
RegisterImporter(custom_importers_table, typeof(TJson), typeof(TValue), importer_wrapper);
}
public static void UnregisterExporters() {
custom_exporters_table.Clear();
}
public static void UnregisterImporters() {
custom_importers_table.Clear();
}
} }
} }