Purge Newtonsoft state annotations
This commit is contained in:
parent
639e701d63
commit
b0d2aa1156
92 changed files with 891 additions and 1684 deletions
|
|
@ -1,13 +1,11 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using JsonKnownTypes;
|
||||
using MessagePack;
|
||||
using MessagePack.Formatters;
|
||||
using Newtonsoft.Json;
|
||||
// TODO: USE THIS EVERYWHERE
|
||||
using Unity.Mathematics;
|
||||
using static Unity.Mathematics.math;
|
||||
|
|
@ -18,42 +16,40 @@ public interface INamedEntry
|
|||
}
|
||||
|
||||
[MessagePackObject,
|
||||
JsonObject(MemberSerialization.OptIn),
|
||||
JsonConverter(typeof(JsonKnownTypesConverter<DatabaseEntry>)),
|
||||
Union(0, typeof(SimpleCommodityData)),
|
||||
Union(0, typeof(SimpleCommodityData)),
|
||||
Union(1, typeof(CompoundCommodityData)),
|
||||
Union(2, typeof(GearData)),
|
||||
Union(3, typeof(HullData)),
|
||||
Union(4, typeof(SimpleCommodity)),
|
||||
Union(5, typeof(CompoundCommodity)),
|
||||
Union(2, typeof(GearData)),
|
||||
Union(3, typeof(HullData)),
|
||||
Union(4, typeof(SimpleCommodity)),
|
||||
Union(5, typeof(CompoundCommodity)),
|
||||
Union(6, typeof(EquippableItem)),
|
||||
Union(8, typeof(GalaxyMapLayerData)),
|
||||
Union(9, typeof(NameFile)),
|
||||
//Union(10, typeof(ZoneData)),
|
||||
Union(11, typeof(PlayerData)),
|
||||
Union(9, typeof(NameFile)),
|
||||
//Union(10, typeof(ZoneData)),
|
||||
Union(11, typeof(PlayerData)),
|
||||
// Union(12, typeof(Corporation)),
|
||||
Union(13, typeof(Faction)),
|
||||
Union(14, typeof(OrbitalEntity)),
|
||||
Union(15, typeof(OrbitData)),
|
||||
Union(14, typeof(OrbitalEntity)),
|
||||
Union(15, typeof(OrbitData)),
|
||||
Union(16, typeof(BodyData)),
|
||||
Union(17, typeof(PersonalityAttribute)),
|
||||
Union(18, typeof(AgentTask)),
|
||||
// Union(19, typeof(LoadoutData)),
|
||||
Union(20, typeof(Ship)),
|
||||
Union(21, typeof(Mining)),
|
||||
Union(22, typeof(StationTowing)),
|
||||
Union(23, typeof(Survey)),
|
||||
Union(24, typeof(HaulingTask)),
|
||||
Union(25, typeof(AsteroidBeltData)),
|
||||
Union(26, typeof(GasGiantData)),
|
||||
Union(27, typeof(SunData)),
|
||||
Union(28, typeof(PlanetData)),
|
||||
Union(29, typeof(CargoBayData)),
|
||||
Union(30, typeof(DockingBayData)),
|
||||
Union(22, typeof(StationTowing)),
|
||||
Union(23, typeof(Survey)),
|
||||
Union(24, typeof(HaulingTask)),
|
||||
Union(25, typeof(AsteroidBeltData)),
|
||||
Union(26, typeof(GasGiantData)),
|
||||
Union(27, typeof(SunData)),
|
||||
Union(28, typeof(PlanetData)),
|
||||
Union(29, typeof(CargoBayData)),
|
||||
Union(30, typeof(DockingBayData)),
|
||||
Union(31, typeof(WeaponItemData))]
|
||||
public abstract class DatabaseEntry
|
||||
{
|
||||
[JsonProperty("id"), Key(0)]
|
||||
[Key(0)]
|
||||
public Guid ID = Guid.NewGuid();
|
||||
|
||||
public override int GetHashCode()
|
||||
|
|
@ -68,19 +64,19 @@ public abstract class DatabaseEntry
|
|||
}
|
||||
}
|
||||
|
||||
[MessagePackObject, JsonObject(MemberSerialization.OptIn)]
|
||||
[MessagePackObject]
|
||||
public class DatabaseLink<T> : DatabaseLinkBase where T : DatabaseEntry
|
||||
{
|
||||
[IgnoreMember]
|
||||
public T Value => Cache.Get<T>(LinkID);
|
||||
}
|
||||
|
||||
[MessagePackObject, JsonObject(MemberSerialization.OptIn)]
|
||||
[MessagePackObject]
|
||||
public class DatabaseLinkBase
|
||||
{
|
||||
[Key(0), JsonProperty("linkID")]
|
||||
[Key(0)]
|
||||
public Guid LinkID;
|
||||
|
||||
|
||||
[IgnoreMember]
|
||||
public static CultCache Cache;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,169 +0,0 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Unity.Mathematics;
|
||||
using static Unity.Mathematics.math;
|
||||
|
||||
public class MathJsonConverter : JsonConverter
|
||||
{
|
||||
private readonly Dictionary<Type, Action<JsonWriter, object>> _writers;
|
||||
private readonly Dictionary<Type, Func<JArray, object>> _readers;
|
||||
|
||||
public MathJsonConverter()
|
||||
{
|
||||
_writers = GenerateWriters();
|
||||
_readers = GenerateReaders();
|
||||
}
|
||||
|
||||
private Dictionary<Type, Action<JsonWriter, object>> GenerateWriters()
|
||||
{
|
||||
var writers = new Dictionary<Type, Action<JsonWriter, object>>();
|
||||
|
||||
writers[typeof(float2)] = (writer, o) =>
|
||||
{
|
||||
if (!(o is float2 v))
|
||||
throw new JsonReaderException();
|
||||
|
||||
writer.WriteStartArray();
|
||||
writer.WriteValue(v.x);
|
||||
writer.WriteValue(v.y);
|
||||
writer.WriteEndArray();
|
||||
};
|
||||
|
||||
writers[typeof(int2)] = (writer, o) =>
|
||||
{
|
||||
if (!(o is int2 v))
|
||||
throw new JsonReaderException();
|
||||
|
||||
writer.WriteStartArray();
|
||||
writer.WriteValue(v.x);
|
||||
writer.WriteValue(v.y);
|
||||
writer.WriteEndArray();
|
||||
};
|
||||
|
||||
writers[typeof(float3)] = (writer, o) =>
|
||||
{
|
||||
if (!(o is float3 v))
|
||||
throw new JsonReaderException();
|
||||
|
||||
writer.WriteStartArray();
|
||||
writer.WriteValue(v.x);
|
||||
writer.WriteValue(v.y);
|
||||
writer.WriteValue(v.z);
|
||||
writer.WriteEndArray();
|
||||
};
|
||||
|
||||
writers[typeof(float4)] = (writer, o) =>
|
||||
{
|
||||
if (!(o is float4 v))
|
||||
throw new JsonReaderException();
|
||||
|
||||
writer.WriteStartArray();
|
||||
writer.WriteValue(v.x);
|
||||
writer.WriteValue(v.y);
|
||||
writer.WriteValue(v.z);
|
||||
writer.WriteValue(v.w);
|
||||
writer.WriteEndArray();
|
||||
};
|
||||
|
||||
return writers;
|
||||
}
|
||||
|
||||
private Dictionary<Type, Func<JArray, object>> GenerateReaders()
|
||||
{
|
||||
var readers = new Dictionary<Type, Func<JArray, object>>();
|
||||
|
||||
readers[typeof(float2)] = array =>
|
||||
{
|
||||
if (array.Count < 2)
|
||||
{
|
||||
throw new JsonReaderException(
|
||||
$"Could not read {typeof(float2)} from json, expected a json array with 2 elements");
|
||||
}
|
||||
|
||||
return float2(
|
||||
array[0].ToObject<float>(),
|
||||
array[1].ToObject<float>());
|
||||
};
|
||||
|
||||
readers[typeof(int2)] = array =>
|
||||
{
|
||||
if (array.Count < 2)
|
||||
{
|
||||
throw new JsonReaderException(
|
||||
$"Could not read {typeof(float2)} from json, expected a json array with 2 elements");
|
||||
}
|
||||
|
||||
return int2(
|
||||
array[0].ToObject<int>(),
|
||||
array[1].ToObject<int>());
|
||||
};
|
||||
|
||||
readers[typeof(float3)] = array =>
|
||||
{
|
||||
if (array.Count < 3)
|
||||
{
|
||||
throw new JsonReaderException(
|
||||
$"Could not read {typeof(float3)} from json, expected a json array with 3 elements");
|
||||
}
|
||||
|
||||
return float3(
|
||||
array[0].ToObject<float>(),
|
||||
array[1].ToObject<float>(),
|
||||
array[2].ToObject<float>());
|
||||
};
|
||||
|
||||
readers[typeof(float4)] = array =>
|
||||
{
|
||||
if (array.Count < 4)
|
||||
{
|
||||
throw new JsonReaderException(
|
||||
$"Could not read {typeof(float4)} from json, expected a json array with 4 elements");
|
||||
}
|
||||
|
||||
return float4(
|
||||
array[0].ToObject<float>(),
|
||||
array[1].ToObject<float>(),
|
||||
array[2].ToObject<float>(),
|
||||
array[3].ToObject<float>());
|
||||
};
|
||||
|
||||
return readers;
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
if (_writers.TryGetValue(value.GetType(), out var serializeAction))
|
||||
serializeAction(writer, value);
|
||||
else
|
||||
throw new JsonReaderException($"Invalid type for ValueConverter:{value.GetType()}");
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
if (_readers.TryGetValue(objectType, out var deserializeFunc))
|
||||
{
|
||||
// Convert token from reader to array for deserialize function
|
||||
var token = JToken.ReadFrom(reader);
|
||||
if (!(token is JArray asArray))
|
||||
throw new JsonReaderException(
|
||||
$"Could not read {objectType} from json, expected a json array but got {token.Type}");
|
||||
|
||||
return deserializeFunc(asArray);
|
||||
}
|
||||
|
||||
throw new JsonReaderException($"Invalid type for ValueConverter:{objectType}");
|
||||
}
|
||||
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
//If the dictionary contains the key, the type can be handled by this class
|
||||
return _writers.ContainsKey(objectType);
|
||||
}
|
||||
|
||||
//Enable this JSON Converter for reading as well as writing values
|
||||
public override bool CanRead => true;
|
||||
public override bool CanWrite => true;
|
||||
}
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
fileFormatVersion: 2
|
||||
guid: fafad9d553198e942a5501ea1472ec9c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
fileFormatVersion: 2
|
||||
guid: f94c63c8b140fb34bb5d70a8d8a5dbd6
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -1,16 +0,0 @@
|
|||
using System;
|
||||
|
||||
namespace JsonKnownTypes
|
||||
{
|
||||
public static class AttributesManager
|
||||
{
|
||||
public static JsonKnownTypeAttribute[] GetJsonKnownAttributes(Type type) =>
|
||||
(JsonKnownTypeAttribute[])Attribute.GetCustomAttributes(type, typeof(JsonKnownTypeAttribute));
|
||||
|
||||
public static JsonKnownThisTypeAttribute GetJsonKnownThisAttribute(Type type) =>
|
||||
(JsonKnownThisTypeAttribute)Attribute.GetCustomAttribute(type, typeof(JsonKnownThisTypeAttribute));
|
||||
|
||||
public static JsonDiscriminatorAttribute GetJsonDiscriminatorAttribute(Type type) =>
|
||||
(JsonDiscriminatorAttribute)Attribute.GetCustomAttribute(type, typeof(JsonDiscriminatorAttribute));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 6036d25d64e90064ca92c91f6d7827b2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 01722fba3f2fd3942bca61d48508ad27
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
using System;
|
||||
|
||||
namespace JsonKnownTypes.Exceptions
|
||||
{
|
||||
public class JsonKnownTypesException : Exception
|
||||
{
|
||||
public JsonKnownTypesException(string message)
|
||||
: base(message)
|
||||
{ }
|
||||
|
||||
public JsonKnownTypesException(string message, Exception innerException)
|
||||
: base(message, innerException)
|
||||
{ }
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 36f9ed87d773cf146bf68e828cefe89c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
using System;
|
||||
|
||||
namespace JsonKnownTypes
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)]
|
||||
public class JsonDiscriminatorAttribute : Attribute
|
||||
{
|
||||
public string Name { get; set; }
|
||||
|
||||
internal bool? _autoJson;
|
||||
|
||||
public bool AutoJson
|
||||
{
|
||||
get => _autoJson != null && (bool) _autoJson;
|
||||
set => _autoJson = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
fileFormatVersion: 2
|
||||
guid: f7c6582f43e22994ebbaf0e139e63dc5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
namespace JsonKnownTypes
|
||||
{
|
||||
public class JsonDiscriminatorSettings
|
||||
{
|
||||
public string Name { get; set; } = "$type";
|
||||
public bool AutoJsonKnown { get; set; } = true;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
fileFormatVersion: 2
|
||||
guid: b5f09ee78f257904caa3ab939d3edc65
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
using System;
|
||||
|
||||
namespace JsonKnownTypes
|
||||
{
|
||||
public class JsonKnownThisTypeAttribute : Attribute
|
||||
{
|
||||
public string Discriminator { get; }
|
||||
|
||||
public JsonKnownThisTypeAttribute()
|
||||
{ }
|
||||
|
||||
public JsonKnownThisTypeAttribute(string discriminator)
|
||||
{
|
||||
Discriminator = discriminator;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c8dc4cbd1c54167409cf14ec8ec37d70
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
using System;
|
||||
|
||||
namespace JsonKnownTypes
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, AllowMultiple = true)]
|
||||
public class JsonKnownTypeAttribute : Attribute
|
||||
{
|
||||
public Type Type { get; }
|
||||
public string Discriminator { get; }
|
||||
|
||||
public JsonKnownTypeAttribute(Type type)
|
||||
{
|
||||
Type = type;
|
||||
}
|
||||
|
||||
public JsonKnownTypeAttribute(Type type, string discriminator)
|
||||
{
|
||||
Type = type;
|
||||
Discriminator = discriminator;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
fileFormatVersion: 2
|
||||
guid: ecbe3646a4d9ca04eac598b60e5c77ca
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Version>0.2.0</Version>
|
||||
<Authors>Dmitry Kaznacheev</Authors>
|
||||
<PackageProjectUrl>https://github.com/dmitry-bym/JsonKnownTypes</PackageProjectUrl>
|
||||
<PackageLicenseUrl>https://github.com/dmitry-bym/JsonKnownTypes/blob/master/LICENSE</PackageLicenseUrl>
|
||||
<Copyright>Dmitry Kaznacheev</Copyright>
|
||||
<RepositoryUrl>https://github.com/dmitry-bym/JsonKnownTypes</RepositoryUrl>
|
||||
<PackageTags>json known polymorphic types</PackageTags>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 937d902757d946b47a7bea20ade37beb
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -1,16 +0,0 @@
|
|||
using System;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
|
||||
namespace JsonKnownTypes
|
||||
{
|
||||
public class JsonKnownTypesContractResolver<T> : DefaultContractResolver
|
||||
{
|
||||
protected override JsonConverter ResolveContractConverter(Type objectType)
|
||||
{
|
||||
if (typeof(T).IsAssignableFrom(objectType) && !objectType.IsAbstract)
|
||||
return null;
|
||||
return base.ResolveContractConverter(objectType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
fileFormatVersion: 2
|
||||
guid: bce1b7fa2da230447b3614d23394ee9f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -1,57 +0,0 @@
|
|||
using System;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace JsonKnownTypes
|
||||
{
|
||||
public class JsonKnownTypesConverter<T> : JsonConverter
|
||||
{
|
||||
private readonly JsonKnownTypesSettings _typesSettings
|
||||
= JsonKnownTypesSettingsManager.GetSettings<T>();
|
||||
|
||||
private JsonSerializerSettings SpecifiedSubclassConversion
|
||||
{
|
||||
get =>
|
||||
new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = new JsonKnownTypesContractResolver<T>()
|
||||
};
|
||||
}
|
||||
|
||||
public override bool CanConvert(Type objectType)
|
||||
=> _typesSettings.TypeToDiscriminator.ContainsKey(objectType);
|
||||
|
||||
public override bool CanWrite { get => true; }
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
if (reader.TokenType == JsonToken.Null) return null;
|
||||
|
||||
var jo = JObject.Load(reader);
|
||||
|
||||
var discriminator = jo[_typesSettings.Name].ToString();
|
||||
|
||||
if(_typesSettings.DiscriminatorToType.TryGetValue(discriminator, out var typeForObject))
|
||||
return JsonConvert.DeserializeObject(jo.ToString(), typeForObject, SpecifiedSubclassConversion);
|
||||
|
||||
return null;
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
var objectType = value.GetType();
|
||||
if (_typesSettings.TypeToDiscriminator.TryGetValue(objectType, out var discriminator))
|
||||
{
|
||||
var json = JsonConvert.SerializeObject(value, SpecifiedSubclassConversion);
|
||||
var jo = JObject.Parse(json);
|
||||
jo.Add(_typesSettings.Name, new JValue(discriminator));
|
||||
jo.WriteTo(writer);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException($"Json Known Types: Type Unknown {objectType.FullName}!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
fileFormatVersion: 2
|
||||
guid: b0a6c832fc846634789aa691cd2b81d9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -1,64 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using JsonKnownTypes.Exceptions;
|
||||
|
||||
namespace JsonKnownTypes
|
||||
{
|
||||
internal static class JsonKnownTypesDictionaryManager
|
||||
{
|
||||
public static void AddJsonKnown<T>(this Dictionary<Type, string> dictionary)
|
||||
{
|
||||
var attrs = AttributesManager.GetJsonKnownAttributes(typeof(T));
|
||||
foreach (var attr in attrs)
|
||||
{
|
||||
var discriminator = attr.Discriminator ?? attr.Type.Name;
|
||||
dictionary.TryAdd(attr.Type, discriminator);
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddJsonKnownThis(this Dictionary<Type, string> dictionary, Type[] inherited)
|
||||
{
|
||||
foreach (var type in inherited)
|
||||
{
|
||||
var attr = AttributesManager.GetJsonKnownThisAttribute(type);
|
||||
if (attr != null)
|
||||
{
|
||||
var discriminator = attr.Discriminator ?? type.Name;
|
||||
dictionary.TryAdd(type, discriminator);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddAutoGenerated(this Dictionary<Type, string> dictionary, Type[] inherited)
|
||||
{
|
||||
foreach (var type in inherited.Except(dictionary.Keys))
|
||||
{
|
||||
dictionary.TryAdd(type, type.Name);
|
||||
}
|
||||
}
|
||||
|
||||
public static void TryAdd(this Dictionary<Type, string> dictionary, Type key, string discriminator)
|
||||
{
|
||||
if (dictionary.ContainsKey(key))
|
||||
throw new JsonKnownTypesException($"Dictionary already contains the key {key.Name}");//todo
|
||||
|
||||
if (dictionary.ContainsValue(discriminator))
|
||||
throw new JsonKnownTypesException($"Dictionary already contains the value {discriminator}");//todo
|
||||
|
||||
dictionary.Add(key, discriminator);
|
||||
}
|
||||
|
||||
public static Dictionary<string, Type> Revert(this Dictionary<Type, string> dictionary)
|
||||
{
|
||||
try
|
||||
{
|
||||
return dictionary.ToDictionary(x => x.Value, x => x.Key);
|
||||
}
|
||||
catch (ArgumentException e)
|
||||
{
|
||||
throw new JsonKnownTypesException("Dictionary has a repeated value", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 77ce621509349204a87085f089b3daa3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace JsonKnownTypes
|
||||
{
|
||||
public class JsonKnownTypesSettings
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public Dictionary<string, Type> DiscriminatorToType { get; set; } = new Dictionary<string, Type>();
|
||||
public Dictionary<Type, string> TypeToDiscriminator { get; set; } = new Dictionary<Type, string>();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 99b988650c739094aa3a5ba7040ea379
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -1,60 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using JsonKnownTypes.Exceptions;
|
||||
using JsonKnownTypes.Utils;
|
||||
|
||||
namespace JsonKnownTypes
|
||||
{
|
||||
public static class JsonKnownTypesSettingsManager
|
||||
{
|
||||
public static JsonDiscriminatorSettings DefaultDiscriminatorSettings { get; set; } = new JsonDiscriminatorSettings();
|
||||
|
||||
private static HashSet<Assembly> RegisteredAssemblies = new HashSet<Assembly>();
|
||||
public static void RegisterTypeAssembly<T>()
|
||||
{
|
||||
var type = typeof(T);
|
||||
RegisteredAssemblies.Add(type.Assembly);
|
||||
}
|
||||
|
||||
public static JsonKnownTypesSettings GetSettings<T>()
|
||||
{
|
||||
var discriminatorAttribute = AttributesManager.GetJsonDiscriminatorAttribute(typeof(T));
|
||||
|
||||
var discriminatorSettings = discriminatorAttribute == null ? DefaultDiscriminatorSettings : Mapper.Map(discriminatorAttribute);
|
||||
|
||||
var typeSettings = new JsonKnownTypesSettings
|
||||
{
|
||||
Name = discriminatorSettings.Name
|
||||
};
|
||||
|
||||
var allTypes = GetAllInheritance<T>();
|
||||
|
||||
typeSettings.TypeToDiscriminator.AddJsonKnown<T>();
|
||||
typeSettings.TypeToDiscriminator.AddJsonKnownThis(allTypes);
|
||||
|
||||
if (discriminatorSettings.AutoJsonKnown)
|
||||
{
|
||||
typeSettings.TypeToDiscriminator.AddAutoGenerated(allTypes);
|
||||
}
|
||||
else if(!allTypes.All(typeSettings.TypeToDiscriminator.ContainsKey))
|
||||
{
|
||||
throw new JsonKnownTypesException("Dictionary contains not all types of hierarchy, enable AutoJsonKnow or add attributes for all classes"); //todo
|
||||
}
|
||||
|
||||
typeSettings.DiscriminatorToType = typeSettings.TypeToDiscriminator.Revert();
|
||||
|
||||
return typeSettings;
|
||||
}
|
||||
|
||||
private static Type[] GetAllInheritance<T>()
|
||||
{
|
||||
RegisterTypeAssembly<T>();
|
||||
var type = typeof(T);
|
||||
return RegisteredAssemblies.SelectMany(a=>a.GetTypes())
|
||||
.Where(x => type.IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract)
|
||||
.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
fileFormatVersion: 2
|
||||
guid: a361505c5408b374788cbaeed6fa2f55
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 6d6706b0998d8af458321fd5f35906fb
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
namespace JsonKnownTypes.Utils
|
||||
{
|
||||
internal static class Mapper
|
||||
{
|
||||
public static JsonDiscriminatorSettings Map(JsonDiscriminatorAttribute entity)
|
||||
{
|
||||
var settings = new JsonDiscriminatorSettings();
|
||||
|
||||
settings.Name = entity.Name ?? settings.Name;
|
||||
settings.AutoJsonKnown = entity._autoJson ?? settings.AutoJsonKnown;
|
||||
|
||||
return settings;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c30d51fc767627a41b65f5cdd2b22cc3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Loading…
Reference in a new issue