Cross Platform Primitive Types with Generics
See original GitHub issueSource/destination types
public class TestClass<T>
{
public int A { get; set; }
public T B { get; set; }
}
Source/destination JSON
With UWP/.NetCore:
{
"$type": "AppTest.MainPage+TestClass`1[[System.Int32, System.Private.CoreLib]], AppTest",
"A": 1,
"B": 2
}
With .NET Framework
{
"$type": "AppTest.MainPage+TestClass`1[[System.Int32, mscorlib]], AppTest",
"A": 1,
"B": 2
}
Expected behavior
Successful deserialization. Or the suggested fix of serializing primitives without the assembly name:
{
"$type": "AppTest.MainPage+TestClass`1[[System.Int32]], AppTest",
"A": 1,
"B": 2
}
Actual behavior
Runtime Exception:
Newtonsoft.Json.JsonSerializationException: ‘Error resolving type specified in JSON ‘ConsoleApp3.Program+TestClass`1[[System.Int32, mscorlib]], ConsoleApp3’. Path ‘$type’, line 1, position 81.’
Steps to reproduce
- Serialize class with TypeNameHandling (Tested with
TypeNameHandling.Objects
)
string serialized = JsonConvert.SerializeObject(a, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Objects});
- Deserialize json file on a different .Net system a. That is, .NETFramework if UWP/.NETCore, and vice versa.
object deserialized = JsonConvert.DeserializeObject(serialized, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Objects });
Issue Analytics
- State:
- Created 5 years ago
- Reactions:5
- Comments:19 (2 by maintainers)
Top Results From Across the Web
JEP 218: Generics over Primitive Types
Summary. Extend generic types to support the specialization of generic classes and interfaces over primitive types.
Read more >Generics for Native Types (Primitives) in Java (or ...
I'm currently using two classes which both represent a data structure (sth like a special buffer, but to keep things simple in this...
Read more >Can you use primitive types for generic parameters in a ...
No. Generics aren't meant to represent primitives. They're meant to represent objects (as the generic types all extend Object implicitly).
Read more >Java Generics - No Primitive Types
Using generics, primitive types can not be passed as type parameters. In the example given below, if we pass int primitive type to...
Read more >OpenJDK proposals would bring universal generics to Java
Universal generics would allow Java type variables to range over primitive and reference types, making it easier to extend or reuse code ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@danebou i didnt consider my suggestion a “fix”, just a workaround. My preference would be that this scenario was supported OOTB
potential workaround?