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

View File

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