forked from DynamoDS/Dynamo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from DynamoDS/master
Update master
- Loading branch information
Showing
10 changed files
with
121 additions
and
33 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using Dynamo.Utilities; | ||
using Python.Runtime; | ||
|
||
namespace DSCPython.Encoders | ||
{ | ||
internal class ListEncoder : IPyObjectEncoder, IPyObjectDecoder | ||
{ | ||
private static readonly Type[] decodableTypes = new Type[] | ||
{ | ||
typeof(IList), typeof(ArrayList), | ||
typeof(IList<>), typeof(List<>) | ||
}; | ||
|
||
public bool CanDecode(PyObject objectType, Type targetType) | ||
{ | ||
if (targetType.IsGenericType) | ||
{ | ||
targetType = targetType.GetGenericTypeDefinition(); | ||
} | ||
return decodableTypes.IndexOf(targetType) >= 0; | ||
} | ||
|
||
public bool CanEncode(Type type) | ||
{ | ||
return typeof(IList).IsAssignableFrom(type); | ||
} | ||
|
||
public bool TryDecode<T>(PyObject pyObj, out T value) | ||
{ | ||
if (!PySequence.IsSequenceType(pyObj)) | ||
{ | ||
value = default; | ||
return false; | ||
} | ||
|
||
using (var pyList = PyList.AsList(pyObj)) | ||
{ | ||
if (typeof(T).IsGenericType) | ||
{ | ||
value = pyList.ToList<T>(); | ||
} | ||
else | ||
{ | ||
value = (T)pyList.ToList(); | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
public PyObject TryEncode(object value) | ||
{ | ||
// This is a no-op to prevent Python.NET from encoding generic lists | ||
// https://github.com/pythonnet/pythonnet/pull/963#issuecomment-642938541 | ||
return PyObject.FromManagedObject(value); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters