Skip to content

Commit

Permalink
feat: migrate from DeerJson and add unity type ser/deser
Browse files Browse the repository at this point in the history
  • Loading branch information
Wxwind committed Dec 10, 2023
0 parents commit 65566fa
Show file tree
Hide file tree
Showing 246 changed files with 8,075 additions and 0 deletions.
74 changes: 74 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# This .gitignore file should be placed at the root of your Unity project directory
#
# Get latest from https://github.com/github/gitignore/blob/main/Unity.gitignore
#
/[Ll]ibrary/
/[Tt]emp/
/[Oo]bj/
/[Bb]uild/
/[Bb]uilds/
/[Ll]ogs/
/[Uu]ser[Ss]ettings/

# MemoryCaptures can get excessive in size.
# They also could contain extremely sensitive data
/[Mm]emoryCaptures/

# Recordings can get excessive in size
/[Rr]ecordings/

# Uncomment this line if you wish to ignore the asset store tools plugin
# /[Aa]ssets/AssetStoreTools*

# Autogenerated Jetbrains Rider plugin
/[Aa]ssets/Plugins/Editor/JetBrains*

# Visual Studio cache directory
.vs/

# Gradle cache directory
.gradle/

# Autogenerated VS/MD/Consulo solution and project files
ExportedObj/
.consulo/
*.csproj
*.unityproj
*.sln
*.suo
*.tmp
*.user
*.userprefs
*.pidb
*.booproj
*.svd
*.pdb
*.mdb
*.opendb
*.VC.db

# Unity3D generated meta files
*.pidb.meta
*.pdb.meta
*.mdb.meta

# Unity3D generated file on crash reports
sysinfo.txt

# Builds
*.apk
*.aab
*.unitypackage
*.app

# Crashlytics generated file
crashlytics-build.properties

# Packed Addressables
/[Aa]ssets/[Aa]ddressable[Aa]ssets[Dd]ata/*/*.bin*

# Temporary auto-generated Android Assets
/[Aa]ssets/[Ss]treamingAssets/aa.meta
/[Aa]ssets/[Ss]treamingAssets/aa/*

.idea/
8 changes: 8 additions & 0 deletions Assets/Plugins.meta

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

8 changes: 8 additions & 0 deletions Assets/Plugins/DeerJson.meta

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

8 changes: 8 additions & 0 deletions Assets/Plugins/DeerJson/Attributes.meta

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

9 changes: 9 additions & 0 deletions Assets/Plugins/DeerJson/Attributes/JsonIgnore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace DeerJson.Attributes
{
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class JsonIgnore : Attribute
{
}
}
11 changes: 11 additions & 0 deletions Assets/Plugins/DeerJson/Attributes/JsonIgnore.cs.meta

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

8 changes: 8 additions & 0 deletions Assets/Plugins/DeerJson/Deserializer.meta

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

47 changes: 47 additions & 0 deletions Assets/Plugins/DeerJson/Deserializer/DeserializeContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using DeerJson.Deserializer.std;

namespace DeerJson.Deserializer
{
public class DeserializeContext
{
private readonly DeserializerCache m_cache;
private readonly JsonConfigure m_configure;

public DeserializeContext() : this(null)
{
}

public DeserializeContext(JsonConfigure cfg)
{
m_cache = new DeserializerCache();
m_configure = cfg;
}

private DeserializeContext(JsonConfigure cfg, DeserializerCache cache)
{
m_cache = cache;
m_configure = cfg;
}

public bool IsEnabled(JsonFeature f)
{
return m_configure.IsEnabled(f);
}

public IKeyDeserializer FindStdKeySerializer(Type type)
{
return m_cache.FindStdKeySerializer(type);
}

public IDeserializer FindDeserializer(Type type)
{
return m_cache.FindOrCreateDeserializer(this, type);
}

public void AddCustomDeserializer<T>(JsonDeserializer<T> deserializer)
{
m_cache.AddCustomDeserializer(deserializer);
}
}
}
11 changes: 11 additions & 0 deletions Assets/Plugins/DeerJson/Deserializer/DeserializeContext.cs.meta

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

151 changes: 151 additions & 0 deletions Assets/Plugins/DeerJson/Deserializer/DeserializerCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
using System;
using System.Collections;
using System.Collections.Generic;
using DeerJson.Deserializer.std;
using DeerJson.Deserializer.std.Key;
using DeerJson.Node;

namespace DeerJson.Deserializer
{
public class DeserializerCache
{
private readonly Dictionary<Type, IDeserializer> m_customDeserializerDict =
new Dictionary<Type, IDeserializer>();

private readonly Dictionary<Type, IDeserializer> m_cachedDeserializerDict =
new Dictionary<Type, IDeserializer>();

// avoid cyclic dependencies
private readonly Dictionary<Type, IDeserializer> m_incompleteDeserializers =
new Dictionary<Type, IDeserializer>();


private readonly DeserializerFactory m_factory = new DeserializerFactory();

private readonly Dictionary<Type, IKeyDeserializer> m_cachedKeyDeserializerDict =
new Dictionary<Type, IKeyDeserializer>();

public IKeyDeserializer FindStdKeySerializer(Type type)
{
if (m_cachedKeyDeserializerDict.TryGetValue(type, out var deser)) return deser;

if (type.IsEnum)
{
var underlyingType = Enum.GetUnderlyingType(type);
var d = new EnumKeyDeserializer(type, underlyingType);
m_cachedKeyDeserializerDict.Add(type, d);
return d;
}

if (type.IsPrimitive || type == typeof(string))
{
var d = new StdKeyDeserializer(type);
m_cachedKeyDeserializerDict.Add(type, d);
return d;
}

throw new JsonException($"not support key of type {type}");
}

public IDeserializer FindOrCreateDeserializer(DeserializeContext ctx, Type type)
{
if (m_customDeserializerDict.TryGetValue(type, out var deserializer)) return deserializer;

if (m_cachedDeserializerDict.TryGetValue(type, out var deser)) return deser;

// may be in creating deserializer
if (m_incompleteDeserializers.TryGetValue(type, out var deser2)) return deser2;

var d = CreateAndCacheDeserializer(ctx, type);
return d;
}

private IDeserializer CreateAndCacheDeserializer(DeserializeContext ctx, Type type)
{
var underlyingType = Nullable.GetUnderlyingType(type);
var realType = underlyingType ?? type;

var deser = CreateDeserializer(realType);

if (deser is IResolvableDeserializer d)
{
m_incompleteDeserializers.Add(type, deser);
d.Resolve(ctx);
m_incompleteDeserializers.Remove(type);
}

m_cachedDeserializerDict.Add(realType, deser);
return deser;
}

private IDeserializer CreateDeserializer(Type type)
{
// may just being resolved cuz in dealing with cyclic dependencies.
if (m_incompleteDeserializers.TryGetValue(type, out var a)) return a;

// is enum
if (type.IsEnum)
{
var deser = m_factory.CreateEnumDeserializer(type);
return deser;
}

// TODO: support dynamic type collection.
// is array
if (type.IsArray)
{
return m_factory.CreateArrayDeserializer(type);
}

//is list like
if (typeof(IList).IsAssignableFrom(type))
{
return m_factory.CreateListDeserializer(type);
}

// is dictionary like
if (typeof(IDictionary).IsAssignableFrom(type))
{
return m_factory.CreateDictionaryDeserializer(type);
}

// is string
if (type == typeof(string))
{
return StringDeserializer.Instance;
}

// is std primitive type
if (type.IsPrimitive)
{
return m_factory.FindStdPrimitiveDeserializer(type);
}

// is class
if (type.IsClass && !type.IsSubclassOf(typeof(Delegate)) && !(type == typeof(string)))
{
// TODO: is generic?
var deser = type == typeof(JsonNode)
? m_factory.CreateJsonObjectDeserializer()
: m_factory.CreateObjectDeserializer(type);
return deser;
}

// is struct
if (type.IsValueType && !type.IsPrimitive && !type.IsEnum)
{
var deser = m_factory.CreateObjectDeserializer(type);
return deser;
}


throw new JsonException($"not supported deserialize of type '{type}'");
}

public void AddCustomDeserializer<T>(JsonDeserializer<T> deserializer)
{
m_customDeserializerDict.Add(typeof(T), deserializer);
}

}
}
11 changes: 11 additions & 0 deletions Assets/Plugins/DeerJson/Deserializer/DeserializerCache.cs.meta

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

Loading

0 comments on commit 65566fa

Please sign in to comment.