make it nice
This commit is contained in:
parent
021417c029
commit
81b0cb3990
1844
litjson/JsonData.cs
1844
litjson/JsonData.cs
File diff suppressed because it is too large
Load Diff
@ -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);
|
||||||
|
|
||||||
@ -291,15 +276,15 @@ namespace LitJson {
|
|||||||
Type value_type = underlying_type ?? inst_type;
|
Type value_type = underlying_type ?? inst_type;
|
||||||
|
|
||||||
if (reader.Token == JsonToken.Null) {
|
if (reader.Token == JsonToken.Null) {
|
||||||
#if NETSTANDARD1_5
|
#if NETSTANDARD1_5
|
||||||
if (inst_type.IsClass() || underlying_type != null) {
|
if (inst_type.IsClass() || underlying_type != null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
if (inst_type.IsClass || underlying_type != null) {
|
if (inst_type.IsClass || underlying_type != null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
throw new JsonException(String.Format("Can't assign null to an instance of type {0}", inst_type));
|
throw new JsonException(String.Format("Can't assign null to an instance of type {0}", inst_type));
|
||||||
}
|
}
|
||||||
@ -339,14 +324,15 @@ 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
|
}
|
||||||
if (value_type.IsEnum) {
|
#else
|
||||||
return Enum.ToObject(value_type, reader.Value);
|
if (value_type.IsEnum) {
|
||||||
}
|
return Enum.ToObject(value_type, reader.Value);
|
||||||
#endif
|
}
|
||||||
|
#endif
|
||||||
// Try using an implicit conversion operator
|
// Try using an implicit conversion operator
|
||||||
MethodInfo conv_op = GetConvOp(value_type, json_type);
|
MethodInfo conv_op = GetConvOp(value_type, json_type);
|
||||||
|
|
||||||
@ -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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user