Skip to content

Commit

Permalink
Json.NET Unity Converters 1.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
applejag committed May 30, 2021
1 parent 1ddeb20 commit 85d4fd5
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 38 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Unity Converters for Newtonsoft.Json changelog

## 1.1.1 (2021-05-30)

- Fixed Newtonsoft.Json converters (ex: `StringEnumConverter` &
`VersionConverter`) not being loaded even if you had then enabled in the
Newtonsoft.Json-for-Unity.Converters config.
([#55](https://github.com/jilleJr/Newtonsoft.Json-for-Unity.Converters/issues/55))

## 1.1.0 (2021-04-05)

- Added configurability to enable/disable any converter that was all previously
Expand Down
5 changes: 5 additions & 0 deletions Configuration/ConverterConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public struct ConverterConfig : IEquatable<ConverterConfig>

public List<KeyedConfig> settings;

public override string ToString()
{
return $"{{enabled={enabled}, converterName={converterName}, settings=[{settings?.Count ?? 0}]}}";
}

public override bool Equals(object obj)
{
return obj is ConverterConfig config && Equals(config);
Expand Down
41 changes: 6 additions & 35 deletions Editor/UnityConvertersConfigEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
using UnityEditor;
using UnityEditor.AnimatedValues;
using UnityEngine;
// Unity 2020 also has a type cache: UnityEditor.TypeCache:
// https://docs.unity3d.com/2019.2/Documentation/ScriptReference/TypeCache.html
using TypeCache = Newtonsoft.Json.UnityConverters.Utility.TypeCache;

namespace Newtonsoft.Json.UnityConverters.Editor
{
Expand All @@ -25,9 +28,6 @@ public class UnityConvertersConfigEditor : Editor
private AnimBool _unityConvertersShow;
private AnimBool _jsonNetConvertersShow;

private readonly Dictionary<string, Type> _converterTypeByName
= new Dictionary<string, Type>();

private GUIStyle _headerStyle;
private GUIStyle _boldHeaderStyle;

Expand All @@ -44,7 +44,7 @@ private void OnEnable()
try
{
_ = serializedObject;
}
}
catch (Exception)
{
return;
Expand Down Expand Up @@ -125,7 +125,7 @@ private void AddMissingConverters(SerializedProperty arrayProperty, IEnumerable<
{
var elements = EnumerateArrayElements(arrayProperty);
var elementTypes = elements
.Select(e => FindType(e.FindPropertyRelative(nameof(ConverterConfig.converterName)).stringValue))
.Select(e => TypeCache.FindType(e.FindPropertyRelative(nameof(ConverterConfig.converterName)).stringValue))
.ToArray();
Type[] missingConverters = converterTypes
.Where(type => !elementTypes.Contains(type))
Expand Down Expand Up @@ -196,7 +196,7 @@ private void FoldoutConvertersList(SerializedProperty property, AnimBool fadedAn
var allConfigsWithType = EnumerateArrayElements(property)
.Select(o => (
serializedProperty: o,
type: FindType(o.FindPropertyRelative(nameof(ConverterConfig.converterName)).stringValue)
type: TypeCache.FindType(o.FindPropertyRelative(nameof(ConverterConfig.converterName)).stringValue)
));

foreach (var namespaceGroup in allConfigsWithType.GroupBy(o => GetTypeNamespace(o.type)))
Expand Down Expand Up @@ -249,35 +249,6 @@ private void FoldoutConvertersList(SerializedProperty property, AnimBool fadedAn
EditorGUILayout.EndFadeGroup();
}

private Type FindType(string name)
{
if (_converterTypeByName.TryGetValue(name, out var type))
{
return type;
}
else
{
// Check this assembly, or if it has AssemblyQualifiedName
type = Type.GetType(name);
if (type != null)
{
_converterTypeByName[name] = type;
return type;
}

// Check all the other assemblies, from last imported to first
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies().Reverse())
{
type = assembly.GetType(name);
if (type != null)
{
_converterTypeByName[name] = type;
return type;
}
}
return null;
}
}

private static string GetNamespaceHeader(IGrouping<string, (SerializedProperty serializedProperty, Type type)> namespaceGroup)
{
Expand Down
8 changes: 8 additions & 0 deletions Properties.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Newtonsoft.Json.UnityConverters.Editor")]
11 changes: 11 additions & 0 deletions Properties/AssemblyInfo.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions UnityConverters/UnityConverterInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.UnityConverters.Configuration;
using Newtonsoft.Json.UnityConverters.Helpers;
using Newtonsoft.Json.UnityConverters.Utility;
using UnityEngine;

namespace Newtonsoft.Json.UnityConverters
Expand Down Expand Up @@ -114,7 +115,8 @@ private static JsonSerializerSettings CreateJsonSettingsWithFreslyLoadedConfig()
{
var config = Resources.Load<UnityConvertersConfig>(UnityConvertersConfig.PATH_FOR_RESOURCES_LOAD);

if (!config) {
if (!config)
{
config = ScriptableObject.CreateInstance<UnityConvertersConfig>();
}

Expand Down Expand Up @@ -219,7 +221,7 @@ private static IEnumerable<Type> ApplyConfigFilter(IEnumerable<Type> types, bool

var typesOfEnabledThroughConfig = configs
.Where(o => o.enabled)
.Select(o => Type.GetType(o.converterName))
.Select(o => TypeCache.FindType(o.converterName))
.Where(o => o != null);

var hashMap = new HashSet<Type>(typesOfEnabledThroughConfig);
Expand Down
8 changes: 8 additions & 0 deletions Utility.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions Utility/TypeCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace Newtonsoft.Json.UnityConverters.Utility
{
internal static class TypeCache
{
private static readonly Dictionary<string, Type> _typeByName
= new Dictionary<string, Type>();

private static readonly Assembly[] _assemblies
= AppDomain.CurrentDomain.GetAssemblies()
// Reversing so we get last imported assembly first.
// When searching for types we want to look in mscorlib last
// and Newtonsoft.Json up as the first ones
.Reverse()
.ToArray();

public static Type FindType(string name)
{
if (_typeByName.TryGetValue(name, out var type))
{
return type;
}
else
{
// Check this assembly, or if it has AssemblyQualifiedName
type = Type.GetType(name);
if (type != null)
{
_typeByName[name] = type;
return type;
}

// Check all the other assemblies, from last imported to first
foreach (var assembly in _assemblies)
{
type = assembly.GetType(name);
if (type != null)
{
_typeByName[name] = type;
return type;
}
}
return null;
}
}
}
}
11 changes: 11 additions & 0 deletions Utility/TypeCache.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "jillejr.newtonsoft.json-for-unity.converters",
"displayName": "Json.NET Converters of Unity types",
"version": "1.1.0",
"version": "1.1.1",
"unity": "2018.1",
"description": "This package contains converters to and from common Unity types for Newtonsoft.Json. Types such as Vector2, Vector3, Matrix4x4, Quaternions, Color, even ScriptableObject, and more.\n\nGoes hand in hand with the jillejr.newtonsoft.json-for-unity package.\n\nThis package is licensed under The MIT License (MIT)\n\nCopyright © 2019 Kalle Jillheden (jilleJr)\nhttps://github.com/jilleJr/Newtonsoft.Json-for-Unity.Converters\n\nCopyright © 2020 Wanzyee Studio\nhttp://wanzyeestudio.blogspot.com/2017/03/jsonnet-converters.html\n\nCopyright © 2007 ParentElement\nhttps://github.com/ianmacgillivray/Json-NET-for-Unity\n\nCopyright © 2017 .NET Foundation and Contributors\nhttps://github.com/dotnet/runtime\n\nSee full copyrights in LICENSE.md inside package",
"main": "index.js",
Expand Down

0 comments on commit 85d4fd5

Please sign in to comment.